100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
UI updates: or-select now supports value display string tuples
Rich Turnera0202d08aea4
diff --git a/ui/component/or-select/src/index.ts b/ui/component/or-select/src/index.tsindex 74551a297..b65a47b90 100644--- a/ui/component/or-select/src/index.ts+++ b/ui/component/or-select/src/index.ts@@ -1,25 +1,37 @@-import {html, LitElement, property, PropertyValues} from 'lit-element';-import openremote from "@openremote/core";--import {MDCSelect} from '@material/select';+import {html, LitElement, property, customElement, PropertyValues, query} from "lit-element";import i18next from "i18next";-import {selectStyle} from './style';--class OrSelect extends LitElement {- @property({type: String})- label: string = '';-- @property({type: String})- name: string = '';+import {selectStyle} from "./style";+import {translate} from "@openremote/or-translate/dist/translate-mixin";++export class OrSelectChangedEvent extends CustomEvent<OrSelectChangedEventDetail> {++ public static readonly NAME = "or-select-changed";++ constructor(value?: string, previousValue?: string) {+ super(OrSelectChangedEvent.NAME, {+ detail: {+ value: value,+ previousValue: previousValue+ },+ bubbles: true,+ composed: true+ });+ }+}- @property({type: String})- value: string = '';+export interface OrSelectChangedEventDetail {+ value?: string;+ previousValue?: string;+}- @property({type: Boolean})- public disabled: boolean = false;+declare global {+ export interface HTMLElementEventMap {+ [OrSelectChangedEvent.NAME]: OrSelectChangedEvent;+ }+}- @property({type: Array})- options: string[] = [];+@customElement("or-select")+export class OrSelect extends translate(i18next)(LitElement) {static get styles() {return [@@ -27,46 +39,68 @@ class OrSelect extends LitElement {];}- protected render() {+ @property({type: String})+ public value?: string;- return html`- <div class="mdc-select">- <select id="or-select" @change="${this.onChange}" name="${this.name}" ?disabled="${this.disabled}">- ${this.options.map((option: string) => {- return html`<option ?selected=${this.value === option} value="${option}">${i18next.t(option || "")}</option>`- })}- </select>- </div>- `;- }+ @property({type: Boolean})+ public readonly: boolean = false;- protected firstUpdated(_changedProperties: PropertyValues): void {- super.firstUpdated(_changedProperties);- this.disabled = !openremote.hasRole("write:assets");- }+ @property({type: Boolean})+ public required: boolean = true;++ @property({type: Boolean})+ public addEmpty: boolean = true;- onChange() {- if(this.shadowRoot){- const input = (<HTMLInputElement>this.shadowRoot.getElementById('or-select'));- const value = input.value;- const name = input.name;+ @property({type: Array})+ public options: string[] | [string, string][] = [];- // Launch event for all parent elements- let event = new CustomEvent('or-input:changed', {- detail: { value: value, name: name },- bubbles: true,- composed: true });+ @query("#or-select")+ protected _select!: HTMLSelectElement;- this.dispatchEvent(event);- }+ protected onChange() {+ const previousValue = this.value;+ this.value = this._select.value && this._select.value.length > 0 ? this._select.value : undefined;+ this.dispatchEvent(new OrSelectChangedEvent(this.value, previousValue));}-- constructor() {- super();+ protected firstUpdated(_changedProperties: PropertyValues) {+ super.firstUpdated(_changedProperties);+ if (this.options.length === 1) {+ const opt = this.options[0];+ const val = Array.isArray(opt) ? opt[0] : opt;+ if (this.value !== val) {+ window.setTimeout(() => {+ if (this._select) {+ this._select.value = val;+ }+ });+ }+ }}+ protected render() {+ if (this._select) {+ // If we don't do this then selected attribute on option is not respected when a select is updated+ if (!this.value) {+ this._select.selectedIndex = this.addEmpty ? 0 : -1;+ }+ }+ const isSingular = this.options.length === 1;+ return html`+ <div class="mdc-select">+ <select id="or-select" ?required="${this.required}" @change="${this.onChange}" ?disabled="${this.readonly || isSingular}">+ ${this.addEmpty && !isSingular ? html`<option value="" ?selected="${!this.value}"></option>` : ``};+ ${this.options.length > 0 && Array.isArray(this.options[0]) ?+ (this.options as [string, string][]).map((option: [string, string]) => {+ return html`<option ?selected="${this.value === option[0]}" value="${option[0]}">${option[1]}</option>`;+ }) :+ (this.options as string[]).map((option: string) => {+ return html`<option ?selected="${this.value === option}" value="${option}">${option}</option>`;+ })+ }+ </select>+ </div>+ `;+ }}--window.customElements.define('or-select', OrSelect);diff --git a/ui/component/or-select/src/style.ts b/ui/component/or-select/src/style.tsindex aed02a22d..caac90ace 100644--- a/ui/component/or-select/src/style.ts+++ b/ui/component/or-select/src/style.ts@@ -3,10 +3,16 @@ import {css} from "lit-element";// TODO this is temp work, should be replaced with material design components / styling// language=CSSexport const selectStyle = css`++ select:invalid {+ border-bottom: 2px solid red;+ }+.mdc-select {font-family:'Roboto','Helvetica','Arial',sans-serif;position: relative;+ display: inline-block;}select {