Skip to content

Commit 7cff30a

Browse files
authored
fix(web-components): text-input setValidity fails if control is not available (#34372)
1 parent a275842 commit 7cff30a

3 files changed

Lines changed: 29 additions & 7 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "prerelease",
3+
"comment": "fix: text-input setValidity fails if control is not available",
4+
"packageName": "@fluentui/web-components",
5+
"email": "863023+radium-v@users.noreply.github.com",
6+
"dependentChangeType": "patch"
7+
}

packages/web-components/docs/web-components.api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,8 @@ export class BaseTextInput extends FASTElement {
868868
// @internal
869869
control: HTMLInputElement;
870870
// @internal
871+
controlChanged(prev: HTMLInputElement | undefined, next: HTMLInputElement | undefined): void;
872+
// @internal
871873
controlLabel: HTMLLabelElement;
872874
currentValue: string;
873875
// @internal

packages/web-components/src/text-input/text-input.base.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,21 @@ export class BaseTextInput extends FASTElement {
292292
*
293293
* @internal
294294
*/
295+
@observable
295296
public control!: HTMLInputElement;
296297

298+
/**
299+
* Calls the `setValidity` method when the control reference changes.
300+
*
301+
* @param prev - the previous control reference
302+
* @param next - the current control reference
303+
*
304+
* @internal
305+
*/
306+
public controlChanged(prev: HTMLInputElement | undefined, next: HTMLInputElement | undefined): void {
307+
this.setValidity();
308+
}
309+
297310
/**
298311
* A reference to the internal label element.
299312
*
@@ -589,18 +602,18 @@ export class BaseTextInput extends FASTElement {
589602
*
590603
* @internal
591604
*/
592-
public setValidity(
593-
flags: Partial<ValidityState> = this.control.validity,
594-
message: string = this.validationMessage,
595-
anchor: HTMLElement = this.control,
596-
): void {
597-
if (this.$fastController.isConnected) {
605+
public setValidity(flags?: Partial<ValidityState>, message?: string, anchor?: HTMLElement): void {
606+
if (this.$fastController.isConnected && this.control) {
598607
if (this.disabled) {
599608
this.elementInternals.setValidity({});
600609
return;
601610
}
602611

603-
this.elementInternals.setValidity(flags, message, anchor);
612+
this.elementInternals.setValidity(
613+
flags ?? this.control.validity,
614+
message ?? this.validationMessage,
615+
anchor ?? this.control,
616+
);
604617
}
605618
}
606619
}

0 commit comments

Comments
 (0)