date-pickerpackage.src.vaadin-date-picker.js Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vaadin-webcomponents Show documentation
Show all versions of vaadin-webcomponents Show documentation
Mvnpm composite: Vaadin webcomponents
/**
* @license
* Copyright (c) 2016 - 2024 Vaadin Ltd.
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
*/
import '@vaadin/input-container/src/vaadin-input-container.js';
import './vaadin-date-picker-overlay.js';
import './vaadin-date-picker-overlay-content.js';
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
import { defineCustomElement } from '@vaadin/component-base/src/define.js';
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
import { TooltipController } from '@vaadin/component-base/src/tooltip-controller.js';
import { InputControlMixin } from '@vaadin/field-base/src/input-control-mixin.js';
import { InputController } from '@vaadin/field-base/src/input-controller.js';
import { LabelledInputController } from '@vaadin/field-base/src/labelled-input-controller.js';
import { inputFieldShared } from '@vaadin/field-base/src/styles/input-field-shared-styles.js';
import { registerStyles, ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
import { DatePickerMixin } from './vaadin-date-picker-mixin.js';
import { datePickerStyles } from './vaadin-date-picker-styles.js';
registerStyles('vaadin-date-picker', [inputFieldShared, datePickerStyles], { moduleId: 'vaadin-date-picker-styles' });
/**
* `` is an input field that allows to enter a date by typing or by selecting from a calendar overlay.
*
* ```html
*
* ```
*
* ```js
* datePicker.value = '2016-03-02';
* ```
*
* When the selected `value` is changed, a `value-changed` event is triggered.
*
* ### Styling
*
* The following custom properties are available for styling:
*
* Custom property | Description | Default
* -------------------------------|----------------------------|---------
* `--vaadin-field-default-width` | Default width of the field | `12em`
*
* `` provides the same set of shadow DOM parts and state attributes as ``.
* See [``](#/elements/vaadin-text-field) for the styling documentation.
*
* In addition to `` parts, the following parts are available for theming:
*
* Part name | Description
* ----------------------|--------------------
* `toggle-button` | Toggle button
*
* In addition to `` state attributes, the following state attributes are available for theming:
*
* Attribute | Description | Part name
* -----------|--------------------------------------------------|-----------
* `opened` | Set when the date selector overlay is opened | :host
*
* If you want to replace the default `` and its container with a custom implementation to get full control
* over the input field, consider using the [``](#/elements/vaadin-date-picker-light) element.
*
* ### Internal components
*
* In addition to `` itself, the following internal
* components are themable:
*
* - `` - has the same API as [``](#/elements/vaadin-overlay).
* - ``
* - ``
* - ``
* - ``
* - ``
* - [``](#/elements/vaadin-input-container) - an internal element wrapping the input.
*
* In order to style the overlay content, use `` shadow DOM parts:
*
* Part name | Description
* ----------------------|--------------------
* `overlay-header` | Fullscreen mode header
* `label` | Fullscreen mode value/label
* `clear-button` | Fullscreen mode clear button
* `toggle-button` | Fullscreen mode toggle button
* `years-toggle-button` | Fullscreen mode years scroller toggle
* `toolbar` | Footer bar with slotted buttons
*
* The following state attributes are available on the `` element:
*
* Attribute | Description
* ----------------|-------------------------------------------------
* `desktop` | Set when the overlay content is in desktop mode
* `fullscreen` | Set when the overlay content is in fullscreen mode
* `years-visible` | Set when the year scroller is visible in fullscreen mode
*
* In order to style the month calendar, use `` shadow DOM parts:
*
* Part name | Description
* ----------------------|--------------------
* `month-header` | Month title
* `weekdays` | Weekday container
* `weekday` | Weekday element
* `week-numbers` | Week numbers container
* `week-number` | Week number element
* `date` | Date element
* `disabled` | Disabled date element
* `focused` | Focused date element
* `selected` | Selected date element
* `today` | Date element corresponding to the current day
*
* In order to style year scroller elements, use `` shadow DOM parts:
*
* Part name | Description
* ----------------------|--------------------
* `year-number` | Year number
* `year-separator` | Year separator
*
* Note: the `theme` attribute value set on `` is
* propagated to the internal components listed above.
*
* See [Styling Components](https://vaadin.com/docs/latest/styling/styling-components) documentation.
*
* ### Change events
*
* Depending on the nature of the value change that the user attempts to commit e.g. by pressing Enter,
* the component can fire either a `change` event or an `unparsable-change` event:
*
* Value change | Event
* :------------------------|:------------------
* empty => parsable | change
* empty => unparsable | unparsable-change
* parsable => empty | change
* parsable => parsable | change
* parsable => unparsable | change
* unparsable => empty | unparsable-change
* unparsable => parsable | change
* unparsable => unparsable | unparsable-change
*
* @fires {Event} change - Fired when the user commits a value change.
* @fires {Event} unparsable-change Fired when the user commits an unparsable value change and there is no change event.
* @fires {CustomEvent} invalid-changed - Fired when the `invalid` property changes.
* @fires {CustomEvent} opened-changed - Fired when the `opened` property changes.
* @fires {CustomEvent} value-changed - Fired when the `value` property changes.
* @fires {CustomEvent} validated - Fired whenever the field is validated.
*
* @customElement
* @extends HTMLElement
* @mixes ElementMixin
* @mixes ThemableMixin
* @mixes InputControlMixin
* @mixes DatePickerMixin
*/
class DatePicker extends DatePickerMixin(InputControlMixin(ThemableMixin(ElementMixin(PolymerElement)))) {
static get is() {
return 'vaadin-date-picker';
}
static get template() {
return html`
`;
}
/**
* Used by `InputControlMixin` as a reference to the clear button element.
* @protected
* @return {!HTMLElement}
*/
get clearElement() {
return this.$.clearButton;
}
/** @protected */
ready() {
super.ready();
this.addController(
new InputController(this, (input) => {
this._setInputElement(input);
this._setFocusElement(input);
this.stateTarget = input;
this.ariaTarget = input;
}),
);
this.addController(new LabelledInputController(this.inputElement, this._labelController));
this._tooltipController = new TooltipController(this);
this.addController(this._tooltipController);
this._tooltipController.setPosition('top');
this._tooltipController.setAriaTarget(this.inputElement);
this._tooltipController.setShouldShow((target) => !target.opened);
const toggleButton = this.shadowRoot.querySelector('[part="toggle-button"]');
toggleButton.addEventListener('mousedown', (e) => e.preventDefault());
this.$.overlay.addEventListener('vaadin-overlay-close', this._onVaadinOverlayClose.bind(this));
}
/** @private */
_onVaadinOverlayClose(e) {
if (e.detail.sourceEvent && e.detail.sourceEvent.composedPath().includes(this)) {
e.preventDefault();
}
}
/** @private */
_toggle(e) {
e.stopPropagation();
if (this.$.overlay.opened) {
this.close();
} else {
this.open();
}
}
// Workaround https://github.com/vaadin/web-components/issues/2855
/** @protected */
_openedChanged(opened) {
super._openedChanged(opened);
this.$.overlay.positionTarget = this.shadowRoot.querySelector('[part="input-field"]');
this.$.overlay.noVerticalOverlap = true;
}
}
defineCustomElement(DatePicker);
export { DatePicker };