/** Adapted from paper-time-input from https://github.com/ryanburns23/paper-time-input MIT Licensed. Copyright (c) 2017 Ryan Burns `` Polymer element to accept a time with paper-input & paper-dropdown-menu Inspired by the time input in google forms ### Styling `` provides the following custom properties and mixins for styling: Custom property | Description | Default ----------------|-------------|---------- `--paper-time-input-dropdown-ripple-color` | dropdown ripple color | `--primary-color` `--paper-time-input-cotnainer` | Mixin applied to the inputs | `{}` `--paper-time-dropdown-input-cotnainer` | Mixin applied to the dropdown input | `{}` */ import "@polymer/paper-dropdown-menu/paper-dropdown-menu"; import "@polymer/paper-input/paper-input"; import "@polymer/paper-item/paper-item"; import "@polymer/paper-listbox/paper-listbox"; import { html } from "@polymer/polymer/lib/utils/html-tag"; /* eslint-plugin-disable lit */ import { PolymerElement } from "@polymer/polymer/polymer-element"; export class PaperTimeInput extends PolymerElement { static get template() { return html`
: : : AM PM
`; } static get properties() { return { /** * Label for the input */ label: { type: String, value: "Time", }, /** * auto validate time inputs */ autoValidate: { type: Boolean, value: true, }, /** * hides the label */ hideLabel: { type: Boolean, value: false, }, /** * float the input labels */ floatInputLabels: { type: Boolean, value: false, }, /** * always float the input labels */ alwaysFloatInputLabels: { type: Boolean, value: false, }, /** * 12 or 24 hr format */ format: { type: Number, value: 12, }, /** * disables the inputs */ disabled: { type: Boolean, value: false, }, /** * hour */ hour: { type: String, notify: true, }, /** * minute */ min: { type: String, notify: true, }, /** * second */ sec: { type: String, notify: true, }, /** * milli second */ millisec: { type: String, notify: true, }, /** * Label for the hour input */ hourLabel: { type: String, value: "", }, /** * Label for the min input */ minLabel: { type: String, value: "", }, /** * Label for the sec input */ secLabel: { type: String, value: "", }, /** * Label for the milli sec input */ millisecLabel: { type: String, value: "", }, /** * show the sec field */ enableSecond: { type: Boolean, value: false, }, /** * show the milli sec field */ enableMillisecond: { type: Boolean, value: false, }, /** * limit hours input */ noHoursLimit: { type: Boolean, value: false, }, /** * AM or PM */ amPm: { type: String, notify: true, value: "AM", }, /** * Formatted time string */ value: { type: String, notify: true, readOnly: true, computed: "_computeTime(min, hour, sec, millisec, amPm)", }, }; } /** * Validate the inputs * @return {boolean} */ validate() { let valid = true; // Validate hour & min fields if (!this.$.hour.validate() || !this.$.min.validate()) { valid = false; } // Validate second field if (this.enableSecond && !this.$.sec.validate()) { valid = false; } // Validate milli second field if (this.enableMillisecond && !this.$.millisec.validate()) { valid = false; } // Validate AM PM if 12 hour time if (this.format === 12 && !this.$.dropdown.validate()) { valid = false; } return valid; } /** * Create time string */ _computeTime(min, hour, sec, millisec, amPm) { let str; if ( hour || min || (sec && this.enableSecond) || (millisec && this.enableMillisecond) ) { hour = hour || "00"; min = min || "00"; sec = sec || "00"; millisec = millisec || "000"; str = hour + ":" + min; // add sec field if (this.enableSecond && sec) { str = str + ":" + sec; } // add milli sec field if (this.enableMillisecond && millisec) { str = str + ":" + millisec; } // No ampm on 24 hr time if (this.format === 12) { str = str + " " + amPm; } } return str; } _onFocus(ev) { ev.target.inputElement.inputElement.select(); } /** * Format milli sec */ _formatMillisec() { if (this.millisec.toString().length === 1) { this.millisec = this.millisec.toString().padStart(3, "0"); } } /** * Format sec */ _formatSec() { if (this.sec.toString().length === 1) { this.sec = this.sec.toString().padStart(2, "0"); } } /** * Format min */ _formatMin() { if (this.min.toString().length === 1) { this.min = this.min.toString().padStart(2, "0"); } } /** * Format hour */ _shouldFormatHour() { if (this.format === 24 && this.hour.toString().length === 1) { this.hour = this.hour.toString().padStart(2, "0"); } } /** * 24 hour format has a max hr of 23 */ _computeHourMax(format) { if (this.noHoursLimit) { return null; } if (format === 12) { return format; } return 23; } _equal(n1, n2) { return n1 === n2; } _computeClassNames(hasSuffix) { return hasSuffix ? " " : "no-suffix"; } } customElements.define("paper-time-input", PaperTimeInput);