|
| 1 | +import { html, css, LitElement, DisabledMixin } from '@lion/core'; |
| 2 | +import { FormRegisteringMixin } from '@lion/field'; |
| 3 | +import { ChoiceInputMixin } from '@lion/choice-input'; |
| 4 | + |
| 5 | +/** |
| 6 | + * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/option |
| 7 | + * Can be a child of datalist/select, or role="listbox" |
| 8 | + * |
| 9 | + * Element gets state supplied externally, reflects this to attributes, |
| 10 | + * enabling SubClassers to style based on those states |
| 11 | + */ |
| 12 | +export class LionOption extends DisabledMixin(ChoiceInputMixin(FormRegisteringMixin(LitElement))) { |
| 13 | + static get properties() { |
| 14 | + return { |
| 15 | + active: { |
| 16 | + type: Boolean, |
| 17 | + reflect: true, |
| 18 | + }, |
| 19 | + }; |
| 20 | + } |
| 21 | + |
| 22 | + static get styles() { |
| 23 | + return [ |
| 24 | + css` |
| 25 | + :host { |
| 26 | + display: block; |
| 27 | + background-color: white; |
| 28 | + padding: 4px; |
| 29 | + } |
| 30 | +
|
| 31 | + :host([active]) { |
| 32 | + background-color: #ddd; |
| 33 | + } |
| 34 | +
|
| 35 | + :host([checked]) { |
| 36 | + background-color: #bde4ff; |
| 37 | + } |
| 38 | +
|
| 39 | + :host([disabled]) { |
| 40 | + color: #adadad; |
| 41 | + } |
| 42 | + `, |
| 43 | + ]; |
| 44 | + } |
| 45 | + |
| 46 | + constructor() { |
| 47 | + super(); |
| 48 | + this.active = false; |
| 49 | + this.__registerEventListener(); |
| 50 | + } |
| 51 | + |
| 52 | + _requestUpdate(name, oldValue) { |
| 53 | + super._requestUpdate(name, oldValue); |
| 54 | + |
| 55 | + if (name === 'active') { |
| 56 | + this.dispatchEvent(new Event('active-changed', { bubbles: true })); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + updated(changedProperties) { |
| 61 | + super.updated(changedProperties); |
| 62 | + if (changedProperties.has('checked')) { |
| 63 | + this.setAttribute('aria-selected', `${this.checked}`); |
| 64 | + } |
| 65 | + |
| 66 | + if (changedProperties.has('disabled')) { |
| 67 | + this.setAttribute('aria-disabled', `${this.disabled}`); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + render() { |
| 72 | + return html` |
| 73 | + <div class="choice-field__label"> |
| 74 | + <slot></slot> |
| 75 | + </div> |
| 76 | + `; |
| 77 | + } |
| 78 | + |
| 79 | + connectedCallback() { |
| 80 | + super.connectedCallback(); |
| 81 | + this.setAttribute('role', 'option'); |
| 82 | + } |
| 83 | + |
| 84 | + disconnectedCallback() { |
| 85 | + super.disconnectedCallback(); |
| 86 | + this.__unRegisterEventListeners(); |
| 87 | + } |
| 88 | + |
| 89 | + __registerEventListener() { |
| 90 | + this.__onClick = () => { |
| 91 | + if (!this.disabled) { |
| 92 | + this.checked = true; |
| 93 | + } |
| 94 | + }; |
| 95 | + this.__onMouseEnter = () => { |
| 96 | + if (!this.disabled) { |
| 97 | + this.active = true; |
| 98 | + } |
| 99 | + }; |
| 100 | + this.__onMouseLeave = () => { |
| 101 | + if (!this.disabled) { |
| 102 | + this.active = false; |
| 103 | + } |
| 104 | + }; |
| 105 | + this.addEventListener('click', this.__onClick); |
| 106 | + this.addEventListener('mouseenter', this.__onMouseEnter); |
| 107 | + this.addEventListener('mouseleave', this.__onMouseLeave); |
| 108 | + } |
| 109 | + |
| 110 | + __unRegisterEventListeners() { |
| 111 | + this.removeEventListener('click', this.__onClick); |
| 112 | + this.removeEventListener('mouseenter', this.__onMouseEnter); |
| 113 | + this.removeEventListener('mouseleave', this.__onMouseLeave); |
| 114 | + } |
| 115 | +} |
0 commit comments