Skip to content

Commit 1da18a0

Browse files
authored
Fix TextInput validity when value reset outside of DOM/onInput (#771)
1 parent 686a998 commit 1da18a0

File tree

5 files changed

+213
-103
lines changed

5 files changed

+213
-103
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ yarn.lock
1919
deploy_key
2020
deploy_key.pub
2121
package.json.bk
22+
/.vscode

src/common/InputValidity.ts

-27
This file was deleted.

src/text-input/example/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export default class App extends WidgetBase {
1111
private _value6Valid!: boolean | { valid?: boolean; message?: string };
1212

1313
render() {
14+
console.log(this._value6, this._value6Valid);
1415
return v(
1516
'div',
1617
{

src/text-input/index.ts

+10-22
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { DNode, PropertyChangeRecord } from '@dojo/framework/core/interfaces';
33
import { ThemedMixin, ThemedProperties, theme } from '@dojo/framework/core/mixins/Themed';
44
import { v, w } from '@dojo/framework/core/vdom';
55
import Focus from '@dojo/framework/core/meta/Focus';
6+
import InputValidity from '@dojo/framework/core/meta/InputValidity';
67
import { FocusMixin, FocusProperties } from '@dojo/framework/core/mixins/Focus';
78
import Label from '../label/index';
89
import {
@@ -17,7 +18,6 @@ import * as css from '../theme/text-input.m.css';
1718
import { customElement } from '@dojo/framework/core/decorators/customElement';
1819
import diffProperty from '@dojo/framework/core/decorators/diffProperty';
1920
import { reference } from '@dojo/framework/core/diff';
20-
import InputValidity from '../common/InputValidity';
2121
import HelperText from '../helper-text/index';
2222

2323
export type TextInputType =
@@ -31,9 +31,7 @@ export type TextInputType =
3131
| 'date';
3232

3333
interface TextInputInternalState {
34-
previousValue?: string;
35-
previousValid?: boolean;
36-
previousMessage?: string;
34+
dirty?: boolean;
3735
}
3836

3937
/**
@@ -155,6 +153,7 @@ export class TextInput extends ThemedMixin(FocusMixin(WidgetBase))<TextInputProp
155153
}
156154
private _onChange(event: Event) {
157155
event.stopPropagation();
156+
this._state.dirty = true;
158157
this.properties.onChange &&
159158
this.properties.onChange((event.target as HTMLInputElement).value);
160159
}
@@ -215,35 +214,23 @@ export class TextInput extends ThemedMixin(FocusMixin(WidgetBase))<TextInputProp
215214
}
216215

217216
private _validate() {
218-
const {
219-
_state: state,
220-
properties: { onValidate, value, customValidator }
221-
} = this;
222-
223-
if (!onValidate || value === undefined || value === null || state.previousValue === value) {
217+
const { customValidator, onValidate, value = '' } = this.properties;
218+
const { dirty = false } = this._state;
219+
if (value === '' && !dirty) {
220+
onValidate && onValidate(undefined, '');
224221
return;
225222
}
226223

227-
state.previousValue = value;
228-
224+
this._state.dirty = true;
229225
let { valid, message = '' } = this.meta(InputValidity).get('input', value);
230-
231226
if (valid && customValidator) {
232227
const customValid = customValidator(value);
233228
if (customValid) {
234229
valid = customValid.valid;
235230
message = customValid.message || '';
236231
}
237232
}
238-
239-
if (valid === state.previousValid && message === state.previousMessage) {
240-
return;
241-
}
242-
243-
state.previousValid = valid;
244-
state.previousMessage = message;
245-
246-
onValidate(valid, message);
233+
onValidate && onValidate(valid, message);
247234
}
248235

249236
protected get validity() {
@@ -305,6 +292,7 @@ export class TextInput extends ThemedMixin(FocusMixin(WidgetBase))<TextInputProp
305292

306293
this._validate();
307294
const { valid, message } = this.validity;
295+
308296
const focus = this.meta(Focus).get('root');
309297

310298
const computedHelperText = (valid === false && message) || helperText;

0 commit comments

Comments
 (0)