|
| 1 | +import { HXElement } from './HXElement'; |
| 2 | + |
| 3 | +const STATE = { |
| 4 | + changed: 'hx-changed', |
| 5 | + touched: 'hx-touched', |
| 6 | +}; |
| 7 | + |
| 8 | +/** |
| 9 | + * Abstract class which defines shared behavior among all |
| 10 | + * form control custom elements (e.g., HXSelectControlElement, |
| 11 | + * HXCheckboxControlElement, etc.). |
| 12 | + * |
| 13 | + * ## States |
| 14 | + * States are applied as events occur on the `controlElement`. |
| 15 | + * |
| 16 | + * ### Changed |
| 17 | + * Applies the `hx-changed` content attribute when controlElement |
| 18 | + * emits a `change` event. This typically occurs after the value |
| 19 | + * has been modified and the user moves away (blurs) the text control. |
| 20 | + * |
| 21 | + * ### Touched |
| 22 | + * Applies the `hx-touched` content attribute when controlElement |
| 23 | + * emits a `blur` event (meaning that the user has "visited" the |
| 24 | + * text control and moved on). |
| 25 | + * |
| 26 | + * @abstract |
| 27 | + * @hideconstructor |
| 28 | + * @since 0.16.0 |
| 29 | + */ |
| 30 | +export class HXFormControlElement extends HXElement { |
| 31 | + /** @override */ |
| 32 | + constructor () { |
| 33 | + super(); |
| 34 | + |
| 35 | + this._onCtrlBlur = this._onCtrlBlur.bind(this); |
| 36 | + this._onCtrlChange = this._onCtrlChange.bind(this); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Adds `change` and `blur` event listeners to apply |
| 41 | + * "changed" and "touched" states to the custom control |
| 42 | + * element, in addition to superclass behavior. |
| 43 | + * |
| 44 | + * - preserves `$onConnect()` hook for subclasses |
| 45 | + * |
| 46 | + * @override |
| 47 | + */ |
| 48 | + connectedCallback () { |
| 49 | + super.connectedCallback(); |
| 50 | + |
| 51 | + let ctrl = this.controlElement; |
| 52 | + if (ctrl) { |
| 53 | + ctrl.addEventListener('change', this._onCtrlChange); |
| 54 | + ctrl.addEventListener('blur', this._onCtrlBlur); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Removes event listeners added in connectedCallback, |
| 60 | + * in addition to superclass behavior. |
| 61 | + * |
| 62 | + * - preserves `$onDisconnect()` hook for subclasses |
| 63 | + * |
| 64 | + * @override |
| 65 | + */ |
| 66 | + disconnectedCallback () { |
| 67 | + super.disconnectedCallback(); |
| 68 | + |
| 69 | + let ctrl = this.controlElement; |
| 70 | + if (ctrl) { |
| 71 | + ctrl.removeEventListener('change', this._onCtrlChange); |
| 72 | + ctrl.removeEventListener('blur', this._onCtrlBlur); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * This should be overridden by subclasses. |
| 78 | + * |
| 79 | + * Logic should make a best effort to return an HTML |
| 80 | + * form control element (e.g., `<input>`, `<select>`, |
| 81 | + * `<textarea>`, etc.). |
| 82 | + * |
| 83 | + * @abstract |
| 84 | + * @default undefined |
| 85 | + * @type {?HTMLElement} |
| 86 | + */ |
| 87 | + get controlElement () {} |
| 88 | + |
| 89 | + /** |
| 90 | + * @readonly |
| 91 | + * @type {Boolean} [false] |
| 92 | + */ |
| 93 | + get wasChanged () { |
| 94 | + return this.hasAttribute(STATE.changed); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @readonly |
| 99 | + * @type {Boolean} [false] |
| 100 | + */ |
| 101 | + get wasTouched () { |
| 102 | + return this.hasAttribute(STATE.touched); |
| 103 | + } |
| 104 | + |
| 105 | + /** @private */ |
| 106 | + _onCtrlBlur () { |
| 107 | + // communicate state via read-only, boolean content attribute |
| 108 | + this.$defaultAttribute(STATE.touched, ''); |
| 109 | + } |
| 110 | + |
| 111 | + /** @private */ |
| 112 | + _onCtrlChange () { |
| 113 | + // communicate state via read-only, boolean content attribute |
| 114 | + this.$defaultAttribute(STATE.changed, ''); |
| 115 | + } |
| 116 | +} |
0 commit comments