Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controls: Fix edge case in datetime control #14348

Merged
merged 2 commits into from Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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