-
Notifications
You must be signed in to change notification settings - Fork 931
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(select): use shared validation mixins
PiperOrigin-RevId: 585751913
- Loading branch information
1 parent
bedcd65
commit 33c1afe
Showing
6 changed files
with
131 additions
and
175 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,52 @@ | ||
/** | ||
* @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 select dropdown. | ||
*/ | ||
export interface SelectState { | ||
/** | ||
* The current selected value. | ||
*/ | ||
readonly value: string; | ||
|
||
/** | ||
* Whether the select is required. | ||
*/ | ||
readonly required: boolean; | ||
} | ||
|
||
/** | ||
* A validator that provides constraint validation that emulates `<select>` | ||
* validation. | ||
*/ | ||
export class SelectValidator extends Validator<SelectState> { | ||
private selectControl?: HTMLSelectElement; | ||
|
||
protected override computeValidity(state: SelectState) { | ||
if (!this.selectControl) { | ||
// Lazily create the platform select | ||
this.selectControl = document.createElement('select'); | ||
} | ||
|
||
render(html`<option value=${state.value}></option>`, this.selectControl); | ||
|
||
this.selectControl.value = state.value; | ||
this.selectControl.required = state.required; | ||
return { | ||
validity: this.selectControl.validity, | ||
validationMessage: this.selectControl.validationMessage, | ||
}; | ||
} | ||
|
||
protected override equals(prev: SelectState, next: SelectState) { | ||
return prev.value === next.value && prev.required === next.required; | ||
} | ||
} |
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,47 @@ | ||
/** | ||
* @license | ||
* Copyright 2023 Google LLC | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
// import 'jasmine'; (google3-only) | ||
|
||
import {SelectValidator} from './select-validator.js'; | ||
|
||
describe('SelectValidator', () => { | ||
it('is invalid when required and value is empty', () => { | ||
const state = { | ||
required: true, | ||
value: '', | ||
}; | ||
|
||
const validator = new SelectValidator(() => state); | ||
const {validity, validationMessage} = validator.getValidity(); | ||
expect(validity.valueMissing).withContext('valueMissing').toBeTrue(); | ||
expect(validationMessage).withContext('validationMessage').not.toBe(''); | ||
}); | ||
|
||
it('is valid when required and value is provided', () => { | ||
const state = { | ||
required: true, | ||
value: 'Foo', | ||
}; | ||
|
||
const validator = new SelectValidator(() => state); | ||
const {validity, validationMessage} = validator.getValidity(); | ||
expect(validity.valueMissing).withContext('valueMissing').toBeFalse(); | ||
expect(validationMessage).withContext('validationMessage').toBe(''); | ||
}); | ||
|
||
it('is valid when not required', () => { | ||
const state = { | ||
required: false, | ||
value: '', | ||
}; | ||
|
||
const validator = new SelectValidator(() => state); | ||
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
Oops, something went wrong.