Skip to content

Commit

Permalink
Merge pull request #14348 from gabiseabra/fix/issue_13771
Browse files Browse the repository at this point in the history
Controls: Fix edge case in datetime control
  • Loading branch information
shilman committed Mar 30, 2021
2 parents a686a99 + f1df09e commit 5595b1e
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 9 deletions.
4 changes: 1 addition & 3 deletions addons/knobs/src/components/types/Date.tsx
Expand Up @@ -86,9 +86,7 @@ export default class DateType extends Component<DateTypeProps, DateTypeState> {
const [year, month, day] = e.target.value.split('-');
const result = new Date(knob.value);
if (result.getTime()) {
result.setFullYear(parseInt(year, 10));
result.setMonth(parseInt(month, 10) - 1);
result.setDate(parseInt(day, 10));
result.setFullYear(parseInt(year, 10), parseInt(month, 10) - 1, parseInt(day, 10));
if (result.getTime()) {
valid = true;
onChange(result.getTime());
Expand Down
8 changes: 2 additions & 6 deletions lib/components/src/controls/Date.tsx
Expand Up @@ -7,9 +7,7 @@ import { ControlProps, DateValue, DateConfig } from './types';
const parseDate = (value: string) => {
const [year, month, day] = value.split('-');
const result = new Date();
result.setFullYear(parseInt(year, 10));
result.setMonth(parseInt(month, 10) - 1);
result.setDate(parseInt(day, 10));
result.setFullYear(parseInt(year, 10), parseInt(month, 10) - 1, parseInt(day, 10));
return result;
};

Expand Down Expand Up @@ -75,9 +73,7 @@ export const DateControl: FC<DateProps> = ({ name, value, onChange, onFocus, onB
const onDateChange = (e: ChangeEvent<HTMLInputElement>) => {
const parsed = parseDate(e.target.value);
const result = new Date(value);
result.setFullYear(parsed.getFullYear());
result.setMonth(parsed.getMonth());
result.setDate(parsed.getDate());
result.setFullYear(parsed.getFullYear(), parsed.getMonth(), parsed.getDate());
const time = result.getTime();
if (time) onChange(time);
setValid(!!time);
Expand Down

0 comments on commit 5595b1e

Please sign in to comment.