|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2023 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import {isServer, LitElement} from 'lit'; |
| 8 | +import {property} from 'lit/decorators.js'; |
| 9 | + |
| 10 | +import { |
| 11 | + afterDispatch, |
| 12 | + setupDispatchHooks, |
| 13 | +} from '../../internal/events/dispatch-hooks.js'; |
| 14 | +import {internals, WithElementInternals} from './element-internals.js'; |
| 15 | +import {MixinBase, MixinReturn} from './mixin.js'; |
| 16 | + |
| 17 | +/** |
| 18 | + * A string indicating the form submission behavior of the element. |
| 19 | + * |
| 20 | + * - submit: The element submits the form. This is the default value if the |
| 21 | + * attribute is not specified, or if it is dynamically changed to an empty or |
| 22 | + * invalid value. |
| 23 | + * - reset: The element resets the form. |
| 24 | + * - button: The element does nothing. |
| 25 | + */ |
| 26 | +export type FormSubmitterType = 'button' | 'submit' | 'reset'; |
| 27 | + |
| 28 | +/** |
| 29 | + * An element that can submit or reset a `<form>`, similar to |
| 30 | + * `<button type="submit">`. |
| 31 | + */ |
| 32 | +export interface FormSubmitter { |
| 33 | + /** |
| 34 | + * A string indicating the form submission behavior of the element. |
| 35 | + * |
| 36 | + * - submit: The element submits the form. This is the default value if the |
| 37 | + * attribute is not specified, or if it is dynamically changed to an empty or |
| 38 | + * invalid value. |
| 39 | + * - reset: The element resets the form. |
| 40 | + * - button: The element does nothing. |
| 41 | + */ |
| 42 | + type: FormSubmitterType; |
| 43 | + |
| 44 | + /** |
| 45 | + * The HTML name to use in form submission. When combined with a `value`, the |
| 46 | + * submitting button's name/value will be added to the form. |
| 47 | + * |
| 48 | + * Names must reflect to a `name` attribute for form integration. |
| 49 | + */ |
| 50 | + name: string; |
| 51 | + |
| 52 | + /** |
| 53 | + * The value of the button. When combined with a `name`, the submitting |
| 54 | + * button's name/value will be added to the form. |
| 55 | + */ |
| 56 | + value: string; |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Mixes in form submitter behavior for a class. |
| 61 | + * |
| 62 | + * A click listener is added to each element instance. If the click is not |
| 63 | + * default prevented, it will submit the element's form, if any. |
| 64 | + * |
| 65 | + * @example |
| 66 | + * ```ts |
| 67 | + * const base = mixinFormSubmitter(mixinElementInternals(LitElement)); |
| 68 | + * class MyButton extends base { |
| 69 | + * static formAssociated = true; |
| 70 | + * } |
| 71 | + * ``` |
| 72 | + * |
| 73 | + * @param base The class to mix functionality into. |
| 74 | + * @return The provided class with `FormSubmitter` mixed in. |
| 75 | + */ |
| 76 | +export function mixinFormSubmitter< |
| 77 | + T extends MixinBase<LitElement & WithElementInternals>, |
| 78 | +>(base: T): MixinReturn<T, FormSubmitter> { |
| 79 | + abstract class FormSubmitterElement extends base implements FormSubmitter { |
| 80 | + @property() type: FormSubmitterType = 'submit'; |
| 81 | + @property({reflect: true}) value = ''; |
| 82 | + |
| 83 | + // Name attribute must reflect synchronously for form integration. |
| 84 | + get name() { |
| 85 | + return this.getAttribute('name') ?? ''; |
| 86 | + } |
| 87 | + set name(name: string) { |
| 88 | + this.setAttribute('name', name); |
| 89 | + } |
| 90 | + |
| 91 | + // Mixins must have a constructor with `...args: any[]` |
| 92 | + // tslint:disable-next-line:no-any |
| 93 | + constructor(...args: any[]) { |
| 94 | + super(...args); |
| 95 | + if (isServer) return; |
| 96 | + setupDispatchHooks(this, 'click'); |
| 97 | + this.addEventListener('click', async (event: Event) => { |
| 98 | + const isReset = this.type === 'reset'; |
| 99 | + const isSubmit = this.type === 'submit'; |
| 100 | + const elementInternals = this[internals]; |
| 101 | + const {form} = elementInternals; |
| 102 | + if (!form || !(isSubmit || isReset)) { |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + afterDispatch(event, () => { |
| 107 | + if (event.defaultPrevented) { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + if (isReset) { |
| 112 | + form.reset(); |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + // form.requestSubmit(submitter) does not work with form associated custom |
| 117 | + // elements. This patches the dispatched submit event to add the correct |
| 118 | + // `submitter`. |
| 119 | + // See https://github.com/WICG/webcomponents/issues/814 |
| 120 | + form.addEventListener( |
| 121 | + 'submit', |
| 122 | + (submitEvent: Event) => { |
| 123 | + Object.defineProperty(submitEvent, 'submitter', { |
| 124 | + configurable: true, |
| 125 | + enumerable: true, |
| 126 | + get: () => this, |
| 127 | + }); |
| 128 | + }, |
| 129 | + {capture: true, once: true}, |
| 130 | + ); |
| 131 | + |
| 132 | + elementInternals.setFormValue(this.value); |
| 133 | + form.requestSubmit(); |
| 134 | + }); |
| 135 | + }); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + return FormSubmitterElement; |
| 140 | +} |
0 commit comments