Skip to content

Commit 449bdc8

Browse files
committed
add DateInput component
1 parent e8f7645 commit 449bdc8

File tree

1 file changed

+82
-33
lines changed

1 file changed

+82
-33
lines changed

Diff for: packages/bento-design-system/src/DateField/DateField.tsx

+82-33
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,49 @@ export type ShortcutProps<Value> = {
1616
label: LocalizedString;
1717
value: Value;
1818
};
19-
type SingleDateFieldProps = {
20-
type?: "single";
21-
shortcuts?: ShortcutProps<Date | null>[];
22-
} & FieldProps<Date | null>;
23-
type RangeDateFieldProps = {
24-
type: "range";
25-
shortcuts?: ShortcutProps<[Date, Date] | null>[];
26-
} & FieldProps<[Date, Date] | null>;
27-
type Props = (SingleDateFieldProps | RangeDateFieldProps) & {
19+
20+
type StandaloneProps<T> = Pick<
21+
FieldProps<T>,
22+
"autoFocus" | "disabled" | "name" | "onBlur" | "onChange" | "value"
23+
>;
24+
25+
type SingleDateProps = { type?: "single"; shortcuts?: ShortcutProps<Date | null>[] };
26+
type RangeDateProps = { type: "range"; shortcuts?: ShortcutProps<[Date, Date] | null>[] };
27+
28+
type SingleDateFieldProps = SingleDateProps & FieldProps<Date | null>;
29+
type SingleDateStandaloneProps = SingleDateProps & StandaloneProps<Date | null>;
30+
31+
type RangeDateFieldProps = RangeDateProps & FieldProps<[Date, Date] | null>;
32+
type RangeDateStandaloneProps = RangeDateProps & StandaloneProps<[Date, Date] | null>;
33+
34+
type DateProps = {
2835
minDate?: Date;
2936
maxDate?: Date;
3037
shouldDisableDate?: (date: Date) => boolean;
3138
readOnly?: boolean;
3239
};
3340

41+
type PublicDateFieldProps = (SingleDateFieldProps | RangeDateFieldProps) & DateProps;
42+
type PublicDateInputProps = (SingleDateStandaloneProps | RangeDateStandaloneProps) & DateProps;
43+
44+
type InternalDateProps =
45+
| ({
46+
isStandalone: true;
47+
} & PublicDateInputProps)
48+
| ({
49+
isStandalone?: false;
50+
} & PublicDateFieldProps);
51+
3452
function dateToCalendarDate(date: Date): CalendarDate {
3553
return new CalendarDate(date.getFullYear(), date.getMonth() + 1, date.getDate());
3654
}
3755

38-
function SingleDateField({ disabled, readOnly, ...props }: Extract<Props, { type?: "single" }>) {
56+
function SingleDate({
57+
disabled,
58+
readOnly,
59+
...props
60+
}: Extract<InternalDateProps, { type?: "single" }>) {
3961
const localTimeZone = getLocalTimeZone();
40-
4162
const internalProps = {
4263
...props,
4364
value: props.value ? dateToCalendarDate(props.value) : props.value,
@@ -46,7 +67,7 @@ function SingleDateField({ disabled, readOnly, ...props }: Extract<Props, { type
4667
},
4768
isDisabled: disabled,
4869
isReadOnly: readOnly,
49-
validationState: props.issues ? "invalid" : "valid",
70+
validationState: !props.isStandalone && props.issues ? "invalid" : "valid",
5071
minValue: props.minDate ? dateToCalendarDate(props.minDate) : undefined,
5172
maxValue: props.maxDate ? dateToCalendarDate(props.maxDate) : undefined,
5273
isDateUnavailable: props.shouldDisableDate
@@ -84,14 +105,8 @@ function SingleDateField({ disabled, readOnly, ...props }: Extract<Props, { type
84105
</Inline>
85106
);
86107

87-
return (
88-
<Field
89-
{...props}
90-
disabled={disabled}
91-
labelProps={labelProps}
92-
assistiveTextProps={descriptionProps}
93-
errorMessageProps={errorMessageProps}
94-
>
108+
const datePicker = (
109+
<>
95110
<Box {...groupProps} ref={ref}>
96111
<Input
97112
type="single"
@@ -109,17 +124,35 @@ function SingleDateField({ disabled, readOnly, ...props }: Extract<Props, { type
109124
shortcuts={shortcuts}
110125
/>
111126
)}
127+
</>
128+
);
129+
130+
return props.isStandalone ? (
131+
datePicker
132+
) : (
133+
<Field
134+
{...props}
135+
disabled={disabled}
136+
labelProps={labelProps}
137+
assistiveTextProps={descriptionProps}
138+
errorMessageProps={errorMessageProps}
139+
>
140+
{datePicker}
112141
</Field>
113142
);
114143
}
115144

116-
function RangeDateField({ disabled, readOnly, ...props }: Extract<Props, { type: "range" }>) {
145+
function RangeDateField({
146+
disabled,
147+
readOnly,
148+
...props
149+
}: Extract<InternalDateProps, { type: "range" }>) {
117150
const localTimeZone = getLocalTimeZone();
118151
const internalProps = {
119152
...props,
120153
isDisabled: disabled,
121154
isReadOnly: readOnly,
122-
validationState: props.issues ? "invalid" : "valid",
155+
validationState: !props.isStandalone && props.issues ? "invalid" : "valid",
123156
minValue: props.minDate ? dateToCalendarDate(props.minDate) : undefined,
124157
maxValue: props.maxDate ? dateToCalendarDate(props.maxDate) : undefined,
125158
isDateUnavailable: props.shouldDisableDate
@@ -171,14 +204,8 @@ function RangeDateField({ disabled, readOnly, ...props }: Extract<Props, { type:
171204
</Inline>
172205
);
173206

174-
return (
175-
<Field
176-
{...props}
177-
disabled={disabled}
178-
labelProps={labelProps}
179-
assistiveTextProps={descriptionProps}
180-
errorMessageProps={errorMessageProps}
181-
>
207+
const datePicker = (
208+
<>
182209
<Box {...groupProps} ref={ref}>
183210
<Input
184211
type="range"
@@ -196,12 +223,34 @@ function RangeDateField({ disabled, readOnly, ...props }: Extract<Props, { type:
196223
shortcuts={shortcuts}
197224
/>
198225
)}
226+
</>
227+
);
228+
229+
return props.isStandalone ? (
230+
datePicker
231+
) : (
232+
<Field
233+
{...props}
234+
disabled={disabled}
235+
labelProps={labelProps}
236+
assistiveTextProps={descriptionProps}
237+
errorMessageProps={errorMessageProps}
238+
>
239+
{datePicker}
199240
</Field>
200241
);
201242
}
202243

203-
export function DateField(props: Props) {
204-
return props.type === "range" ? <RangeDateField {...props} /> : <SingleDateField {...props} />;
244+
export function DateField(props: PublicDateFieldProps) {
245+
return props.type === "range" ? <RangeDateField {...props} /> : <SingleDate {...props} />;
246+
}
247+
248+
export function DateInput(props: PublicDateInputProps) {
249+
return props.type === "range" ? (
250+
<RangeDateField {...props} isStandalone />
251+
) : (
252+
<SingleDate {...props} isStandalone />
253+
);
205254
}
206255

207-
export type { Props as DateFieldProps };
256+
export type { PublicDateFieldProps as DateFieldProps, PublicDateInputProps as DateInputProps };

0 commit comments

Comments
 (0)