|
| 1 | +import type {} from 'element-internals-polyfill'; |
| 2 | +import { BaseFieldProps } from 'web-utility'; |
| 3 | + |
| 4 | +import { WebCellProps } from './utility'; |
| 5 | +import { mixin, WebCellClass, WebCellComponent } from './WebCell'; |
| 6 | +import { watch, attribute } from './decorator'; |
| 7 | + |
| 8 | +export interface WebFieldProps extends BaseFieldProps, WebCellProps {} |
| 9 | + |
| 10 | +export interface WebFieldComponent< |
| 11 | + P extends WebFieldProps = WebFieldProps, |
| 12 | + S = {} |
| 13 | +> extends WebCellComponent<P, S> { |
| 14 | + /** |
| 15 | + * Called when the browser associates the element with a form element, |
| 16 | + * or disassociates the element from a form element. |
| 17 | + */ |
| 18 | + formAssociatedCallback?(form: HTMLFormElement): void; |
| 19 | + /** |
| 20 | + * Called after the disabled state of the element changes, |
| 21 | + * either because the disabled attribute of this element was added or removed; |
| 22 | + * or because the disabled state changed on a `<fieldset>` that's an ancestor of this element. |
| 23 | + * |
| 24 | + * @param disabled This parameter represents the new disabled state of the element. |
| 25 | + */ |
| 26 | + formDisabledCallback?(disabled: boolean): void; |
| 27 | + /** |
| 28 | + * Called after the form is reset. |
| 29 | + * The element should reset itself to some kind of default state. |
| 30 | + */ |
| 31 | + formResetCallback?(): void; |
| 32 | + /** |
| 33 | + * Called in one of two circumstances: |
| 34 | + * - When the browser restores the state of the element (for example, after a navigation, or when the browser restarts). The `mode` argument is `"restore"` in this case. |
| 35 | + * - When the browser's input-assist features such as form autofilling sets a value. The `mode` argument is `"autocomplete"` in this case. |
| 36 | + * |
| 37 | + * @param state The type of this argument depends on how the `this.internals.setFormValue()` method was called. |
| 38 | + * @param mode |
| 39 | + */ |
| 40 | + formStateRestoreCallback?( |
| 41 | + state: string | File | FormData, |
| 42 | + mode: 'restore' | 'autocomplete' |
| 43 | + ): void; |
| 44 | +} |
| 45 | + |
| 46 | +export interface WebFieldClass<P extends WebFieldProps = WebFieldProps, S = {}> |
| 47 | + extends WebCellClass<P, S> { |
| 48 | + new (options?: ShadowRootInit): WebFieldComponent<P, S>; |
| 49 | +} |
| 50 | + |
| 51 | +export function mixinForm<P extends WebFieldProps = WebFieldProps, S = {}>( |
| 52 | + superClass = HTMLElement |
| 53 | +): WebFieldClass<P, S> { |
| 54 | + class WebField |
| 55 | + extends mixin<P, S>(superClass) |
| 56 | + implements WebFieldComponent<P, S> { |
| 57 | + static formAssociated = true; |
| 58 | + |
| 59 | + @attribute |
| 60 | + @watch |
| 61 | + name: string; |
| 62 | + |
| 63 | + @watch |
| 64 | + value: string; |
| 65 | + |
| 66 | + @attribute |
| 67 | + @watch |
| 68 | + required: boolean; |
| 69 | + |
| 70 | + @attribute |
| 71 | + @watch |
| 72 | + disabled: boolean; |
| 73 | + |
| 74 | + @attribute |
| 75 | + @watch |
| 76 | + placeholder: string; |
| 77 | + |
| 78 | + @attribute |
| 79 | + @watch |
| 80 | + autofocus: boolean; |
| 81 | + |
| 82 | + set defaultValue(raw: string) { |
| 83 | + this.setAttribute('value', raw); |
| 84 | + |
| 85 | + this.props.value ?? (this.value = raw); |
| 86 | + } |
| 87 | + |
| 88 | + get defaultValue() { |
| 89 | + return this.getAttribute('value'); |
| 90 | + } |
| 91 | + |
| 92 | + protected internals = this.attachInternals(); |
| 93 | + |
| 94 | + get form() { |
| 95 | + return this.internals.form; |
| 96 | + } |
| 97 | + get validity() { |
| 98 | + return this.internals.validity; |
| 99 | + } |
| 100 | + get validationMessage() { |
| 101 | + return this.internals.validationMessage; |
| 102 | + } |
| 103 | + get willValidate() { |
| 104 | + return this.internals.willValidate; |
| 105 | + } |
| 106 | + checkValidity() { |
| 107 | + return this.internals.checkValidity(); |
| 108 | + } |
| 109 | + reportValidity() { |
| 110 | + return this.internals.reportValidity(); |
| 111 | + } |
| 112 | + } |
| 113 | + return WebField; |
| 114 | +} |
0 commit comments