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 17242e8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 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 @@ -482,9 +487,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} />,
);
cy.mount(<Single openOnClick defaultSelectedDate={initialDate} />);
cy.findByRole("application").should("not.exist");
// Simulate opening the calendar on click
cy.document().find("input").realClick();
Expand All @@ -503,12 +506,7 @@ describe("GIVEN a DatePicker where selectionVariant is single", () => {
});

it("SHOULD be able to enable the overlay to open on keydown", () => {
cy.mount(
<UncontrolledOpen
openOnKeyDown
defaultSelectedDate={initialDate}
/>,
);
cy.mount(<Single openOnKeyDown defaultSelectedDate={initialDate} />);
cy.findByRole("application").should("not.exist");
// Simulate opening the calendar on arrow down
cy.document().find("input").realClick();
Expand All @@ -529,9 +527,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} />,
);
cy.mount(<Single openOnFocus defaultSelectedDate={initialDate} />);
cy.findByRole("application").should("not.exist");
// Simulate opening the calendar on focus
cy.document().find("input").focus();
Expand Down
13 changes: 7 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,13 @@ export const DatePickerOverlayProvider: React.FC<
: [
useDismiss(floatingUIResult.context),
useFocus(floatingUIResult.context, {
enabled: !!openOnFocus,
enabled: !!openOnFocus && !readOnly,
}),
useKeyboard(floatingUIResult.context, {
enabled: !!openOnKeyDown && !readOnly,
}),
useKeyboard(floatingUIResult.context, { enabled: !!openOnKeyDown }),
useClick(floatingUIResult.context, {
enabled: !!openOnClick,
enabled: !!openOnClick && !readOnly,
toggle: false,
}),
],
Expand Down
2 changes: 1 addition & 1 deletion packages/lab/stories/date-picker/date-picker.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ Range.args = {

export const SingleReadOnly = DatePickerSingleTemplate.bind({});
SingleReadOnly.args = {
readOnly: true
readOnly: true,
};

export const RangeReadOnly = DatePickerRangeTemplate.bind({});
Expand Down

0 comments on commit 17242e8

Please sign in to comment.