|
| 1 | +import type { ReactiveController, ReactiveControllerHost } from "lit"; |
| 2 | +import type { DirectiveResult } from "lit/async-directive"; |
| 3 | +import { live, type LiveDirective } from "lit/directives/live.js"; |
| 4 | +import kebabToCamel from "../kebab-to-camel"; |
| 5 | +import SystemError from "../SystemError"; |
| 6 | + |
| 7 | +class ControlledPropController< |
| 8 | + T, |
| 9 | + H extends ReactiveControllerHost = ReactiveControllerHost, |
| 10 | +> implements ReactiveController |
| 11 | +{ |
| 12 | + private _host: ReactiveControllerHost; |
| 13 | + |
| 14 | + private _isControlled: boolean = false; |
| 15 | + private _propKey: PropertyKey; |
| 16 | + private _controlBehaviorPropKey: PropertyKey; |
| 17 | + |
| 18 | + constructor(host: H, propKey: keyof H, controlBehaviorPropKey?: string) { |
| 19 | + host.addController(this); |
| 20 | + |
| 21 | + this._host = host; |
| 22 | + this._propKey = propKey; |
| 23 | + |
| 24 | + if (typeof controlBehaviorPropKey === "undefined") { |
| 25 | + let key: PropertyKey; |
| 26 | + |
| 27 | + if (typeof propKey === "symbol") { |
| 28 | + key = Symbol(kebabToCamel(`controlled-${propKey.description}`)); |
| 29 | + } else { |
| 30 | + key = kebabToCamel(`controlled-${String(propKey)}`); |
| 31 | + } |
| 32 | + |
| 33 | + this._controlBehaviorPropKey = key; |
| 34 | + } else this._controlBehaviorPropKey = controlBehaviorPropKey; |
| 35 | + } |
| 36 | + |
| 37 | + private _getPropDescriptor(propKey: PropertyKey): PropertyDescriptor { |
| 38 | + const proto = Object.getPrototypeOf(this._host) as object; |
| 39 | + const prop = Object.getOwnPropertyDescriptor(proto, propKey); |
| 40 | + |
| 41 | + if (!prop) { |
| 42 | + throw new SystemError( |
| 43 | + [ |
| 44 | + `The required member \`${String(propKey)}\` is not present in the prototype.`, |
| 45 | + "Please ensure it is included for correct functionality.", |
| 46 | + ].join("\n"), |
| 47 | + `${proto.constructor.name}`, |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + return prop; |
| 52 | + } |
| 53 | + |
| 54 | + private _getControlledProp(): PropertyDescriptor { |
| 55 | + return this._getPropDescriptor(this._propKey); |
| 56 | + } |
| 57 | + |
| 58 | + private _getBehaviorProp(): PropertyDescriptor { |
| 59 | + return this._getPropDescriptor(this._controlBehaviorPropKey); |
| 60 | + } |
| 61 | + |
| 62 | + private _setProp(newValue: T): void { |
| 63 | + const prop = this._getControlledProp(); |
| 64 | + |
| 65 | + prop.set?.call(this._host, newValue); |
| 66 | + } |
| 67 | + |
| 68 | + public get isControlled(): boolean { |
| 69 | + return this._isControlled; |
| 70 | + } |
| 71 | + |
| 72 | + public get value(): T { |
| 73 | + return this._getControlledProp().get?.call(this._host) as T; |
| 74 | + } |
| 75 | + |
| 76 | + public get liveInputBinding(): DirectiveResult<typeof LiveDirective> { |
| 77 | + return live(this.value); |
| 78 | + } |
| 79 | + |
| 80 | + public set value(newValue: T) { |
| 81 | + if (this._isControlled) { |
| 82 | + this._host.requestUpdate(); |
| 83 | + |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + this._setProp(newValue); |
| 88 | + } |
| 89 | + |
| 90 | + hostConnected(): void { |
| 91 | + const behaviorProp = this._getBehaviorProp(); |
| 92 | + |
| 93 | + this._isControlled = Boolean(behaviorProp.get?.call(this._host)); |
| 94 | + } |
| 95 | + |
| 96 | + hostDisconnected(): void {} |
| 97 | +} |
| 98 | + |
| 99 | +export default ControlledPropController; |
0 commit comments