@material/web 1.0.2-nightly.6a1fb38.0 → 1.0.2-nightly.f7a66a8.0
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/checkbox/internal/checkbox.d.ts +5 -65
- package/checkbox/internal/checkbox.js +11 -115
- package/checkbox/internal/checkbox.js.map +1 -1
- package/labs/behaviors/constraint-validation.d.ts +133 -0
- package/labs/behaviors/constraint-validation.js +114 -0
- package/labs/behaviors/constraint-validation.js.map +1 -0
- package/labs/behaviors/validators/checkbox-validator.d.ts +32 -0
- package/labs/behaviors/validators/checkbox-validator.js +32 -0
- package/labs/behaviors/validators/checkbox-validator.js.map +1 -0
- package/labs/behaviors/validators/validator.d.ts +92 -0
- package/labs/behaviors/validators/validator.js +73 -0
- package/labs/behaviors/validators/validator.js.map +1 -0
- package/menu/internal/controllers/surfacePositionController.d.ts +2 -1
- package/menu/internal/controllers/surfacePositionController.js.map +1 -1
- package/package.json +1 -1
- package/switch/internal/switch.d.ts +5 -65
- package/switch/internal/switch.js +14 -115
- package/switch/internal/switch.js.map +1 -1
|
@@ -6,8 +6,10 @@
|
|
|
6
6
|
import '../../focus/md-focus-ring.js';
|
|
7
7
|
import '../../ripple/ripple.js';
|
|
8
8
|
import { LitElement, PropertyValues } from 'lit';
|
|
9
|
+
import { createValidator, getValidityAnchor } from '../../labs/behaviors/constraint-validation.js';
|
|
9
10
|
import { getFormState, getFormValue } from '../../labs/behaviors/form-associated.js';
|
|
10
|
-
|
|
11
|
+
import { CheckboxValidator } from '../../labs/behaviors/validators/checkbox-validator.js';
|
|
12
|
+
declare const checkboxBaseClass: import("../../labs/behaviors/mixin.js").MixinReturn<import("../../labs/behaviors/mixin.js").MixinReturn<(abstract new (...args: any[]) => import("../../labs/behaviors/element-internals.js").WithElementInternals) & typeof LitElement & import("../../labs/behaviors/form-associated.js").FormAssociatedConstructor, import("../../labs/behaviors/form-associated.js").FormAssociated>, import("../../labs/behaviors/constraint-validation.js").ConstraintValidation>;
|
|
11
13
|
/**
|
|
12
14
|
* A checkbox component.
|
|
13
15
|
*
|
|
@@ -49,83 +51,21 @@ export declare class Checkbox extends checkboxBaseClass {
|
|
|
49
51
|
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#value
|
|
50
52
|
*/
|
|
51
53
|
value: string;
|
|
52
|
-
/**
|
|
53
|
-
* Returns a ValidityState object that represents the validity states of the
|
|
54
|
-
* checkbox.
|
|
55
|
-
*
|
|
56
|
-
* Note that checkboxes will only set `valueMissing` if `required` and not
|
|
57
|
-
* checked.
|
|
58
|
-
*
|
|
59
|
-
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#validation
|
|
60
|
-
*/
|
|
61
|
-
get validity(): ValidityState;
|
|
62
|
-
/**
|
|
63
|
-
* Returns the native validation error message.
|
|
64
|
-
*
|
|
65
|
-
* https://developer.mozilla.org/en-US/docs/Web/HTML/Constraint_validation#constraint_validation_process
|
|
66
|
-
*/
|
|
67
|
-
get validationMessage(): string;
|
|
68
|
-
/**
|
|
69
|
-
* Returns whether an element will successfully validate based on forms
|
|
70
|
-
* validation rules and constraints.
|
|
71
|
-
*
|
|
72
|
-
* https://developer.mozilla.org/en-US/docs/Web/HTML/Constraint_validation#constraint_validation_process
|
|
73
|
-
*/
|
|
74
|
-
get willValidate(): boolean;
|
|
75
54
|
private prevChecked;
|
|
76
55
|
private prevDisabled;
|
|
77
56
|
private prevIndeterminate;
|
|
78
57
|
private readonly input;
|
|
79
|
-
private hasCustomValidityError;
|
|
80
58
|
constructor();
|
|
81
|
-
/**
|
|
82
|
-
* Checks the checkbox's native validation and returns whether or not the
|
|
83
|
-
* element is valid.
|
|
84
|
-
*
|
|
85
|
-
* If invalid, this method will dispatch the `invalid` event.
|
|
86
|
-
*
|
|
87
|
-
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/checkValidity
|
|
88
|
-
*
|
|
89
|
-
* @return true if the checkbox is valid, or false if not.
|
|
90
|
-
*/
|
|
91
|
-
checkValidity(): boolean;
|
|
92
|
-
/**
|
|
93
|
-
* Checks the checkbox's native validation and returns whether or not the
|
|
94
|
-
* element is valid.
|
|
95
|
-
*
|
|
96
|
-
* If invalid, this method will dispatch the `invalid` event.
|
|
97
|
-
*
|
|
98
|
-
* The `validationMessage` is reported to the user by the browser. Use
|
|
99
|
-
* `setCustomValidity()` to customize the `validationMessage`.
|
|
100
|
-
*
|
|
101
|
-
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/reportValidity
|
|
102
|
-
*
|
|
103
|
-
* @return true if the checkbox is valid, or false if not.
|
|
104
|
-
*/
|
|
105
|
-
reportValidity(): boolean;
|
|
106
|
-
/**
|
|
107
|
-
* Sets the checkbox's native validation error message. This is used to
|
|
108
|
-
* customize `validationMessage`.
|
|
109
|
-
*
|
|
110
|
-
* When the error is not an empty string, the checkbox is considered invalid
|
|
111
|
-
* and `validity.customError` will be true.
|
|
112
|
-
*
|
|
113
|
-
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/setCustomValidity
|
|
114
|
-
*
|
|
115
|
-
* @param error The error message to display.
|
|
116
|
-
*/
|
|
117
|
-
setCustomValidity(error: string): void;
|
|
118
59
|
protected update(changed: PropertyValues<Checkbox>): void;
|
|
119
60
|
protected render(): import("lit-html").TemplateResult<1>;
|
|
120
|
-
protected updated(): void;
|
|
121
61
|
private handleChange;
|
|
122
|
-
private syncValidity;
|
|
123
|
-
private getInput;
|
|
124
62
|
disabled: boolean;
|
|
125
63
|
name: string;
|
|
126
64
|
[getFormValue](): string;
|
|
127
65
|
[getFormState](): string;
|
|
128
66
|
formResetCallback(): void;
|
|
129
67
|
formStateRestoreCallback(state: string): void;
|
|
68
|
+
[createValidator](): CheckboxValidator;
|
|
69
|
+
[getValidityAnchor](): HTMLInputElement;
|
|
130
70
|
}
|
|
131
71
|
export {};
|
|
@@ -11,10 +11,12 @@ import { property, query, state } from 'lit/decorators.js';
|
|
|
11
11
|
import { classMap } from 'lit/directives/class-map.js';
|
|
12
12
|
import { requestUpdateOnAriaChange } from '../../internal/aria/delegate.js';
|
|
13
13
|
import { dispatchActivationClick, isActivationClick, redispatchEvent, } from '../../internal/controller/events.js';
|
|
14
|
-
import {
|
|
14
|
+
import { createValidator, getValidityAnchor, mixinConstraintValidation, } from '../../labs/behaviors/constraint-validation.js';
|
|
15
|
+
import { mixinElementInternals } from '../../labs/behaviors/element-internals.js';
|
|
15
16
|
import { getFormState, getFormValue, mixinFormAssociated, } from '../../labs/behaviors/form-associated.js';
|
|
17
|
+
import { CheckboxValidator } from '../../labs/behaviors/validators/checkbox-validator.js';
|
|
16
18
|
// Separate variable needed for closure.
|
|
17
|
-
const checkboxBaseClass = mixinFormAssociated(mixinElementInternals(LitElement));
|
|
19
|
+
const checkboxBaseClass = mixinConstraintValidation(mixinFormAssociated(mixinElementInternals(LitElement)));
|
|
18
20
|
/**
|
|
19
21
|
* A checkbox component.
|
|
20
22
|
*
|
|
@@ -27,38 +29,6 @@ const checkboxBaseClass = mixinFormAssociated(mixinElementInternals(LitElement))
|
|
|
27
29
|
* --bubbles --composed
|
|
28
30
|
*/
|
|
29
31
|
export class Checkbox extends checkboxBaseClass {
|
|
30
|
-
/**
|
|
31
|
-
* Returns a ValidityState object that represents the validity states of the
|
|
32
|
-
* checkbox.
|
|
33
|
-
*
|
|
34
|
-
* Note that checkboxes will only set `valueMissing` if `required` and not
|
|
35
|
-
* checked.
|
|
36
|
-
*
|
|
37
|
-
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#validation
|
|
38
|
-
*/
|
|
39
|
-
get validity() {
|
|
40
|
-
this.syncValidity();
|
|
41
|
-
return this[internals].validity;
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Returns the native validation error message.
|
|
45
|
-
*
|
|
46
|
-
* https://developer.mozilla.org/en-US/docs/Web/HTML/Constraint_validation#constraint_validation_process
|
|
47
|
-
*/
|
|
48
|
-
get validationMessage() {
|
|
49
|
-
this.syncValidity();
|
|
50
|
-
return this[internals].validationMessage;
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Returns whether an element will successfully validate based on forms
|
|
54
|
-
* validation rules and constraints.
|
|
55
|
-
*
|
|
56
|
-
* https://developer.mozilla.org/en-US/docs/Web/HTML/Constraint_validation#constraint_validation_process
|
|
57
|
-
*/
|
|
58
|
-
get willValidate() {
|
|
59
|
-
this.syncValidity();
|
|
60
|
-
return this[internals].willValidate;
|
|
61
|
-
}
|
|
62
32
|
constructor() {
|
|
63
33
|
super();
|
|
64
34
|
/**
|
|
@@ -87,12 +57,9 @@ export class Checkbox extends checkboxBaseClass {
|
|
|
87
57
|
this.prevChecked = false;
|
|
88
58
|
this.prevDisabled = false;
|
|
89
59
|
this.prevIndeterminate = false;
|
|
90
|
-
// Needed for Safari, see https://bugs.webkit.org/show_bug.cgi?id=261432
|
|
91
|
-
// Replace with this[internals].validity.customError when resolved.
|
|
92
|
-
this.hasCustomValidityError = false;
|
|
93
60
|
if (!isServer) {
|
|
94
61
|
this.addEventListener('click', (event) => {
|
|
95
|
-
if (!isActivationClick(event)) {
|
|
62
|
+
if (!isActivationClick(event) || !this.input) {
|
|
96
63
|
return;
|
|
97
64
|
}
|
|
98
65
|
this.focus();
|
|
@@ -100,52 +67,6 @@ export class Checkbox extends checkboxBaseClass {
|
|
|
100
67
|
});
|
|
101
68
|
}
|
|
102
69
|
}
|
|
103
|
-
/**
|
|
104
|
-
* Checks the checkbox's native validation and returns whether or not the
|
|
105
|
-
* element is valid.
|
|
106
|
-
*
|
|
107
|
-
* If invalid, this method will dispatch the `invalid` event.
|
|
108
|
-
*
|
|
109
|
-
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/checkValidity
|
|
110
|
-
*
|
|
111
|
-
* @return true if the checkbox is valid, or false if not.
|
|
112
|
-
*/
|
|
113
|
-
checkValidity() {
|
|
114
|
-
this.syncValidity();
|
|
115
|
-
return this[internals].checkValidity();
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* Checks the checkbox's native validation and returns whether or not the
|
|
119
|
-
* element is valid.
|
|
120
|
-
*
|
|
121
|
-
* If invalid, this method will dispatch the `invalid` event.
|
|
122
|
-
*
|
|
123
|
-
* The `validationMessage` is reported to the user by the browser. Use
|
|
124
|
-
* `setCustomValidity()` to customize the `validationMessage`.
|
|
125
|
-
*
|
|
126
|
-
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/reportValidity
|
|
127
|
-
*
|
|
128
|
-
* @return true if the checkbox is valid, or false if not.
|
|
129
|
-
*/
|
|
130
|
-
reportValidity() {
|
|
131
|
-
this.syncValidity();
|
|
132
|
-
return this[internals].reportValidity();
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* Sets the checkbox's native validation error message. This is used to
|
|
136
|
-
* customize `validationMessage`.
|
|
137
|
-
*
|
|
138
|
-
* When the error is not an empty string, the checkbox is considered invalid
|
|
139
|
-
* and `validity.customError` will be true.
|
|
140
|
-
*
|
|
141
|
-
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/setCustomValidity
|
|
142
|
-
*
|
|
143
|
-
* @param error The error message to display.
|
|
144
|
-
*/
|
|
145
|
-
setCustomValidity(error) {
|
|
146
|
-
this.hasCustomValidityError = !!error;
|
|
147
|
-
this[internals].setValidity({ customError: !!error }, error, this.getInput());
|
|
148
|
-
}
|
|
149
70
|
update(changed) {
|
|
150
71
|
if (changed.has('checked') ||
|
|
151
72
|
changed.has('disabled') ||
|
|
@@ -203,43 +124,12 @@ export class Checkbox extends checkboxBaseClass {
|
|
|
203
124
|
</div>
|
|
204
125
|
`;
|
|
205
126
|
}
|
|
206
|
-
updated() {
|
|
207
|
-
// Sync validity when properties change, since validation properties may
|
|
208
|
-
// have changed.
|
|
209
|
-
this.syncValidity();
|
|
210
|
-
}
|
|
211
127
|
handleChange(event) {
|
|
212
128
|
const target = event.target;
|
|
213
129
|
this.checked = target.checked;
|
|
214
130
|
this.indeterminate = target.indeterminate;
|
|
215
131
|
redispatchEvent(this, event);
|
|
216
132
|
}
|
|
217
|
-
syncValidity() {
|
|
218
|
-
// Sync the internal <input>'s validity and the host's ElementInternals
|
|
219
|
-
// validity. We do this to re-use native `<input>` validation messages.
|
|
220
|
-
const input = this.getInput();
|
|
221
|
-
if (this.hasCustomValidityError) {
|
|
222
|
-
input.setCustomValidity(this[internals].validationMessage);
|
|
223
|
-
}
|
|
224
|
-
else {
|
|
225
|
-
input.setCustomValidity('');
|
|
226
|
-
}
|
|
227
|
-
this[internals].setValidity(input.validity, input.validationMessage, this.getInput());
|
|
228
|
-
}
|
|
229
|
-
getInput() {
|
|
230
|
-
if (!this.input) {
|
|
231
|
-
// If the input is not yet defined, synchronously render.
|
|
232
|
-
this.connectedCallback();
|
|
233
|
-
this.performUpdate();
|
|
234
|
-
}
|
|
235
|
-
if (this.isUpdatePending) {
|
|
236
|
-
// If there are pending updates, synchronously perform them. This ensures
|
|
237
|
-
// that constraint validation properties (like `required`) are synced
|
|
238
|
-
// before interacting with input APIs that depend on them.
|
|
239
|
-
this.scheduleUpdate();
|
|
240
|
-
}
|
|
241
|
-
return this.input;
|
|
242
|
-
}
|
|
243
133
|
[getFormValue]() {
|
|
244
134
|
if (!this.checked || this.indeterminate) {
|
|
245
135
|
return null;
|
|
@@ -257,6 +147,12 @@ export class Checkbox extends checkboxBaseClass {
|
|
|
257
147
|
formStateRestoreCallback(state) {
|
|
258
148
|
this.checked = state === 'true';
|
|
259
149
|
}
|
|
150
|
+
[createValidator]() {
|
|
151
|
+
return new CheckboxValidator(() => this);
|
|
152
|
+
}
|
|
153
|
+
[getValidityAnchor]() {
|
|
154
|
+
return this.input;
|
|
155
|
+
}
|
|
260
156
|
}
|
|
261
157
|
(() => {
|
|
262
158
|
requestUpdateOnAriaChange(Checkbox);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"checkbox.js","sourceRoot":"","sources":["checkbox.ts"],"names":[],"mappings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AhC,KAAK,CAAC,OAAO,CAAC;uCAAkD","sourcesContent":["/**\n * @license\n * Copyright 2019 Google LLC\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport '../../focus/md-focus-ring.js';\nimport '../../ripple/ripple.js';\n\nimport {html, isServer, LitElement, nothing, PropertyValues} from 'lit';\nimport {property, query, state} from 'lit/decorators.js';\nimport {classMap} from 'lit/directives/class-map.js';\n\nimport {ARIAMixinStrict} from '../../internal/aria/aria.js';\nimport {requestUpdateOnAriaChange} from '../../internal/aria/delegate.js';\nimport {\n dispatchActivationClick,\n isActivationClick,\n redispatchEvent,\n} from '../../internal/controller/events.js';\nimport {\n internals,\n mixinElementInternals,\n} from '../../labs/behaviors/element-internals.js';\nimport {\n getFormState,\n getFormValue,\n mixinFormAssociated,\n} from '../../labs/behaviors/form-associated.js';\n\n// Separate variable needed for closure.\nconst checkboxBaseClass = mixinFormAssociated(\n mixinElementInternals(LitElement),\n);\n\n/**\n * A checkbox component.\n *\n *\n * @fires change {Event} The native `change` event on\n * [`<input>`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/change_event)\n * --bubbles\n * @fires input {InputEvent} The native `input` event on\n * [`<input>`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/input_event)\n * --bubbles --composed\n */\nexport class Checkbox extends checkboxBaseClass {\n static {\n requestUpdateOnAriaChange(Checkbox);\n }\n\n /** @nocollapse */\n static override shadowRootOptions = {\n ...LitElement.shadowRootOptions,\n delegatesFocus: true,\n };\n\n /**\n * Whether or not the checkbox is selected.\n */\n @property({type: Boolean}) checked = false;\n\n /**\n * Whether or not the checkbox is indeterminate.\n *\n * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#indeterminate_state_checkboxes\n */\n @property({type: Boolean}) indeterminate = false;\n\n /**\n * When true, require the checkbox to be selected when participating in\n * form submission.\n *\n * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#validation\n */\n @property({type: Boolean}) required = false;\n\n /**\n * The value of the checkbox that is submitted with a form when selected.\n *\n * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#value\n */\n @property() value = 'on';\n\n /**\n * Returns a ValidityState object that represents the validity states of the\n * checkbox.\n *\n * Note that checkboxes will only set `valueMissing` if `required` and not\n * checked.\n *\n * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#validation\n */\n get validity() {\n this.syncValidity();\n return this[internals].validity;\n }\n\n /**\n * Returns the native validation error message.\n *\n * https://developer.mozilla.org/en-US/docs/Web/HTML/Constraint_validation#constraint_validation_process\n */\n get validationMessage() {\n this.syncValidity();\n return this[internals].validationMessage;\n }\n\n /**\n * Returns whether an element will successfully validate based on forms\n * validation rules and constraints.\n *\n * https://developer.mozilla.org/en-US/docs/Web/HTML/Constraint_validation#constraint_validation_process\n */\n get willValidate() {\n this.syncValidity();\n return this[internals].willValidate;\n }\n\n @state() private prevChecked = false;\n @state() private prevDisabled = false;\n @state() private prevIndeterminate = false;\n @query('input') private readonly input!: HTMLInputElement | null;\n // Needed for Safari, see https://bugs.webkit.org/show_bug.cgi?id=261432\n // Replace with this[internals].validity.customError when resolved.\n private hasCustomValidityError = false;\n\n constructor() {\n super();\n if (!isServer) {\n this.addEventListener('click', (event: MouseEvent) => {\n if (!isActivationClick(event)) {\n return;\n }\n this.focus();\n dispatchActivationClick(this.input!);\n });\n }\n }\n\n /**\n * Checks the checkbox's native validation and returns whether or not the\n * element is valid.\n *\n * If invalid, this method will dispatch the `invalid` event.\n *\n * https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/checkValidity\n *\n * @return true if the checkbox is valid, or false if not.\n */\n checkValidity() {\n this.syncValidity();\n return this[internals].checkValidity();\n }\n\n /**\n * Checks the checkbox's native validation and returns whether or not the\n * element is valid.\n *\n * If invalid, this method will dispatch the `invalid` event.\n *\n * The `validationMessage` is reported to the user by the browser. Use\n * `setCustomValidity()` to customize the `validationMessage`.\n *\n * https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/reportValidity\n *\n * @return true if the checkbox is valid, or false if not.\n */\n reportValidity() {\n this.syncValidity();\n return this[internals].reportValidity();\n }\n\n /**\n * Sets the checkbox's native validation error message. This is used to\n * customize `validationMessage`.\n *\n * When the error is not an empty string, the checkbox is considered invalid\n * and `validity.customError` will be true.\n *\n * https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/setCustomValidity\n *\n * @param error The error message to display.\n */\n setCustomValidity(error: string) {\n this.hasCustomValidityError = !!error;\n this[internals].setValidity({customError: !!error}, error, this.getInput());\n }\n\n protected override update(changed: PropertyValues<Checkbox>) {\n if (\n changed.has('checked') ||\n changed.has('disabled') ||\n changed.has('indeterminate')\n ) {\n this.prevChecked = changed.get('checked') ?? this.checked;\n this.prevDisabled = changed.get('disabled') ?? this.disabled;\n this.prevIndeterminate =\n changed.get('indeterminate') ?? this.indeterminate;\n }\n\n super.update(changed);\n }\n\n protected override render() {\n const prevNone = !this.prevChecked && !this.prevIndeterminate;\n const prevChecked = this.prevChecked && !this.prevIndeterminate;\n const prevIndeterminate = this.prevIndeterminate;\n const isChecked = this.checked && !this.indeterminate;\n const isIndeterminate = this.indeterminate;\n\n const containerClasses = classMap({\n 'disabled': this.disabled,\n 'selected': isChecked || isIndeterminate,\n 'unselected': !isChecked && !isIndeterminate,\n 'checked': isChecked,\n 'indeterminate': isIndeterminate,\n 'prev-unselected': prevNone,\n 'prev-checked': prevChecked,\n 'prev-indeterminate': prevIndeterminate,\n 'prev-disabled': this.prevDisabled,\n });\n\n // Needed for closure conformance\n const {ariaLabel, ariaInvalid} = this as ARIAMixinStrict;\n // Note: <input> needs to be rendered before the <svg> for\n // form.reportValidity() to work in Chrome.\n return html`\n <div class=\"container ${containerClasses}\">\n <input\n type=\"checkbox\"\n id=\"input\"\n aria-checked=${isIndeterminate ? 'mixed' : nothing}\n aria-label=${ariaLabel || nothing}\n aria-invalid=${ariaInvalid || nothing}\n ?disabled=${this.disabled}\n ?required=${this.required}\n .indeterminate=${this.indeterminate}\n .checked=${this.checked}\n @change=${this.handleChange} />\n\n <div class=\"outline\"></div>\n <div class=\"background\"></div>\n <md-focus-ring part=\"focus-ring\" for=\"input\"></md-focus-ring>\n <md-ripple for=\"input\" ?disabled=${this.disabled}></md-ripple>\n <svg class=\"icon\" viewBox=\"0 0 18 18\" aria-hidden=\"true\">\n <rect class=\"mark short\" />\n <rect class=\"mark long\" />\n </svg>\n </div>\n `;\n }\n\n protected override updated() {\n // Sync validity when properties change, since validation properties may\n // have changed.\n this.syncValidity();\n }\n\n private handleChange(event: Event) {\n const target = event.target as HTMLInputElement;\n this.checked = target.checked;\n this.indeterminate = target.indeterminate;\n\n redispatchEvent(this, event);\n }\n\n private syncValidity() {\n // Sync the internal <input>'s validity and the host's ElementInternals\n // validity. We do this to re-use native `<input>` validation messages.\n const input = this.getInput();\n if (this.hasCustomValidityError) {\n input.setCustomValidity(this[internals].validationMessage);\n } else {\n input.setCustomValidity('');\n }\n\n this[internals].setValidity(\n input.validity,\n input.validationMessage,\n this.getInput(),\n );\n }\n\n private getInput() {\n if (!this.input) {\n // If the input is not yet defined, synchronously render.\n this.connectedCallback();\n this.performUpdate();\n }\n\n if (this.isUpdatePending) {\n // If there are pending updates, synchronously perform them. This ensures\n // that constraint validation properties (like `required`) are synced\n // before interacting with input APIs that depend on them.\n this.scheduleUpdate();\n }\n\n return this.input!;\n }\n\n // Writable mixin properties for lit-html binding, needed for lit-analyzer\n declare disabled: boolean;\n declare name: string;\n\n override [getFormValue]() {\n if (!this.checked || this.indeterminate) {\n return null;\n }\n\n return this.value;\n }\n\n override [getFormState]() {\n return String(this.checked);\n }\n\n override formResetCallback() {\n // The checked property does not reflect, so the original attribute set by\n // the user is used to determine the default value.\n this.checked = this.hasAttribute('checked');\n }\n\n override formStateRestoreCallback(state: string) {\n this.checked = state === 'true';\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"checkbox.js","sourceRoot":"","sources":["checkbox.ts"],"names":[],"mappings":"AAAA;;;;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;QACxB,0EAA0E;QAC1E,mDAAmD;QACnD,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,YAAY,CAAC,SAAS,CAAC,CAAC;IAC9C,CAAC;IAEQ,wBAAwB,CAAC,KAAa;QAC7C,IAAI,CAAC,OAAO,GAAG,KAAK,KAAK,MAAM,CAAC;IAClC,CAAC;IAED,CAAC,eAAe,CAAC;QACf,OAAO,IAAI,iBAAiB,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,CAAC;IAC3C,CAAC;IAED,CAAC,iBAAiB,CAAC;QACjB,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;;AA/JD;IACE,yBAAyB,CAAC,QAAQ,CAAC,CAAC;AACtC,CAAC,GAAA,CAAA;AAED,kBAAkB;AACF,0BAAiB,GAAG;IAClC,GAAG,UAAU,CAAC,iBAAiB;IAC/B,cAAc,EAAE,IAAI;CACrB,AAHgC,CAG/B;AAKyB;IAA1B,QAAQ,CAAC,EAAC,IAAI,EAAE,OAAO,EAAC,CAAC;yCAAiB;AAOhB;IAA1B,QAAQ,CAAC,EAAC,IAAI,EAAE,OAAO,EAAC,CAAC;+CAAuB;AAQtB;IAA1B,QAAQ,CAAC,EAAC,IAAI,EAAE,OAAO,EAAC,CAAC;0CAAkB;AAOhC;IAAX,QAAQ,EAAE;uCAAc;AAER;IAAhB,KAAK,EAAE;6CAA6B;AACpB;IAAhB,KAAK,EAAE;8CAA8B;AACrB;IAAhB,KAAK,EAAE;mDAAmC;AACV;IAAhC,KAAK,CAAC,OAAO,CAAC;uCAAkD","sourcesContent":["/**\n * @license\n * Copyright 2019 Google LLC\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport '../../focus/md-focus-ring.js';\nimport '../../ripple/ripple.js';\n\nimport {html, isServer, LitElement, nothing, PropertyValues} from 'lit';\nimport {property, query, state} from 'lit/decorators.js';\nimport {classMap} from 'lit/directives/class-map.js';\n\nimport {ARIAMixinStrict} from '../../internal/aria/aria.js';\nimport {requestUpdateOnAriaChange} from '../../internal/aria/delegate.js';\nimport {\n dispatchActivationClick,\n isActivationClick,\n redispatchEvent,\n} from '../../internal/controller/events.js';\nimport {\n createValidator,\n getValidityAnchor,\n mixinConstraintValidation,\n} from '../../labs/behaviors/constraint-validation.js';\nimport {mixinElementInternals} from '../../labs/behaviors/element-internals.js';\nimport {\n getFormState,\n getFormValue,\n mixinFormAssociated,\n} from '../../labs/behaviors/form-associated.js';\nimport {CheckboxValidator} from '../../labs/behaviors/validators/checkbox-validator.js';\n\n// Separate variable needed for closure.\nconst checkboxBaseClass = mixinConstraintValidation(\n mixinFormAssociated(mixinElementInternals(LitElement)),\n);\n\n/**\n * A checkbox component.\n *\n *\n * @fires change {Event} The native `change` event on\n * [`<input>`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/change_event)\n * --bubbles\n * @fires input {InputEvent} The native `input` event on\n * [`<input>`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/input_event)\n * --bubbles --composed\n */\nexport class Checkbox extends checkboxBaseClass {\n static {\n requestUpdateOnAriaChange(Checkbox);\n }\n\n /** @nocollapse */\n static override shadowRootOptions = {\n ...LitElement.shadowRootOptions,\n delegatesFocus: true,\n };\n\n /**\n * Whether or not the checkbox is selected.\n */\n @property({type: Boolean}) checked = false;\n\n /**\n * Whether or not the checkbox is indeterminate.\n *\n * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#indeterminate_state_checkboxes\n */\n @property({type: Boolean}) indeterminate = false;\n\n /**\n * When true, require the checkbox to be selected when participating in\n * form submission.\n *\n * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#validation\n */\n @property({type: Boolean}) required = false;\n\n /**\n * The value of the checkbox that is submitted with a form when selected.\n *\n * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#value\n */\n @property() value = 'on';\n\n @state() private prevChecked = false;\n @state() private prevDisabled = false;\n @state() private prevIndeterminate = false;\n @query('input') private readonly input!: HTMLInputElement | null;\n\n constructor() {\n super();\n if (!isServer) {\n this.addEventListener('click', (event: MouseEvent) => {\n if (!isActivationClick(event) || !this.input) {\n return;\n }\n this.focus();\n dispatchActivationClick(this.input);\n });\n }\n }\n\n protected override update(changed: PropertyValues<Checkbox>) {\n if (\n changed.has('checked') ||\n changed.has('disabled') ||\n changed.has('indeterminate')\n ) {\n this.prevChecked = changed.get('checked') ?? this.checked;\n this.prevDisabled = changed.get('disabled') ?? this.disabled;\n this.prevIndeterminate =\n changed.get('indeterminate') ?? this.indeterminate;\n }\n\n super.update(changed);\n }\n\n protected override render() {\n const prevNone = !this.prevChecked && !this.prevIndeterminate;\n const prevChecked = this.prevChecked && !this.prevIndeterminate;\n const prevIndeterminate = this.prevIndeterminate;\n const isChecked = this.checked && !this.indeterminate;\n const isIndeterminate = this.indeterminate;\n\n const containerClasses = classMap({\n 'disabled': this.disabled,\n 'selected': isChecked || isIndeterminate,\n 'unselected': !isChecked && !isIndeterminate,\n 'checked': isChecked,\n 'indeterminate': isIndeterminate,\n 'prev-unselected': prevNone,\n 'prev-checked': prevChecked,\n 'prev-indeterminate': prevIndeterminate,\n 'prev-disabled': this.prevDisabled,\n });\n\n // Needed for closure conformance\n const {ariaLabel, ariaInvalid} = this as ARIAMixinStrict;\n // Note: <input> needs to be rendered before the <svg> for\n // form.reportValidity() to work in Chrome.\n return html`\n <div class=\"container ${containerClasses}\">\n <input\n type=\"checkbox\"\n id=\"input\"\n aria-checked=${isIndeterminate ? 'mixed' : nothing}\n aria-label=${ariaLabel || nothing}\n aria-invalid=${ariaInvalid || nothing}\n ?disabled=${this.disabled}\n ?required=${this.required}\n .indeterminate=${this.indeterminate}\n .checked=${this.checked}\n @change=${this.handleChange} />\n\n <div class=\"outline\"></div>\n <div class=\"background\"></div>\n <md-focus-ring part=\"focus-ring\" for=\"input\"></md-focus-ring>\n <md-ripple for=\"input\" ?disabled=${this.disabled}></md-ripple>\n <svg class=\"icon\" viewBox=\"0 0 18 18\" aria-hidden=\"true\">\n <rect class=\"mark short\" />\n <rect class=\"mark long\" />\n </svg>\n </div>\n `;\n }\n\n private handleChange(event: Event) {\n const target = event.target as HTMLInputElement;\n this.checked = target.checked;\n this.indeterminate = target.indeterminate;\n\n redispatchEvent(this, event);\n }\n\n // Writable mixin properties for lit-html binding, needed for lit-analyzer\n declare disabled: boolean;\n declare name: string;\n\n override [getFormValue]() {\n if (!this.checked || this.indeterminate) {\n return null;\n }\n\n return this.value;\n }\n\n override [getFormState]() {\n return String(this.checked);\n }\n\n override formResetCallback() {\n // The checked property does not reflect, so the original attribute set by\n // the user is used to determine the default value.\n this.checked = this.hasAttribute('checked');\n }\n\n override formStateRestoreCallback(state: string) {\n this.checked = state === 'true';\n }\n\n [createValidator]() {\n return new CheckboxValidator(() => this);\n }\n\n [getValidityAnchor]() {\n return this.input;\n }\n}\n"]}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2023 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { LitElement } from 'lit';
|
|
7
|
+
import { WithElementInternals } from './element-internals.js';
|
|
8
|
+
import { FormAssociated } from './form-associated.js';
|
|
9
|
+
import { MixinBase, MixinReturn } from './mixin.js';
|
|
10
|
+
import { Validator } from './validators/validator.js';
|
|
11
|
+
/**
|
|
12
|
+
* A form associated element that provides constraint validation APIs.
|
|
13
|
+
*
|
|
14
|
+
* https://developer.mozilla.org/en-US/docs/Web/HTML/Constraint_validation
|
|
15
|
+
*/
|
|
16
|
+
export interface ConstraintValidation {
|
|
17
|
+
/**
|
|
18
|
+
* Returns a ValidityState object that represents the validity states of the
|
|
19
|
+
* element.
|
|
20
|
+
*
|
|
21
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/ValidityState
|
|
22
|
+
*/
|
|
23
|
+
readonly validity: ValidityState;
|
|
24
|
+
/**
|
|
25
|
+
* Returns a validation error message or an empty string if the element is
|
|
26
|
+
* valid.
|
|
27
|
+
*
|
|
28
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/ElementInternals/validationMessage
|
|
29
|
+
*/
|
|
30
|
+
readonly validationMessage: string;
|
|
31
|
+
/**
|
|
32
|
+
* Returns whether an element will successfully validate based on forms
|
|
33
|
+
* validation rules and constraints.
|
|
34
|
+
*
|
|
35
|
+
* Disabled and readonly elements will not validate.
|
|
36
|
+
*
|
|
37
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/ElementInternals/willValidate
|
|
38
|
+
*/
|
|
39
|
+
readonly willValidate: boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Checks the element's constraint validation and returns true if the element
|
|
42
|
+
* is valid or false if not.
|
|
43
|
+
*
|
|
44
|
+
* If invalid, this method will dispatch an `invalid` event.
|
|
45
|
+
*
|
|
46
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/ElementInternals/checkValidity
|
|
47
|
+
*
|
|
48
|
+
* @return true if the element is valid, or false if not.
|
|
49
|
+
*/
|
|
50
|
+
checkValidity(): boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Checks the element's constraint validation and returns true if the element
|
|
53
|
+
* is valid or false if not.
|
|
54
|
+
*
|
|
55
|
+
* If invalid, this method will dispatch a cancelable `invalid` event. If not
|
|
56
|
+
* canceled, a the current `validationMessage` will be reported to the user.
|
|
57
|
+
*
|
|
58
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/ElementInternals/reportValidity
|
|
59
|
+
*
|
|
60
|
+
* @return true if the element is valid, or false if not.
|
|
61
|
+
*/
|
|
62
|
+
reportValidity(): boolean;
|
|
63
|
+
/**
|
|
64
|
+
* Sets the element's constraint validation error message. When set to a
|
|
65
|
+
* non-empty string, `validity.customError` will be true and
|
|
66
|
+
* `validationMessage` will display the provided error.
|
|
67
|
+
*
|
|
68
|
+
* Use this method to customize error messages reported.
|
|
69
|
+
*
|
|
70
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/setCustomValidity
|
|
71
|
+
*
|
|
72
|
+
* @param error The error message to display, or an empty string.
|
|
73
|
+
*/
|
|
74
|
+
setCustomValidity(error: string): void;
|
|
75
|
+
/**
|
|
76
|
+
* Creates and returns a `Validator` that is used to compute and cache
|
|
77
|
+
* validity for the element.
|
|
78
|
+
*
|
|
79
|
+
* A validator that caches validity is important since constraint validation
|
|
80
|
+
* must be computed synchronously and frequently in response to constraint
|
|
81
|
+
* validation property changes.
|
|
82
|
+
*/
|
|
83
|
+
[createValidator](): Validator<unknown>;
|
|
84
|
+
/**
|
|
85
|
+
* Returns shadow DOM child that is used as the anchor for the platform
|
|
86
|
+
* `reportValidity()` popup. This is often the root element or the inner
|
|
87
|
+
* focus-delegated element.
|
|
88
|
+
*/
|
|
89
|
+
[getValidityAnchor](): HTMLElement | null;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* A symbol property used to create a constraint validation `Validator`.
|
|
93
|
+
* Required for all `mixinConstraintValidation()` elements.
|
|
94
|
+
*/
|
|
95
|
+
export declare const createValidator: unique symbol;
|
|
96
|
+
/**
|
|
97
|
+
* A symbol property used to return an anchor for constraint validation popups.
|
|
98
|
+
* Required for all `mixinConstraintValidation()` elements.
|
|
99
|
+
*/
|
|
100
|
+
export declare const getValidityAnchor: unique symbol;
|
|
101
|
+
/**
|
|
102
|
+
* Mixins in constraint validation APIs for an element.
|
|
103
|
+
*
|
|
104
|
+
* See https://developer.mozilla.org/en-US/docs/Web/HTML/Constraint_validation
|
|
105
|
+
* for more details.
|
|
106
|
+
*
|
|
107
|
+
* Implementations must provide a validator to cache and compute its validity,
|
|
108
|
+
* along with a shadow root element to anchor validation popups to.
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* ```ts
|
|
112
|
+
* const baseClass = mixinConstraintValidation(
|
|
113
|
+
* mixinFormAssociated(mixinElementInternals(LitElement))
|
|
114
|
+
* );
|
|
115
|
+
*
|
|
116
|
+
* class MyCheckbox extends baseClass {
|
|
117
|
+
* \@property({type: Boolean}) checked = false;
|
|
118
|
+
* \@property({type: Boolean}) required = false;
|
|
119
|
+
*
|
|
120
|
+
* [createValidator]() {
|
|
121
|
+
* return new CheckboxValidator(() => this);
|
|
122
|
+
* }
|
|
123
|
+
*
|
|
124
|
+
* [getValidityAnchor]() {
|
|
125
|
+
* return this.renderRoot.querySelector('.root');
|
|
126
|
+
* }
|
|
127
|
+
* }
|
|
128
|
+
* ```
|
|
129
|
+
*
|
|
130
|
+
* @param base The class to mix functionality into.
|
|
131
|
+
* @return The provided class with `ConstraintValidation` mixed in.
|
|
132
|
+
*/
|
|
133
|
+
export declare function mixinConstraintValidation<T extends MixinBase<LitElement & FormAssociated & WithElementInternals>>(base: T): MixinReturn<T, ConstraintValidation>;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2023 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { isServer } from 'lit';
|
|
7
|
+
import { internals } from './element-internals.js';
|
|
8
|
+
/**
|
|
9
|
+
* A symbol property used to create a constraint validation `Validator`.
|
|
10
|
+
* Required for all `mixinConstraintValidation()` elements.
|
|
11
|
+
*/
|
|
12
|
+
export const createValidator = Symbol('createValidator');
|
|
13
|
+
/**
|
|
14
|
+
* A symbol property used to return an anchor for constraint validation popups.
|
|
15
|
+
* Required for all `mixinConstraintValidation()` elements.
|
|
16
|
+
*/
|
|
17
|
+
export const getValidityAnchor = Symbol('getValidityAnchor');
|
|
18
|
+
// Private symbol members, used to avoid name clashing.
|
|
19
|
+
const privateValidator = Symbol('privateValidator');
|
|
20
|
+
const privateSyncValidity = Symbol('privateSyncValidity');
|
|
21
|
+
const privateCustomValidationMessage = Symbol('privateCustomValidationMessage');
|
|
22
|
+
/**
|
|
23
|
+
* Mixins in constraint validation APIs for an element.
|
|
24
|
+
*
|
|
25
|
+
* See https://developer.mozilla.org/en-US/docs/Web/HTML/Constraint_validation
|
|
26
|
+
* for more details.
|
|
27
|
+
*
|
|
28
|
+
* Implementations must provide a validator to cache and compute its validity,
|
|
29
|
+
* along with a shadow root element to anchor validation popups to.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* const baseClass = mixinConstraintValidation(
|
|
34
|
+
* mixinFormAssociated(mixinElementInternals(LitElement))
|
|
35
|
+
* );
|
|
36
|
+
*
|
|
37
|
+
* class MyCheckbox extends baseClass {
|
|
38
|
+
* \@property({type: Boolean}) checked = false;
|
|
39
|
+
* \@property({type: Boolean}) required = false;
|
|
40
|
+
*
|
|
41
|
+
* [createValidator]() {
|
|
42
|
+
* return new CheckboxValidator(() => this);
|
|
43
|
+
* }
|
|
44
|
+
*
|
|
45
|
+
* [getValidityAnchor]() {
|
|
46
|
+
* return this.renderRoot.querySelector('.root');
|
|
47
|
+
* }
|
|
48
|
+
* }
|
|
49
|
+
* ```
|
|
50
|
+
*
|
|
51
|
+
* @param base The class to mix functionality into.
|
|
52
|
+
* @return The provided class with `ConstraintValidation` mixed in.
|
|
53
|
+
*/
|
|
54
|
+
export function mixinConstraintValidation(base) {
|
|
55
|
+
var _a;
|
|
56
|
+
class ConstraintValidationElement extends base {
|
|
57
|
+
constructor() {
|
|
58
|
+
super(...arguments);
|
|
59
|
+
/**
|
|
60
|
+
* Needed for Safari, see https://bugs.webkit.org/show_bug.cgi?id=261432
|
|
61
|
+
* Replace with this[internals].validity.customError when resolved.
|
|
62
|
+
*/
|
|
63
|
+
this[_a] = '';
|
|
64
|
+
}
|
|
65
|
+
get validity() {
|
|
66
|
+
this[privateSyncValidity]();
|
|
67
|
+
return this[internals].validity;
|
|
68
|
+
}
|
|
69
|
+
get validationMessage() {
|
|
70
|
+
this[privateSyncValidity]();
|
|
71
|
+
return this[internals].validationMessage;
|
|
72
|
+
}
|
|
73
|
+
get willValidate() {
|
|
74
|
+
this[privateSyncValidity]();
|
|
75
|
+
return this[internals].willValidate;
|
|
76
|
+
}
|
|
77
|
+
checkValidity() {
|
|
78
|
+
this[privateSyncValidity]();
|
|
79
|
+
return this[internals].checkValidity();
|
|
80
|
+
}
|
|
81
|
+
reportValidity() {
|
|
82
|
+
this[privateSyncValidity]();
|
|
83
|
+
return this[internals].reportValidity();
|
|
84
|
+
}
|
|
85
|
+
setCustomValidity(error) {
|
|
86
|
+
this[privateCustomValidationMessage] = error;
|
|
87
|
+
this[privateSyncValidity]();
|
|
88
|
+
}
|
|
89
|
+
requestUpdate(name, oldValue, options) {
|
|
90
|
+
super.requestUpdate(name, oldValue, options);
|
|
91
|
+
this[privateSyncValidity]();
|
|
92
|
+
}
|
|
93
|
+
[(_a = privateCustomValidationMessage, privateSyncValidity)]() {
|
|
94
|
+
if (isServer) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
if (!this[privateValidator]) {
|
|
98
|
+
this[privateValidator] = this[createValidator]();
|
|
99
|
+
}
|
|
100
|
+
const { validity, validationMessage: nonCustomValidationMessage } = this[privateValidator].getValidity();
|
|
101
|
+
const customError = !!this[privateCustomValidationMessage];
|
|
102
|
+
const validationMessage = this[privateCustomValidationMessage] || nonCustomValidationMessage;
|
|
103
|
+
this[internals].setValidity({ ...validity, customError }, validationMessage, this[getValidityAnchor]() ?? undefined);
|
|
104
|
+
}
|
|
105
|
+
[createValidator]() {
|
|
106
|
+
throw new Error('Implement [createValidator]');
|
|
107
|
+
}
|
|
108
|
+
[getValidityAnchor]() {
|
|
109
|
+
throw new Error('Implement [getValidityAnchor]');
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return ConstraintValidationElement;
|
|
113
|
+
}
|
|
114
|
+
//# sourceMappingURL=constraint-validation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constraint-validation.js","sourceRoot":"","sources":["constraint-validation.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAC,QAAQ,EAAkC,MAAM,KAAK,CAAC;AAE9D,OA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right 2023 Google LLC\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport {isServer, LitElement, PropertyDeclaration} from 'lit';\n\nimport {internals, WithElementInternals} from './element-internals.js';\nimport {FormAssociated} from './form-associated.js';\nimport {MixinBase, MixinReturn} from './mixin.js';\nimport {Validator} from './validators/validator.js';\n\n/**\n * A form associated element that provides constraint validation APIs.\n *\n * https://developer.mozilla.org/en-US/docs/Web/HTML/Constraint_validation\n */\nexport interface ConstraintValidation {\n /**\n * Returns a ValidityState object that represents the validity states of the\n * element.\n *\n * https://developer.mozilla.org/en-US/docs/Web/API/ValidityState\n */\n readonly validity: ValidityState;\n\n /**\n * Returns a validation error message or an empty string if the element is\n * valid.\n *\n * https://developer.mozilla.org/en-US/docs/Web/API/ElementInternals/validationMessage\n */\n readonly validationMessage: string;\n\n /**\n * Returns whether an element will successfully validate based on forms\n * validation rules and constraints.\n *\n * Disabled and readonly elements will not validate.\n *\n * https://developer.mozilla.org/en-US/docs/Web/API/ElementInternals/willValidate\n */\n readonly willValidate: boolean;\n\n /**\n * Checks the element's constraint validation and returns true if the element\n * is valid or false if not.\n *\n * If invalid, this method will dispatch an `invalid` event.\n *\n * https://developer.mozilla.org/en-US/docs/Web/API/ElementInternals/checkValidity\n *\n * @return true if the element is valid, or false if not.\n */\n checkValidity(): boolean;\n\n /**\n * Checks the element's constraint validation and returns true if the element\n * is valid or false if not.\n *\n * If invalid, this method will dispatch a cancelable `invalid` event. If not\n * canceled, a the current `validationMessage` will be reported to the user.\n *\n * https://developer.mozilla.org/en-US/docs/Web/API/ElementInternals/reportValidity\n *\n * @return true if the element is valid, or false if not.\n */\n reportValidity(): boolean;\n\n /**\n * Sets the element's constraint validation error message. When set to a\n * non-empty string, `validity.customError` will be true and\n * `validationMessage` will display the provided error.\n *\n * Use this method to customize error messages reported.\n *\n * https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/setCustomValidity\n *\n * @param error The error message to display, or an empty string.\n */\n setCustomValidity(error: string): void;\n\n /**\n * Creates and returns a `Validator` that is used to compute and cache\n * validity for the element.\n *\n * A validator that caches validity is important since constraint validation\n * must be computed synchronously and frequently in response to constraint\n * validation property changes.\n */\n [createValidator](): Validator<unknown>;\n\n /**\n * Returns shadow DOM child that is used as the anchor for the platform\n * `reportValidity()` popup. This is often the root element or the inner\n * focus-delegated element.\n */\n [getValidityAnchor](): HTMLElement | null;\n}\n\n/**\n * A symbol property used to create a constraint validation `Validator`.\n * Required for all `mixinConstraintValidation()` elements.\n */\nexport const createValidator = Symbol('createValidator');\n\n/**\n * A symbol property used to return an anchor for constraint validation popups.\n * Required for all `mixinConstraintValidation()` elements.\n */\nexport const getValidityAnchor = Symbol('getValidityAnchor');\n\n// Private symbol members, used to avoid name clashing.\nconst privateValidator = Symbol('privateValidator');\nconst privateSyncValidity = Symbol('privateSyncValidity');\nconst privateCustomValidationMessage = Symbol('privateCustomValidationMessage');\n\n/**\n * Mixins in constraint validation APIs for an element.\n *\n * See https://developer.mozilla.org/en-US/docs/Web/HTML/Constraint_validation\n * for more details.\n *\n * Implementations must provide a validator to cache and compute its validity,\n * along with a shadow root element to anchor validation popups to.\n *\n * @example\n * ```ts\n * const baseClass = mixinConstraintValidation(\n * mixinFormAssociated(mixinElementInternals(LitElement))\n * );\n *\n * class MyCheckbox extends baseClass {\n * \\@property({type: Boolean}) checked = false;\n * \\@property({type: Boolean}) required = false;\n *\n * [createValidator]() {\n * return new CheckboxValidator(() => this);\n * }\n *\n * [getValidityAnchor]() {\n * return this.renderRoot.querySelector('.root');\n * }\n * }\n * ```\n *\n * @param base The class to mix functionality into.\n * @return The provided class with `ConstraintValidation` mixed in.\n */\nexport function mixinConstraintValidation<\n T extends MixinBase<LitElement & FormAssociated & WithElementInternals>,\n>(base: T): MixinReturn<T, ConstraintValidation> {\n abstract class ConstraintValidationElement\n extends base\n implements ConstraintValidation\n {\n get validity() {\n this[privateSyncValidity]();\n return this[internals].validity;\n }\n\n get validationMessage() {\n this[privateSyncValidity]();\n return this[internals].validationMessage;\n }\n\n get willValidate() {\n this[privateSyncValidity]();\n return this[internals].willValidate;\n }\n\n /**\n * A validator instance created from `[createValidator]()`.\n */\n [privateValidator]?: Validator<unknown>;\n\n /**\n * Needed for Safari, see https://bugs.webkit.org/show_bug.cgi?id=261432\n * Replace with this[internals].validity.customError when resolved.\n */\n [privateCustomValidationMessage] = '';\n\n checkValidity() {\n this[privateSyncValidity]();\n return this[internals].checkValidity();\n }\n\n reportValidity() {\n this[privateSyncValidity]();\n return this[internals].reportValidity();\n }\n\n setCustomValidity(error: string) {\n this[privateCustomValidationMessage] = error;\n this[privateSyncValidity]();\n }\n\n override requestUpdate(\n name?: PropertyKey,\n oldValue?: unknown,\n options?: PropertyDeclaration,\n ) {\n super.requestUpdate(name, oldValue, options);\n this[privateSyncValidity]();\n }\n\n [privateSyncValidity]() {\n if (isServer) {\n return;\n }\n\n if (!this[privateValidator]) {\n this[privateValidator] = this[createValidator]();\n }\n\n const {validity, validationMessage: nonCustomValidationMessage} =\n this[privateValidator].getValidity();\n\n const customError = !!this[privateCustomValidationMessage];\n const validationMessage =\n this[privateCustomValidationMessage] || nonCustomValidationMessage;\n\n this[internals].setValidity(\n {...validity, customError},\n validationMessage,\n this[getValidityAnchor]() ?? undefined,\n );\n }\n\n [createValidator](): Validator<unknown> {\n throw new Error('Implement [createValidator]');\n }\n\n [getValidityAnchor](): HTMLElement | null {\n throw new Error('Implement [getValidityAnchor]');\n }\n }\n\n return ConstraintValidationElement;\n}\n"]}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2023 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { Validator } from './validator.js';
|
|
7
|
+
/**
|
|
8
|
+
* Constraint validation properties for a checkbox.
|
|
9
|
+
*/
|
|
10
|
+
export interface CheckboxState {
|
|
11
|
+
/**
|
|
12
|
+
* Whether the checkbox is checked.
|
|
13
|
+
*/
|
|
14
|
+
checked: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Whether the checkbox is required.
|
|
17
|
+
*/
|
|
18
|
+
required: boolean;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* A validator that provides constraint validation that emulates
|
|
22
|
+
* `<input type="checkbox">` validation.
|
|
23
|
+
*/
|
|
24
|
+
export declare class CheckboxValidator extends Validator<CheckboxState> {
|
|
25
|
+
private checkboxControl?;
|
|
26
|
+
protected computeValidity(state: CheckboxState): {
|
|
27
|
+
validity: ValidityState;
|
|
28
|
+
validationMessage: string;
|
|
29
|
+
};
|
|
30
|
+
protected equals(prev: CheckboxState, next: CheckboxState): boolean;
|
|
31
|
+
protected copy({ checked, required }: CheckboxState): CheckboxState;
|
|
32
|
+
}
|