@m3e/form-field 1.0.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/NOTICE.md +8 -0
- package/README.md +140 -0
- package/cem.config.mjs +16 -0
- package/demo/index.html +116 -0
- package/dist/css-custom-data.json +142 -0
- package/dist/custom-elements.json +777 -0
- package/dist/html-custom-data.json +33 -0
- package/dist/index.js +969 -0
- package/dist/index.js.map +1 -0
- package/dist/index.min.js +504 -0
- package/dist/index.min.js.map +1 -0
- package/dist/src/FloatLabelType.d.ts +3 -0
- package/dist/src/FloatLabelType.d.ts.map +1 -0
- package/dist/src/FormFieldControl.d.ts +48 -0
- package/dist/src/FormFieldControl.d.ts.map +1 -0
- package/dist/src/FormFieldElement.d.ts +144 -0
- package/dist/src/FormFieldElement.d.ts.map +1 -0
- package/dist/src/FormFieldVariant.d.ts +3 -0
- package/dist/src/FormFieldVariant.d.ts.map +1 -0
- package/dist/src/HideSubscriptType.d.ts +3 -0
- package/dist/src/HideSubscriptType.d.ts.map +1 -0
- package/dist/src/index.d.ts +6 -0
- package/dist/src/index.d.ts.map +1 -0
- package/eslint.config.mjs +13 -0
- package/package.json +48 -0
- package/rollup.config.js +32 -0
- package/src/FloatLabelType.ts +2 -0
- package/src/FormFieldControl.ts +83 -0
- package/src/FormFieldElement.ts +876 -0
- package/src/FormFieldVariant.ts +2 -0
- package/src/HideSubscriptType.ts +2 -0
- package/src/index.ts +5 -0
- package/tsconfig.json +9 -0
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adapted from Angular Material Form Field Control
|
|
3
|
+
* Source: https://github.com/angular/components/blob/main/src/material/form-field/form-field-control.ts
|
|
4
|
+
*
|
|
5
|
+
* @license MIT
|
|
6
|
+
* Copyright (c) 2025 Google LLC
|
|
7
|
+
* See LICENSE file in the project root for full license text.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** An interface which allows a control to work inside of a `M3eFormField`. */
|
|
11
|
+
export interface FormFieldControl extends HTMLElement {
|
|
12
|
+
/** A value indicating whether the control is disabled. */
|
|
13
|
+
disabled: boolean;
|
|
14
|
+
|
|
15
|
+
/** The value of the control. */
|
|
16
|
+
value?: unknown;
|
|
17
|
+
|
|
18
|
+
/** A value indicating whether the control is required. */
|
|
19
|
+
required?: boolean;
|
|
20
|
+
|
|
21
|
+
/** A value indicated whether the content of the control is read-only. */
|
|
22
|
+
readonly?: boolean;
|
|
23
|
+
|
|
24
|
+
/** A value indicating whether the form field's label should try to float. */
|
|
25
|
+
readonly shouldLabelFloat?: boolean;
|
|
26
|
+
|
|
27
|
+
/** The error message that would be displayed if the user submits the form, or an empty string if no error message. */
|
|
28
|
+
readonly validationMessage?: string;
|
|
29
|
+
|
|
30
|
+
/** The `HTMLFormElement` associated with this element. */
|
|
31
|
+
readonly form?: HTMLFormElement | null;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Handles the click event on the control's container.
|
|
35
|
+
* @param {MouseEvent} event The `MouseEvent`.
|
|
36
|
+
*/
|
|
37
|
+
onContainerClick?: (event: MouseEvent) => void;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Returns `true` if the element has no validity problems; otherwise,
|
|
41
|
+
* returns `false`, fires an invalid event.
|
|
42
|
+
*/
|
|
43
|
+
checkValidity?: () => boolean;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const KNOWN_FORM_FIELD_TAGS = ["m3e-input-chip-set", "m3e-select"];
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Determines whether a value is a `FormFieldControl`.
|
|
50
|
+
* @param {unknown} value The value to test.
|
|
51
|
+
* @returns {value is FormFieldControl} A value indicating whether `value` is a `FormFieldControl`.
|
|
52
|
+
*/
|
|
53
|
+
export function isFormFieldControl(value: unknown): value is FormFieldControl {
|
|
54
|
+
return (
|
|
55
|
+
value instanceof HTMLElement &&
|
|
56
|
+
(value instanceof HTMLInputElement ||
|
|
57
|
+
value instanceof HTMLTextAreaElement ||
|
|
58
|
+
value instanceof HTMLSelectElement ||
|
|
59
|
+
KNOWN_FORM_FIELD_TAGS.includes(value.tagName.toLowerCase()))
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Locates the first `FormFieldControl` in a given slot.
|
|
65
|
+
* @param {HTMLSlotElement} slot The slot in which to locate a `FormFieldControl`.
|
|
66
|
+
* @returns {FormFieldControl | null} The `FormFieldControl` located in `slot`.
|
|
67
|
+
*/
|
|
68
|
+
export function findFormFieldControl(slot: HTMLSlotElement): FormFieldControl | null {
|
|
69
|
+
for (const element of slot.assignedElements({ flatten: true })) {
|
|
70
|
+
if (isFormFieldControl(element)) {
|
|
71
|
+
return element;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const walker = document.createTreeWalker(element, NodeFilter.SHOW_ELEMENT);
|
|
75
|
+
while (walker.nextNode()) {
|
|
76
|
+
if (isFormFieldControl(walker.currentNode)) {
|
|
77
|
+
return walker.currentNode;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return null;
|
|
83
|
+
}
|