Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 14 additions & 23 deletions packages/uui-base/lib/mixins/FormControlMixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,25 +164,10 @@ export const UUIFormControlMixin = <
* @attr
* @default true
*/
@property({ type: Boolean, reflect: true, attribute: 'pristine' })
public set pristine(value: boolean) {
if (this._pristine !== value) {
this._pristine = value;
// I have trouble with the reflect option on this one, maybe reflect does not work from mixins? [NL]
if (value) {
this.setAttribute('pristine', '');
} else {
this.removeAttribute('pristine');
}
this.#dispatchValidationState();
}
}
public get pristine(): boolean {
return this._pristine;
}
// Will be set to true instantly to trigger the setAttribute in the setter.
// This is to prevent an issue caused by using setAttribute in the constructor.
private _pristine: boolean = false;
@property({ type: Boolean, reflect: true })
pristine = true;

private _hasConnected = false;

/**
* Apply validation rule for requiring a value of this form control.
Expand Down Expand Up @@ -223,7 +208,6 @@ export const UUIFormControlMixin = <
constructor(...args: any[]) {
super(...args);
this._internals = this.attachInternals();
this.pristine = true;

this.addValidator(
'valueMissing',
Expand Down Expand Up @@ -280,6 +264,13 @@ export const UUIFormControlMixin = <
}
}

connectedCallback() {
super.connectedCallback();

this._hasConnected = true;
this.pristine = true;
}

disconnectedCallback(): void {
super.disconnectedCallback();
this.#removeFormListeners();
Expand Down Expand Up @@ -345,7 +336,7 @@ export const UUIFormControlMixin = <
this._runValidators();
});
// If we are in validationMode/'touched'/not-pristine then we need to validate this newly added control. [NL]
if (this._pristine === false) {
if (this.pristine === false) {
element.checkValidity();
// I think we could just execute validators for the new control, but now lets just run al of it again. [NL]
this._runValidators();
Expand Down Expand Up @@ -436,8 +427,8 @@ export const UUIFormControlMixin = <
}

#dispatchValidationState() {
// Do not fire validation events unless we are not pristine/'untouched'/not-in-validation-mode. [NL]
if (this._pristine === true) return;
if (!this._hasConnected) return;
if (this.pristine) return;
if (this.#validity.valid) {
this.dispatchEvent(new UUIFormControlEvent(UUIFormControlEvent.VALID));
} else {
Expand Down
Loading