fix(field): do not validate disabled fields (#586)
This commit is contained in:
parent
79c51767af
commit
9127f08440
4 changed files with 106 additions and 4 deletions
|
|
@ -125,10 +125,9 @@ export class LionField extends FormControlMixin(
|
||||||
updated(changedProps) {
|
updated(changedProps) {
|
||||||
super.updated(changedProps);
|
super.updated(changedProps);
|
||||||
|
|
||||||
if (changedProps.has('disabled') && this.disabled) {
|
if (changedProps.has('disabled')) {
|
||||||
this._inputNode.disabled = true;
|
this._inputNode.disabled = this.disabled;
|
||||||
} else if (changedProps.has('disabled')) {
|
this.validate();
|
||||||
this._inputNode.disabled = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (changedProps.has('name')) {
|
if (changedProps.has('name')) {
|
||||||
|
|
|
||||||
|
|
@ -359,6 +359,42 @@ describe('<lion-field>', () => {
|
||||||
wantedShowsFeedbackFor: [],
|
wantedShowsFeedbackFor: [],
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
it('should not run validation when disabled', async () => {
|
||||||
|
const HasX = class extends Validator {
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
this.name = 'HasX';
|
||||||
|
}
|
||||||
|
|
||||||
|
execute(value) {
|
||||||
|
const result = value.indexOf('x') === -1;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const disabledEl = await fixture(html`
|
||||||
|
<${tag}
|
||||||
|
disabled
|
||||||
|
.validators=${[new HasX()]}
|
||||||
|
.modelValue=${'a@b.nl'}
|
||||||
|
>
|
||||||
|
${inputSlot}
|
||||||
|
</${tag}>
|
||||||
|
`);
|
||||||
|
const el = await fixture(html`
|
||||||
|
<${tag}
|
||||||
|
.validators=${[new HasX()]}
|
||||||
|
.modelValue=${'a@b.nl'}
|
||||||
|
>
|
||||||
|
${inputSlot}
|
||||||
|
</${tag}>
|
||||||
|
`);
|
||||||
|
|
||||||
|
expect(el.hasFeedbackFor).to.deep.equal(['error']);
|
||||||
|
expect(el.validationStates.error).to.have.a.property('HasX');
|
||||||
|
|
||||||
|
expect(disabledEl.hasFeedbackFor).to.deep.equal([]);
|
||||||
|
expect(disabledEl.validationStates.error).to.equal(undefined);
|
||||||
|
});
|
||||||
|
|
||||||
it('can be required', async () => {
|
it('can be required', async () => {
|
||||||
const el = await fixture(html`
|
const el = await fixture(html`
|
||||||
|
|
|
||||||
|
|
@ -615,3 +615,64 @@ const minDateValidatorRef = new MinDate(beginDate, {
|
||||||
.validators="${[minDateValidatorRef]}"
|
.validators="${[minDateValidatorRef]}"
|
||||||
></lion-input-date>
|
></lion-input-date>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Disabled inputs validation
|
||||||
|
|
||||||
|
According to the W3C specs, Disabled fields should not be validated.
|
||||||
|
Therefor if the attribute disabled is present on a lion-input it will not be validated.
|
||||||
|
|
||||||
|
<Story name="Disabled Validators">
|
||||||
|
{html`
|
||||||
|
<lion-input
|
||||||
|
disabled
|
||||||
|
.validators=${[new EqualsLength(7)]}
|
||||||
|
.modelValue=${'not exactly'}
|
||||||
|
label="EqualsLength"
|
||||||
|
></lion-input>
|
||||||
|
<lion-input
|
||||||
|
disabled
|
||||||
|
.validators=${[new MinLength(10)]}
|
||||||
|
.modelValue=${'too short'}
|
||||||
|
label="MinLength"
|
||||||
|
></lion-input>
|
||||||
|
<lion-input
|
||||||
|
disabled
|
||||||
|
.validators=${[new MaxLength(7)]}
|
||||||
|
.modelValue=${'too long'}
|
||||||
|
label="MaxLength"
|
||||||
|
></lion-input>
|
||||||
|
<lion-input
|
||||||
|
disabled
|
||||||
|
.validators=${[new MinMaxLength({ min: 10, max: 20 })]}
|
||||||
|
.modelValue=${'that should be enough'}
|
||||||
|
label="MinMaxLength"
|
||||||
|
></lion-input>
|
||||||
|
`}
|
||||||
|
</Story>
|
||||||
|
|
||||||
|
```html
|
||||||
|
<lion-input
|
||||||
|
disabled
|
||||||
|
.validators=${[new EqualsLength(7)]}
|
||||||
|
.modelValue=${'not exactly'}
|
||||||
|
label="EqualsLength"
|
||||||
|
></lion-input>
|
||||||
|
<lion-input
|
||||||
|
disabled
|
||||||
|
.validators=${[new MinLength(10)]}
|
||||||
|
.modelValue=${'too short'}
|
||||||
|
label="MinLength"
|
||||||
|
></lion-input>
|
||||||
|
<lion-input
|
||||||
|
disabled
|
||||||
|
.validators=${[new MaxLength(7)]}
|
||||||
|
.modelValue=${'too long'}
|
||||||
|
label="MaxLength"
|
||||||
|
></lion-input>
|
||||||
|
<lion-input
|
||||||
|
disabled
|
||||||
|
.validators=${[new MinMaxLength({ min: 10, max: 20 })]}
|
||||||
|
.modelValue=${'that should be enough'}
|
||||||
|
label="MinMaxLength"
|
||||||
|
></lion-input>
|
||||||
|
```
|
||||||
|
|
|
||||||
|
|
@ -261,6 +261,12 @@ export const ValidateMixin = dedupeMixin(
|
||||||
* call. Situation B will occur after every call.
|
* call. Situation B will occur after every call.
|
||||||
*/
|
*/
|
||||||
async validate({ clearCurrentResult } = {}) {
|
async validate({ clearCurrentResult } = {}) {
|
||||||
|
if (this.disabled) {
|
||||||
|
this.__clearValidationResults();
|
||||||
|
this.__validationResult = [];
|
||||||
|
this._updateFeedbackComponent();
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (!this.__validateInitialized) {
|
if (!this.__validateInitialized) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue