-
Notifications
You must be signed in to change notification settings - Fork 904
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(radio): add required constraint validation
Fixes #4316 PiperOrigin-RevId: 585713253
- Loading branch information
1 parent
21432b3
commit ba8b72a
Showing
5 changed files
with
220 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/** | ||
* @license | ||
* Copyright 2023 Google LLC | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import {html, render} from 'lit'; | ||
|
||
import {Validator} from './validator.js'; | ||
|
||
/** | ||
* Constraint validation properties for a radio. | ||
*/ | ||
export interface RadioState { | ||
/** | ||
* Whether the radio is checked. | ||
*/ | ||
readonly checked: boolean; | ||
|
||
/** | ||
* Whether the radio is required. | ||
*/ | ||
readonly required: boolean; | ||
} | ||
|
||
/** | ||
* Radio constraint validation properties for a single radio and its siblings. | ||
*/ | ||
export type RadioGroupState = readonly [RadioState, ...RadioState[]]; | ||
|
||
/** | ||
* A validator that provides constraint validation that emulates | ||
* `<input type="radio">` validation. | ||
*/ | ||
export class RadioValidator extends Validator<RadioGroupState> { | ||
private root?: HTMLElement; | ||
|
||
protected override computeValidity(states: RadioGroupState) { | ||
if (!this.root) { | ||
// Lazily create the container element | ||
this.root = document.createElement('div'); | ||
} | ||
|
||
render( | ||
states.map( | ||
(state) => | ||
html`<input | ||
type="radio" | ||
name="group" | ||
.checked=${state.checked} | ||
?required=${state.required} />`, | ||
), | ||
this.root, | ||
); | ||
|
||
const control = this.root.firstElementChild as HTMLInputElement; | ||
return { | ||
validity: control.validity, | ||
validationMessage: control.validationMessage, | ||
}; | ||
} | ||
|
||
protected override equals( | ||
prevGroup: RadioGroupState, | ||
nextGroup: RadioGroupState, | ||
) { | ||
if (prevGroup.length !== nextGroup.length) { | ||
return false; | ||
} | ||
|
||
for (let i = 0; i < prevGroup.length; i++) { | ||
const prev = prevGroup[i]; | ||
const next = nextGroup[i]; | ||
if (prev.checked !== next.checked || prev.required !== next.required) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
protected override copy(states: RadioGroupState): RadioGroupState { | ||
// Cast as unknown since typescript does not have enough information to | ||
// infer that the array always has at least one element. | ||
return states.map(({checked, required}) => ({ | ||
checked, | ||
required, | ||
})) as unknown as RadioGroupState; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* @license | ||
* Copyright 2023 Google LLC | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
// import 'jasmine'; (google3-only) | ||
|
||
import {RadioValidator} from './radio-validator.js'; | ||
|
||
describe('RadioValidator', () => { | ||
it('is invalid when required and no radios are checked', () => { | ||
const states = [ | ||
{ | ||
required: true, | ||
checked: false, | ||
}, | ||
{ | ||
required: true, | ||
checked: false, | ||
}, | ||
{ | ||
required: true, | ||
checked: false, | ||
}, | ||
] as const; | ||
|
||
const validator = new RadioValidator(() => states); | ||
const {validity, validationMessage} = validator.getValidity(); | ||
expect(validity.valueMissing).withContext('valueMissing').toBeTrue(); | ||
expect(validationMessage).withContext('validationMessage').not.toBe(''); | ||
}); | ||
|
||
it('is valid when required and any radio is checked', () => { | ||
const states = [ | ||
{ | ||
required: true, | ||
checked: false, | ||
}, | ||
{ | ||
required: true, | ||
checked: true, | ||
}, | ||
{ | ||
required: true, | ||
checked: false, | ||
}, | ||
] as const; | ||
|
||
const validator = new RadioValidator(() => states); | ||
const {validity, validationMessage} = validator.getValidity(); | ||
expect(validity.valueMissing).withContext('valueMissing').toBeFalse(); | ||
expect(validationMessage).withContext('validationMessage').toBe(''); | ||
}); | ||
|
||
it('is valid when not required', () => { | ||
const states = [ | ||
{ | ||
required: false, | ||
checked: false, | ||
}, | ||
{ | ||
required: false, | ||
checked: false, | ||
}, | ||
{ | ||
required: false, | ||
checked: false, | ||
}, | ||
] as const; | ||
|
||
const validator = new RadioValidator(() => states); | ||
const {validity, validationMessage} = validator.getValidity(); | ||
expect(validity.valueMissing).withContext('valueMissing').toBeFalse(); | ||
expect(validationMessage).withContext('validationMessage').toBe(''); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters