Skip to content

Commit f69a6f4

Browse files
committed
add select method
1 parent fe63064 commit f69a6f4

2 files changed

Lines changed: 79 additions & 1 deletion

File tree

src/components/date-range-picker/date-range-picker.spec.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,62 @@ describe('Date range picker', () => {
515515
expect(separator?.innerText).to.equal('Separator - localized');
516516
});
517517
});
518+
describe('Methods', () => {
519+
it('should open/close the picker on invoking show/hide/toggle and not emit events', async () => {
520+
const eventSpy = spy(picker, 'emitEvent');
521+
522+
expect(picker.open).to.be.false;
523+
await picker.show();
524+
525+
expect(eventSpy).not.called;
526+
expect(picker.open).to.be.true;
527+
528+
await picker.hide();
529+
530+
expect(eventSpy).not.called;
531+
expect(picker.open).to.be.false;
532+
533+
await picker.toggle();
534+
535+
expect(eventSpy).not.called;
536+
expect(picker.open).to.be.true;
537+
538+
await picker.toggle();
539+
540+
expect(eventSpy).not.called;
541+
expect(picker.open).to.be.false;
542+
});
543+
it('should clear the input on invoking clear()', async () => {
544+
const eventSpy = spy(picker, 'emitEvent');
545+
picker.value = { start: today.native, end: tomorrow.native };
546+
await elementUpdated(picker);
547+
548+
expect(dateTimeInputs[0].value).to.equal(picker.value.start);
549+
expect(dateTimeInputs[1].value).to.equal(picker.value.end);
550+
picker.clear();
551+
await elementUpdated(picker);
552+
553+
expect(eventSpy).not.called;
554+
expect(picker.value).to.deep.equal({ start: null, end: null });
555+
expect(dateTimeInputs[0].value).to.be.null;
556+
expect(dateTimeInputs[1].value).to.be.null;
557+
});
558+
it('should select a date range on invoking select', async () => {
559+
const eventSpy = spy(picker, 'emitEvent');
560+
expect(picker.value).to.deep.equal({ start: null, end: null });
561+
562+
picker.select({ start: today.native, end: tomorrow.native });
563+
await elementUpdated(picker);
564+
565+
expect(picker.value).to.deep.equal({
566+
start: today.native,
567+
end: tomorrow.native,
568+
});
569+
expect(dateTimeInputs[0].value).to.equal(picker.value?.start);
570+
expect(dateTimeInputs[1].value).to.equal(picker.value?.end);
571+
expect(eventSpy).not.called;
572+
});
573+
});
518574
describe('Interactions', () => {
519575
describe('Selection via the calendar', () => {
520576
it('should select a single date in dropdown mode and emit igcChange', async () => {
@@ -549,6 +605,9 @@ describe('Date range picker', () => {
549605
expect(popover?.hasAttribute('open')).to.equal(false);
550606
});
551607

608+
// TODO
609+
it('should swap the selected dates if the end is earlier than the start', async () => {});
610+
552611
it('should select a range of dates in dialog mode and emit igcChange when done is clicked', async () => {
553612
const eventSpy = spy(picker, 'emitEvent');
554613

@@ -818,6 +877,13 @@ describe('Date range picker', () => {
818877
expect(picker.open).to.be.false;
819878
picker.value = { start: null, end: null };
820879
});
880+
881+
// TODO
882+
it('should emit igcInput and igcChange on input value change', async () => {});
883+
884+
it('should not swap the dates while typing', async () => {});
885+
886+
it('should set the calendar active date to the start of the range while typing', async () => {});
821887
});
822888
});
823889
describe('Slots', () => {

src/components/date-range-picker/date-range-picker.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ export default class IgcDateRangePickerComponent extends FormAssociatedRequiredM
545545
@property()
546546
public locale = 'en';
547547

548-
/** The resource strings of the calendar. */
548+
/** The resource strings of the date range picker. */
549549
@property({ attribute: false })
550550
public resourceStrings: IgcCalendarResourceStrings &
551551
IgcDateRangePickerResourceStrings = {
@@ -886,6 +886,18 @@ export default class IgcDateRangePickerComponent extends FormAssociatedRequiredM
886886
this._inputs[1]?.clear();
887887
}
888888

889+
/** Selects a date range value in the picker */
890+
public select(value: DateRangeValue | null) {
891+
this._select(value);
892+
}
893+
894+
private _select(value: DateRangeValue | null, emitEvent = false) {
895+
this.value = value;
896+
if (emitEvent) {
897+
this.emitEvent('igcChange', { detail: this.value });
898+
}
899+
}
900+
889901
private renderClearIcon(picker: 'start' | 'end' = 'start') {
890902
const clearIcon = !this.useTwoInputs
891903
? 'clear-icon'

0 commit comments

Comments
 (0)