Skip to content

Commit 7cb5105

Browse files
marcopiiiveej
authored andcommitted
create BaseDateInput
1 parent 4270d35 commit 7cb5105

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import { DatePickerAria, DateRangePickerAria } from "@react-aria/datepicker";
2+
import { DatePickerState, DateRangePickerState } from "@react-stately/datepicker";
3+
import React from "react";
4+
import { Input } from "./Input";
5+
import { Calendar } from "./Calendar";
6+
import { Box } from "../Box/Box";
7+
import { Inline } from "../Layout/Inline";
8+
import { Button, FieldProps } from "..";
9+
import { ShortcutProps } from "./types";
10+
11+
type SingleDateProps = {
12+
type?: "single";
13+
shortcuts?: ShortcutProps<Date | null>[];
14+
datePickerAria: DatePickerAria;
15+
datePickerState: DatePickerState;
16+
} & Pick<FieldProps<Date | null>, "onChange" | "value" | "disabled">;
17+
18+
type RangeDateProps = {
19+
type: "range";
20+
shortcuts?: ShortcutProps<[Date, Date] | null>[];
21+
dateRangePickerAria: DateRangePickerAria;
22+
dateRangePickerState: DateRangePickerState;
23+
} & Pick<FieldProps<[Date, Date] | null>, "onChange" | "value" | "disabled">;
24+
25+
type Props = (SingleDateProps | RangeDateProps) & { inputRef: React.RefObject<HTMLInputElement> };
26+
27+
export function BaseSingleDateInput(props: Extract<Props, { type?: "single" }>) {
28+
const shortcuts = props.shortcuts && (
29+
<Inline space={4}>
30+
{props.shortcuts.map((shortcut) => (
31+
<Button
32+
key={shortcut.label}
33+
kind="transparent"
34+
hierarchy="secondary"
35+
size="small"
36+
label={shortcut.label}
37+
onPress={() => {
38+
props.onChange(shortcut.value);
39+
props.datePickerState.close();
40+
}}
41+
/>
42+
))}
43+
</Inline>
44+
);
45+
46+
return (
47+
<>
48+
<Box {...props.datePickerAria.groupProps} ref={props.inputRef}>
49+
<Input
50+
type="single"
51+
fieldProps={props.datePickerAria.fieldProps}
52+
buttonProps={props.datePickerAria.buttonProps}
53+
isCalendarOpen={props.datePickerState.isOpen}
54+
/>
55+
</Box>
56+
{props.datePickerState.isOpen && (
57+
<Calendar
58+
type="single"
59+
{...props.datePickerAria.calendarProps}
60+
inputRef={props.inputRef}
61+
onClose={props.datePickerState.close}
62+
shortcuts={shortcuts}
63+
/>
64+
)}
65+
</>
66+
);
67+
}
68+
69+
export function BaseRangeDateInput(props: Extract<Props, { type: "range" }>) {
70+
const shortcuts = props.shortcuts && (
71+
<Inline space={4}>
72+
{props.shortcuts.map((shortcut) => (
73+
<Button
74+
key={shortcut.label}
75+
kind="transparent"
76+
hierarchy="secondary"
77+
size="small"
78+
label={shortcut.label}
79+
onPress={() => {
80+
props.onChange(shortcut.value);
81+
props.dateRangePickerState.close();
82+
}}
83+
/>
84+
))}
85+
</Inline>
86+
);
87+
88+
return (
89+
<>
90+
<Box {...props.dateRangePickerAria.groupProps} ref={props.inputRef}>
91+
<Input
92+
type="range"
93+
fieldProps={{
94+
start: props.dateRangePickerAria.startFieldProps,
95+
end: props.dateRangePickerAria.endFieldProps,
96+
}}
97+
buttonProps={props.dateRangePickerAria.buttonProps}
98+
isCalendarOpen={props.dateRangePickerState.isOpen}
99+
/>
100+
</Box>
101+
{props.dateRangePickerState.isOpen && (
102+
<Calendar
103+
type="range"
104+
{...props.dateRangePickerAria.calendarProps}
105+
inputRef={props.inputRef}
106+
onClose={props.dateRangePickerState.close}
107+
shortcuts={shortcuts}
108+
/>
109+
)}
110+
</>
111+
);
112+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { LocalizedString } from "../util/ConfigurableTypes";
2+
3+
export type DateProps = {
4+
minDate?: Date;
5+
maxDate?: Date;
6+
shouldDisableDate?: (date: Date) => boolean;
7+
isReadOnly?: boolean;
8+
/** @deprecated Use `isReadOnly` instead. */
9+
readOnly?: boolean;
10+
};
11+
12+
export type ShortcutProps<V> = {
13+
label: LocalizedString;
14+
value: V;
15+
};

0 commit comments

Comments
 (0)