Skip to content

Commit

Permalink
fix shouldDisabledDate and clear date for range picker
Browse files Browse the repository at this point in the history
  • Loading branch information
veej committed Sep 27, 2023
1 parent 429bc91 commit 83b69d9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
25 changes: 17 additions & 8 deletions packages/bento-design-system/src/DateField/DateField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,31 @@ type RangeDateFieldProps = {
type Props = (SingleDateFieldProps | RangeDateFieldProps) & {
minDate?: Date;
maxDate?: Date;
shouldDisableDate?: (date: DateValue) => boolean;
shouldDisableDate?: (date: Date) => boolean;
readOnly?: boolean;
};

function dateToCalendarDate(date: Date): CalendarDate {
if (!date) return date;
return new CalendarDate(date.getFullYear(), date.getMonth() + 1, date.getDate());
}

function SingleDateField({ disabled, readOnly, ...props }: Extract<Props, { type?: "single" }>) {
const localTimeZone = getLocalTimeZone();

const internalProps = {
...props,
value: props.value ? dateToCalendarDate(props.value) : props.value,
onChange: (date: CalendarDate | null) => {
props.onChange(date?.toDate(getLocalTimeZone()) ?? null);
props.onChange(date?.toDate(localTimeZone) ?? null);
},
isDisabled: disabled,
isReadOnly: readOnly,
validationState: props.issues ? "invalid" : "valid",
minValue: props.minDate ? dateToCalendarDate(props.minDate) : undefined,
maxValue: props.maxDate ? dateToCalendarDate(props.maxDate) : undefined,
isDateUnavailable: props.shouldDisableDate,
isDateUnavailable: props.shouldDisableDate
? (date: DateValue) => props.shouldDisableDate!(date.toDate(localTimeZone))
: undefined,
shouldForceLeadingZeros: true,
} as const;
const state = useDatePickerState(internalProps);
Expand Down Expand Up @@ -111,23 +114,29 @@ function SingleDateField({ disabled, readOnly, ...props }: Extract<Props, { type
}

function RangeDateField({ disabled, readOnly, ...props }: Extract<Props, { type: "range" }>) {
const localTimeZone = getLocalTimeZone();
const internalProps = {
...props,
isDisabled: disabled,
isReadOnly: readOnly,
validationState: props.issues ? "invalid" : "valid",
minValue: props.minDate ? dateToCalendarDate(props.minDate) : undefined,
maxValue: props.maxDate ? dateToCalendarDate(props.maxDate) : undefined,
isDateUnavailable: props.shouldDisableDate,
isDateUnavailable: props.shouldDisableDate
? (date: DateValue) => props.shouldDisableDate!(date.toDate(localTimeZone))
: undefined,
value: props.value
? {
start: dateToCalendarDate(props.value[0]),
end: dateToCalendarDate(props.value[1]),
}
: props.value,
onChange: (range: RangeValue<CalendarDate>) => {
const localTimeZone = getLocalTimeZone();
props.onChange([range.start.toDate(localTimeZone), range.end.toDate(localTimeZone)]);
onChange: (range: RangeValue<CalendarDate> | null) => {
if (!range) {
props.onChange(null);
} else {
props.onChange([range.start.toDate(localTimeZone), range.end.toDate(localTimeZone)]);
}
},
} as const;
const state = useDateRangePickerState(internalProps);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export const Range = {

export const RangeWithMinMax = {
args: {
value: [null, null],
value: null,
type: "range",
minDate: today,
maxDate: inOneMonth,
Expand All @@ -129,7 +129,7 @@ export const RangeWithMinMax = {

export const RangeWithShortcuts = {
args: {
value: [null, null],
value: null,
type: "range",
shortcuts: [
{
Expand Down

0 comments on commit 83b69d9

Please sign in to comment.