Skip to content

Commit 4ff5af5

Browse files
committed
Simplify validation state auto-tracking to just consume the value instead of listening to events
1 parent c40ffaf commit 4ff5af5

File tree

2 files changed

+32
-20
lines changed
  • packages

2 files changed

+32
-20
lines changed
Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import BaseBsFormElement from 'ember-bootstrap/components/bs-form/element';
2-
import { action } from '@ember/object';
3-
import { tracked } from '@glimmer/tracking';
42
import { assert } from '@ember/debug';
53

64
export default class BsFormElement extends BaseBsFormElement {
@@ -19,11 +17,10 @@ export default class BsFormElement extends BaseBsFormElement {
1917
return document.getElementById(this.formElementId);
2018
}
2119

22-
@tracked
23-
_invalidateErrors = 0;
24-
2520
get errors() {
26-
let { controlElement, _invalidateErrors } = this;
21+
// native validation state doesn't integrate with Ember's autotracking, so we need to invalidate our `errors` getter explicitly when
22+
// `this.value` changes by consuming it here.
23+
let { controlElement, value } = this;
2724
return !controlElement || controlElement.validity.valid
2825
? []
2926
: [controlElement.validationMessage];
@@ -32,17 +29,4 @@ export default class BsFormElement extends BaseBsFormElement {
3229
get hasValidator() {
3330
return true;
3431
}
35-
36-
@action
37-
showValidationOnHandler(event) {
38-
// native validation state doesn't integrate with Ember's autotracking, so we need to invalidate our `errors` getter explicitly when an
39-
// event occurs that could change the validation state
40-
41-
if (event.type !== 'focusout') {
42-
// ignore focusout event, as this won't be able to change the validation state, but mutating _invalidateErrors can cause "same computation" assertions
43-
this._invalidateErrors++;
44-
}
45-
46-
super.showValidationOnHandler(event);
47-
}
4832
}

packages/test-app/tests/integration/components/bs-form-test.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,35 @@ module('Integration | Component | bs-form', function (hooks) {
153153
await blur('input');
154154
assert
155155
.dom('input')
156-
.hasClass('is-valid', 'validation error is shown after focus out');
156+
.hasClass('is-valid', 'validation success is shown after focus out');
157+
});
158+
159+
test('validation state is updated while typing', async function (assert) {
160+
this.set('model', { name: '' });
161+
162+
await render(hbs`
163+
<BsForm @model={{this.model}} as |form|>
164+
<form.element @controlType="email" @label="Name" @property="name" as |el|>
165+
<el.control required/>
166+
</form.element>
167+
</BsForm>
168+
`);
169+
assert.dom('input').doesNotHaveClass('is-valid');
170+
171+
await focus('input');
172+
await blur('input');
173+
174+
await fillIn('input', 'abc');
175+
176+
assert
177+
.dom('input')
178+
.hasClass('is-invalid', 'validation error is shown after focus out');
179+
180+
await fillIn('input', 'abc@example.com');
181+
182+
assert
183+
.dom('input')
184+
.hasClass('is-valid', 'validation success is shown after focus out');
157185
});
158186

159187
test('does not throw when control element is not available', async function (assert) {

0 commit comments

Comments
 (0)