Skip to content

Commit

Permalink
fix: Restore retrocompability
Browse files Browse the repository at this point in the history
  • Loading branch information
d-beezee committed Apr 3, 2024
1 parent 50b73d3 commit 78c22a7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
11 changes: 3 additions & 8 deletions src/stories/DateInput/DateInput.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,10 @@ export default {
} as Meta;

const DatepickerTemplate: StoryFn = (args) => {
function getEighteenYearsAgo(): string {
function getEighteenYearsAgo() {
const date = new Date();
date.setFullYear(date.getFullYear() - 18);

const year = date.getFullYear();
const month = ("0" + (date.getMonth() + 1)).slice(-2); // Months are 0 based, so +1 and pad with 0
const day = ("0" + date.getDate()).slice(-2);

return `${year}-${month}-${day}`;
return date;
}
const [value, setValue] = useState("");
const [isInvalid, setIsInvalid] = useState(false);
Expand All @@ -31,7 +26,7 @@ const DatepickerTemplate: StoryFn = (args) => {
isInvalid={isInvalid}
id="test1"
value={value}
max={getEighteenYearsAgo()}
maxDate={getEighteenYearsAgo()}
onChange={(e) => setValue(e.target.value)}
onBlur={(e) => {
setIsInvalid(!value);
Expand Down
26 changes: 24 additions & 2 deletions src/stories/DateInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,36 @@ const StyledInput = styled(FormInput)`
padding: 0px;
}
`;
function formatDate(date: Date): string {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
return `${year}-${month}-${day}`;
}

export const DateInput = ({
isInvalid,
maxDate,
minDate,
inputProps,
...props
}: { isInvalid?: boolean } & React.InputHTMLAttributes<HTMLInputElement>) => {
}: {
isInvalid?: boolean;
maxDate?: Date;
minDate?: Date;
inputProps?: React.InputHTMLAttributes<HTMLInputElement>;
} & React.InputHTMLAttributes<HTMLInputElement>) => {
return (
<StyledInput type="text" isInvalid={isInvalid}>
<input id={props.id} name={props.name} type="date" {...props} />
<input
id={props.id}
name={props.name}
type="date"
max={maxDate ? formatDate(maxDate) : undefined}
min={minDate ? formatDate(minDate) : undefined}
{...props}
{...inputProps}
/>
</StyledInput>
);
};

0 comments on commit 78c22a7

Please sign in to comment.