Skip to content

Commit

Permalink
fix: fix datepicker
Browse files Browse the repository at this point in the history
  • Loading branch information
jeripeierSBB committed Nov 27, 2024
1 parent def6fe4 commit 0e78bea
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/elements/datepicker/datepicker/datepicker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,15 @@ describe(`sbb-datepicker`, () => {

it('renders and emit event on value change', async () => {
const changeSpy = new EventSpy('change', element);
const inputSpy = new EventSpy('input', element);
typeInElement(input, '20/01/2023');
expect(inputSpy.count).to.be.equal(10);

button.focus();
await changeSpy.calledOnce();
expect(input.value).to.be.equal('Fr, 20.01.2023');
expect(changeSpy.count).to.be.equal(1);
expect(inputSpy.count).to.be.equal(11);
});

it('renders and interpret two digit year correctly in 2000s', async () => {
Expand Down
18 changes: 15 additions & 3 deletions src/elements/datepicker/datepicker/datepicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { SbbConnectedAbortController, SbbLanguageController } from '../../core/c
import { type DateAdapter, defaultDateAdapter } from '../../core/datetime.js';
import { forceType } from '../../core/decorators.js';
import { findInput, findReferencedElement } from '../../core/dom.js';
import { EventEmitter } from '../../core/eventing.js';
import { EventEmitter, forwardEventToHost } from '../../core/eventing.js';
import { i18nDateChangedTo, i18nDatePickerPlaceholder } from '../../core/i18n.js';
import type { SbbDateLike, SbbValidationChangeEvent } from '../../core/interfaces.js';
import type { SbbDatepickerButton } from '../common.js';
Expand Down Expand Up @@ -57,6 +57,7 @@ export const datepickerControlRegisteredEventFactory = (): CustomEvent =>
* Combined with a native input, it displays the input's value as a formatted date.
*
* @event {CustomEvent<void>} change - Notifies that the connected input has changes.
* @event {CustomEvent<void>} input - Notifies that the connected input fired the input event.
* @event {CustomEvent<SbbInputUpdateEvent>} inputUpdated - Notifies that the attributes of the input connected to the datepicker have changes.
* @event {CustomEvent<void>} datePickerUpdated - Notifies that the attributes of the datepicker have changes.
* @event {CustomEvent<SbbValidationChangeEvent>} validationChange - Emits whenever the internal validation state changes.
Expand Down Expand Up @@ -239,7 +240,14 @@ class SbbDatepickerElement<T = Date> extends LitElement {
}

const options: AddEventListenerOptions = { signal: this._datePickerController.signal };
input.addEventListener('input', () => this._parseInput(), options);
input.addEventListener(
'input',
(e) => {
forwardEventToHost(e, this);
this._parseInput();
},
options,
);
input.addEventListener('change', () => this._handleInputChange(), options);
this._parseInput(true);
this._tryApplyFormatToInput();
Expand Down Expand Up @@ -268,7 +276,11 @@ class SbbDatepickerElement<T = Date> extends LitElement {

const formattedDate = this.valueAsDate ? this._dateAdapter.format(this.valueAsDate!) : '';
if (formattedDate && this._inputElement.value !== formattedDate) {
this._inputElement.value = formattedDate;
// In order to support React onChange event, we have to get the setter and call it.
// https://github.com/facebook/react/issues/11600#issuecomment-345813130
const setValue = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value')!.set!;
setValue.call(this._inputElement, formattedDate);

this._inputElement.dispatchEvent(new InputEvent('input', { bubbles: true, composed: true }));
this._inputElement.dispatchEvent(new Event('change', { bubbles: true, composed: true }));
return true;
Expand Down
1 change: 1 addition & 0 deletions src/elements/datepicker/datepicker/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,6 @@ Whenever the validation state changes (e.g., a valid value becomes invalid or vi
| ------------------- | --------------------------------------- | ----------------------------------------------------------------------------------- | -------------- |
| `change` | `CustomEvent<void>` | Notifies that the connected input has changes. | |
| `datePickerUpdated` | `CustomEvent<void>` | Notifies that the attributes of the datepicker have changes. | |
| `input` | `CustomEvent<void>` | Notifies that the connected input fired the input event. | |
| `inputUpdated` | `CustomEvent<SbbInputUpdateEvent>` | Notifies that the attributes of the input connected to the datepicker have changes. | |
| `validationChange` | `CustomEvent<SbbValidationChangeEvent>` | Emits whenever the internal validation state changes. | |

0 comments on commit 0e78bea

Please sign in to comment.