Skip to content

Commit

Permalink
update un-controlled open behaviour to be disabled when read-only
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-tate committed Dec 13, 2024
1 parent e8d9ea7 commit bce766b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ describe("GIVEN a DatePicker where selectionVariant is single", () => {
cy.findByRole("textbox").click().type("{downArrow}", { force: true });
cy.findByRole("application").should("not.exist");
});

it("SHOULD not open overlay if defaultOpen is set", () => {
cy.mount(<Single readOnly defaultOpen/>);
cy.findByRole("application").should("not.exist");
});
});

adapters.forEach((adapter: SaltDateAdapter<DateFrameworkType>) => {
Expand Down Expand Up @@ -483,7 +488,7 @@ describe("GIVEN a DatePicker where selectionVariant is single", () => {

it("SHOULD be able to enable the overlay to open on click", () => {
cy.mount(
<UncontrolledOpen openOnClick defaultSelectedDate={initialDate} />,
<Single openOnClick defaultSelectedDate={initialDate} />,
);
cy.findByRole("application").should("not.exist");
// Simulate opening the calendar on click
Expand All @@ -504,7 +509,7 @@ describe("GIVEN a DatePicker where selectionVariant is single", () => {

it("SHOULD be able to enable the overlay to open on keydown", () => {
cy.mount(
<UncontrolledOpen
<Single
openOnKeyDown
defaultSelectedDate={initialDate}
/>,
Expand All @@ -530,7 +535,7 @@ describe("GIVEN a DatePicker where selectionVariant is single", () => {

it("SHOULD be able to enable the overlay to open on focus", () => {
cy.mount(
<UncontrolledOpen openOnFocus defaultSelectedDate={initialDate} />,
<Single openOnFocus defaultSelectedDate={initialDate} />,
);
cy.findByRole("application").should("not.exist");
// Simulate opening the calendar on focus
Expand Down
11 changes: 5 additions & 6 deletions packages/lab/src/date-picker/DatePickerOverlayProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export const DatePickerOverlayProvider: React.FC<
open: openProp,
openOnClick,
openOnFocus,
openOnKeyDown,
openOnKeyDown = true,
defaultOpen,
onOpen,
children,
Expand All @@ -135,7 +135,7 @@ export const DatePickerOverlayProvider: React.FC<
}) => {
const [open, setOpenState, isOpenControlled] = useControlled({
controlled: openProp,
default: Boolean(defaultOpen),
default: readOnly ? false : Boolean(defaultOpen),
name: "DatePicker",
state: "openDatePickerOverlay",
});
Expand All @@ -146,7 +146,6 @@ export const DatePickerOverlayProvider: React.FC<
(newOpen: boolean, _event?: Event, reason?: OpenChangeReason) => {
if (newOpen) {
if (readOnly) {
// When not open overlay when readOnly
return;
}
triggeringElement.current = document.activeElement as HTMLElement;
Expand Down Expand Up @@ -192,11 +191,11 @@ export const DatePickerOverlayProvider: React.FC<
: [
useDismiss(floatingUIResult.context),
useFocus(floatingUIResult.context, {
enabled: !!openOnFocus,
enabled: !!openOnFocus && !readOnly,
}),
useKeyboard(floatingUIResult.context, { enabled: !!openOnKeyDown }),
useKeyboard(floatingUIResult.context, { enabled: !!openOnKeyDown && !readOnly }),
useClick(floatingUIResult.context, {
enabled: !!openOnClick,
enabled: !!openOnClick && !readOnly,
toggle: false,
}),
],
Expand Down

0 comments on commit bce766b

Please sign in to comment.