Skip to content

Commit 6a9639b

Browse files
Sarin-UdompanishSarin Udompanish
andauthored
feat(datetime-picker): introduce preventCloseOnSelect to prevents the Picker from closing on date selection (#1277)
* feat(datetime-picker): introduce preventCloseOnSelect to prevents the Picker from closing on date selection * test(datetime-pickier): add preventCloseOnSelect test case * chore(datetime-picker): update property description * chore(datetime-picker): remove demo --------- Co-authored-by: Sarin Udompanish <[email protected]>
1 parent 2d47dd0 commit 6a9639b

File tree

3 files changed

+90
-2
lines changed

3 files changed

+90
-2
lines changed

packages/elements/src/datetime-picker/__demo__/index.html

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@
174174
<ef-checkbox id="inputTriggerDisabled">Input Trigger Disabled</ef-checkbox>
175175
<ef-checkbox id="inputDisabled">Input Disabled</ef-checkbox>
176176
<ef-checkbox id="popupDisabled">Popup Disabled</ef-checkbox>
177+
<ef-checkbox id="preventCloseOnSelect">Prevent Close On Select</ef-checkbox>
177178
</p>
178179
</div>
179180
</div>
@@ -209,7 +210,8 @@
209210
weekendsOnly: dateTimePicker.weekendsOnly,
210211
inputTriggerDisabled: dateTimePicker.inputTriggerDisabled,
211212
inputDisabled: dateTimePicker.inputDisabled,
212-
popupDisabled: dateTimePicker.popupDisabled
213+
popupDisabled: dateTimePicker.popupDisabled,
214+
preventCloseOnSelect: dateTimePicker.preventCloseOnSelect
213215
};
214216

215217
consoleEl.value = JSON.stringify(data, undefined, 4);
@@ -399,6 +401,12 @@
399401
dateTimePicker.popupDisabled = value || undefined;
400402
});
401403

404+
document
405+
.getElementById('preventCloseOnSelect')
406+
.addEventListener('checked-changed', ({ detail: { value } }) => {
407+
dateTimePicker.preventCloseOnSelect = value || undefined;
408+
});
409+
402410
const eventLog = [];
403411
const onEvent = (event) => {
404412
eventLog.unshift(`${event.type}: ${JSON.stringify(event.detail)}`);

packages/elements/src/datetime-picker/__test__/datetime-picker.navigation.test.js

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import '@refinitiv-ui/elements/datetime-picker';
33

44
import '@refinitiv-ui/halo-theme/light/ef-datetime-picker.js';
5-
import { elementUpdated, expect, fixture, oneEvent } from '@refinitiv-ui/test-helpers';
5+
import { elementUpdated, expect, fixture, nextFrame, oneEvent } from '@refinitiv-ui/test-helpers';
66

77
import { fireKeydownEvent } from './utils.js';
88

@@ -77,5 +77,75 @@ describe('datetime-picker/Navigation', function () {
7777
await elementUpdated(el);
7878
expect(el.opened).to.be.equal(false, 'Setting disabled should close calendar');
7979
});
80+
it('Should not close calendar when preventCloseOnSelect is true in single mode', async function () {
81+
const el = await fixture(
82+
'<ef-datetime-picker lang="en-gb" opened prevent-close-on-select></ef-datetime-picker>'
83+
);
84+
await elementUpdated(el);
85+
86+
const calendarEl = el.calendarEl;
87+
const cell = calendarEl.shadowRoot.querySelector('div[tabindex]'); // Select a date cell
88+
cell.click();
89+
await elementUpdated(el);
90+
91+
expect(el.opened).to.be.equal(true, 'Calendar should remain open when preventCloseOnSelect is true');
92+
});
93+
it('Should not close calendar when preventCloseOnSelect is true in range mode', async function () {
94+
const el = await fixture(
95+
'<ef-datetime-picker lang="en-gb" opened range duplex prevent-close-on-select></ef-datetime-picker>'
96+
);
97+
el.views = ['2020-04', '2020-05'];
98+
await elementUpdated(el);
99+
await nextFrame();
100+
await nextFrame();
101+
102+
const calendarFromEl = el.calendarFromEl;
103+
const fromCell = calendarFromEl.shadowRoot.querySelector('div[tabindex]'); // Select a date in the "from" calendar
104+
fromCell.click();
105+
await elementUpdated(el);
106+
await nextFrame();
107+
108+
const calendarToEl = el.calendarToEl;
109+
const toCell = calendarToEl.shadowRoot.querySelector('div[tabindex]'); // Select a date in the "to" calendar
110+
toCell.click();
111+
await elementUpdated(el);
112+
await nextFrame();
113+
114+
expect(el.opened).to.be.equal(
115+
true,
116+
'Calendar should remain open when preventCloseOnSelect is true in range mode'
117+
);
118+
});
119+
it('Should not close calendar when preventCloseOnSelect is true with timepicker', async function () {
120+
const el = await fixture(
121+
'<ef-datetime-picker lang="en-gb" opened timepicker prevent-close-on-select></ef-datetime-picker>'
122+
);
123+
await elementUpdated(el);
124+
125+
const calendarEl = el.calendarEl;
126+
const cell = calendarEl.shadowRoot.querySelector('div[tabindex]'); // Select a date cell
127+
cell.click();
128+
await elementUpdated(el);
129+
130+
const timepickerEl = el.timepickerEl;
131+
timepickerEl.value = '12:30'; // Simulate time selection
132+
await elementUpdated(el);
133+
134+
expect(el.opened).to.be.equal(
135+
true,
136+
'Calendar should remain open when preventCloseOnSelect is true with timepicker'
137+
);
138+
});
139+
it('Should close calendar when preventCloseOnSelect is false', async function () {
140+
const el = await fixture('<ef-datetime-picker lang="en-gb" opened></ef-datetime-picker>');
141+
await elementUpdated(el);
142+
143+
const calendarEl = el.calendarEl;
144+
const cell = calendarEl.shadowRoot.querySelector('div[tabindex]'); // Select a date cell
145+
cell.click();
146+
await elementUpdated(el);
147+
148+
expect(el.opened).to.be.equal(false, 'Calendar should close when preventCloseOnSelect is false');
149+
});
80150
});
81151
});

packages/elements/src/datetime-picker/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,13 @@ export class DatetimePicker extends FormFieldElement implements MultiValue {
343343
@property({ type: Boolean, attribute: 'input-trigger-disabled' })
344344
public inputTriggerDisabled = false;
345345

346+
/**
347+
* Prevents the Picker from closing on date selection.
348+
* Note that if timepicker is true, the picker will remain open regardless of this flag.
349+
*/
350+
@property({ type: Boolean, attribute: 'prevent-close-on-select' })
351+
public preventCloseOnSelect = false;
352+
346353
/**
347354
* Disable input part of the picker
348355
*/
@@ -994,6 +1001,9 @@ export class DatetimePicker extends FormFieldElement implements MultiValue {
9941001
// Close popup if there is no time picker
9951002
const newValues = this.values;
9961003
if (!this.timepicker && newValues[0] && (this.range ? newValues[1] : true)) {
1004+
if (this.preventCloseOnSelect) {
1005+
return;
1006+
}
9971007
this.setOpened(false);
9981008

9991009
/**

0 commit comments

Comments
 (0)