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

feat(date-picker): added isOpen and onOpenChange #3476

Open
wants to merge 7 commits into
base: canary
Choose a base branch
from
5 changes: 5 additions & 0 deletions .changeset/silent-countries-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nextui-org/date-picker": patch
---

Added isOpen and onOpenChanged to DatePicker (#3457)
2 changes: 2 additions & 0 deletions packages/components/date-picker/src/use-date-picker-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export function useDatePickerBase<T extends DateValue>(originalProps: UseDatePic
CalendarTopContent,
CalendarBottomContent,
createCalendar,
onOpenChange,
} = props;

const {
Expand Down Expand Up @@ -297,6 +298,7 @@ export function useDatePickerBase<T extends DateValue>(originalProps: UseDatePic
selectorButtonProps,
selectorIconProps,
onClose,
onOpenChange,
};
}

Expand Down
4 changes: 4 additions & 0 deletions packages/components/date-picker/src/use-date-picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,15 @@ export function useDatePicker<T extends DateValue>({
selectorButtonProps,
selectorIconProps,
onClose,
onOpenChange,
} = useDatePickerBase({...originalProps, validationBehavior});

let state: DatePickerState = useDatePickerState({
...originalProps,
validationBehavior,
shouldCloseOnSelect: () => !state.hasTime,
onOpenChange: (isOpen) => {
onOpenChange?.(isOpen);
if (!isOpen) {
onClose();
}
Expand Down Expand Up @@ -228,6 +230,8 @@ export function useDatePicker<T extends DateValue>({

return {
state,
isOpen: state.isOpen,
onOpenChange,
endContent,
selectorIcon,
showTimeField,
Expand Down
30 changes: 27 additions & 3 deletions packages/components/date-picker/stories/date-picker.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ const PresetsTemplate = (args: DatePickerProps) => {
let defaultDate = today(getLocalTimeZone());

const [value, setValue] = React.useState<DateValue>(defaultDate);
const [isOpen, setIsOpen] = React.useState<boolean>(false);

let {locale} = useLocale();
let formatter = useDateFormatter({dateStyle: "full"});
Expand Down Expand Up @@ -256,9 +257,30 @@ const PresetsTemplate = (args: DatePickerProps) => {
size="sm"
variant="bordered"
>
<Button onPress={() => setValue(now)}>Today</Button>
<Button onPress={() => setValue(nextWeek)}>Next week</Button>
<Button onPress={() => setValue(nextMonth)}>Next month</Button>
<Button
onPress={() => {
setValue(now);
setIsOpen(false);
}}
>
Today
</Button>
<Button
onPress={() => {
setValue(nextWeek);
setIsOpen(false);
}}
>
Next week
</Button>
<Button
onPress={() => {
setValue(nextMonth);
setIsOpen(false);
}}
>
Next month
</Button>
</ButtonGroup>
}
calendarProps={{
Expand All @@ -271,8 +293,10 @@ const PresetsTemplate = (args: DatePickerProps) => {
variant: "bordered",
},
}}
isOpen={isOpen}
value={value}
onChange={setValue}
onOpenChange={setIsOpen}
{...args}
label="Event date"
/>
Expand Down