Skip to content

Commit 17242e8

Browse files
committed
update un-controlled open behaviour to be disabled when read-only
1 parent e8d9ea7 commit 17242e8

File tree

3 files changed

+16
-19
lines changed

3 files changed

+16
-19
lines changed

packages/lab/src/__tests__/__e2e__/date-picker/DatePicker.single.cy.tsx

+8-12
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ describe("GIVEN a DatePicker where selectionVariant is single", () => {
9090
cy.findByRole("textbox").click().type("{downArrow}", { force: true });
9191
cy.findByRole("application").should("not.exist");
9292
});
93+
94+
it("SHOULD not open overlay if defaultOpen is set", () => {
95+
cy.mount(<Single readOnly defaultOpen />);
96+
cy.findByRole("application").should("not.exist");
97+
});
9398
});
9499

95100
adapters.forEach((adapter: SaltDateAdapter<DateFrameworkType>) => {
@@ -482,9 +487,7 @@ describe("GIVEN a DatePicker where selectionVariant is single", () => {
482487
});
483488

484489
it("SHOULD be able to enable the overlay to open on click", () => {
485-
cy.mount(
486-
<UncontrolledOpen openOnClick defaultSelectedDate={initialDate} />,
487-
);
490+
cy.mount(<Single openOnClick defaultSelectedDate={initialDate} />);
488491
cy.findByRole("application").should("not.exist");
489492
// Simulate opening the calendar on click
490493
cy.document().find("input").realClick();
@@ -503,12 +506,7 @@ describe("GIVEN a DatePicker where selectionVariant is single", () => {
503506
});
504507

505508
it("SHOULD be able to enable the overlay to open on keydown", () => {
506-
cy.mount(
507-
<UncontrolledOpen
508-
openOnKeyDown
509-
defaultSelectedDate={initialDate}
510-
/>,
511-
);
509+
cy.mount(<Single openOnKeyDown defaultSelectedDate={initialDate} />);
512510
cy.findByRole("application").should("not.exist");
513511
// Simulate opening the calendar on arrow down
514512
cy.document().find("input").realClick();
@@ -529,9 +527,7 @@ describe("GIVEN a DatePicker where selectionVariant is single", () => {
529527
});
530528

531529
it("SHOULD be able to enable the overlay to open on focus", () => {
532-
cy.mount(
533-
<UncontrolledOpen openOnFocus defaultSelectedDate={initialDate} />,
534-
);
530+
cy.mount(<Single openOnFocus defaultSelectedDate={initialDate} />);
535531
cy.findByRole("application").should("not.exist");
536532
// Simulate opening the calendar on focus
537533
cy.document().find("input").focus();

packages/lab/src/date-picker/DatePickerOverlayProvider.tsx

+7-6
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export const DatePickerOverlayProvider: React.FC<
126126
open: openProp,
127127
openOnClick,
128128
openOnFocus,
129-
openOnKeyDown,
129+
openOnKeyDown = true,
130130
defaultOpen,
131131
onOpen,
132132
children,
@@ -135,7 +135,7 @@ export const DatePickerOverlayProvider: React.FC<
135135
}) => {
136136
const [open, setOpenState, isOpenControlled] = useControlled({
137137
controlled: openProp,
138-
default: Boolean(defaultOpen),
138+
default: readOnly ? false : Boolean(defaultOpen),
139139
name: "DatePicker",
140140
state: "openDatePickerOverlay",
141141
});
@@ -146,7 +146,6 @@ export const DatePickerOverlayProvider: React.FC<
146146
(newOpen: boolean, _event?: Event, reason?: OpenChangeReason) => {
147147
if (newOpen) {
148148
if (readOnly) {
149-
// When not open overlay when readOnly
150149
return;
151150
}
152151
triggeringElement.current = document.activeElement as HTMLElement;
@@ -192,11 +191,13 @@ export const DatePickerOverlayProvider: React.FC<
192191
: [
193192
useDismiss(floatingUIResult.context),
194193
useFocus(floatingUIResult.context, {
195-
enabled: !!openOnFocus,
194+
enabled: !!openOnFocus && !readOnly,
195+
}),
196+
useKeyboard(floatingUIResult.context, {
197+
enabled: !!openOnKeyDown && !readOnly,
196198
}),
197-
useKeyboard(floatingUIResult.context, { enabled: !!openOnKeyDown }),
198199
useClick(floatingUIResult.context, {
199-
enabled: !!openOnClick,
200+
enabled: !!openOnClick && !readOnly,
200201
toggle: false,
201202
}),
202203
],

packages/lab/stories/date-picker/date-picker.stories.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ Range.args = {
177177

178178
export const SingleReadOnly = DatePickerSingleTemplate.bind({});
179179
SingleReadOnly.args = {
180-
readOnly: true
180+
readOnly: true,
181181
};
182182

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

0 commit comments

Comments
 (0)