Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(radio-group): value handling #33362

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "fix radio/group value handling",
"packageName": "@fluentui/web-components",
"email": "[email protected]",
"dependentChangeType": "patch"
}
8 changes: 4 additions & 4 deletions packages/web-components/src/checkbox/checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export class BaseCheckbox extends FASTElement {
*
* @internal
*/
private _validationFallbackMessage: string = '';
static _validationFallbackMessage: string = '';

/**
* The validation message. Uses the browser's default validation message for native checkboxes if not otherwise
Expand All @@ -235,16 +235,16 @@ export class BaseCheckbox extends FASTElement {
return this.elementInternals.validationMessage;
}

if (!this._validationFallbackMessage) {
if (!Checkbox._validationFallbackMessage) {
const validationMessageFallbackControl = document.createElement('input');
validationMessageFallbackControl.type = 'checkbox';
validationMessageFallbackControl.required = true;
validationMessageFallbackControl.checked = false;

this._validationFallbackMessage = validationMessageFallbackControl.validationMessage;
Checkbox._validationFallbackMessage = validationMessageFallbackControl.validationMessage;
}

return this._validationFallbackMessage;
return Checkbox._validationFallbackMessage;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function radioGroupTemplate<T extends RadioGroup>(): ElementViewTemplate<
@focusout="${(x, c) => x.focusoutHandler(c.event as FocusEvent)}"
@keydown="${(x, c) => x.keydownHandler(c.event as KeyboardEvent)}"
>
<slot @slotchange="${(x, c) => x.slotchangeHandler(c.event as Event)}"></slot>
<slot @slotchange="${(x, c) => x.slotchangeHandler()}"></slot>
</template>
`;
}
Expand Down
21 changes: 11 additions & 10 deletions packages/web-components/src/radio-group/radio-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class RadioGroup extends FASTElement {
return;
}

this.checkRadio(next);
this.enabledRadios[next]?.click();
}

/**
Expand Down Expand Up @@ -246,7 +246,7 @@ export class RadioGroup extends FASTElement {
*
* @internal
*/
private _validationFallbackMessage!: string;
private static _validationFallbackMessage: string = '';

/**
* The validation message. Uses the browser's default validation message for native checkboxes if not otherwise
Expand All @@ -263,16 +263,17 @@ export class RadioGroup extends FASTElement {
return this.enabledRadios[0].validationMessage;
}

if (!this._validationFallbackMessage) {
if (!RadioGroup._validationFallbackMessage) {
const validationMessageFallbackControl = document.createElement('input');
validationMessageFallbackControl.type = 'radio';
validationMessageFallbackControl.required = true;
validationMessageFallbackControl.checked = false;
validationMessageFallbackControl.name = '##--ThisShouldBeSufficientForATemporaryString--##';

this._validationFallbackMessage = validationMessageFallbackControl.validationMessage;
RadioGroup._validationFallbackMessage = validationMessageFallbackControl.validationMessage;
}

return this._validationFallbackMessage;
return RadioGroup._validationFallbackMessage;
}

/**
Expand Down Expand Up @@ -470,7 +471,7 @@ export class RadioGroup extends FASTElement {
}

case ' ': {
this.checkRadio();
this.enabledRadios[this.checkedIndex]?.click();
break;
}
}
Expand Down Expand Up @@ -564,9 +565,9 @@ export class RadioGroup extends FASTElement {
* @param e - the slot change event
* @internal
*/
public slotchangeHandler(e: Event): void {
Updates.enqueue(() => {
this.radios = [...this.querySelectorAll('*')].filter(x => x instanceof Radio) as Radio[];
});
public slotchangeHandler(): void {
this.radios = [...this.querySelectorAll('*')].filter(x => x instanceof Radio) as Radio[];
/** this looks silly but is needed to trigger the setter updates (form value + checkedIndex) */
this.value = this.value; // eslint-disable-line no-self-assign
}
}
35 changes: 35 additions & 0 deletions packages/web-components/src/radio/radio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,39 @@ export class Radio extends BaseCheckbox {
public toggleChecked(force: boolean = true): void {
super.toggleChecked(force);
}

/**
* The fallback validation message, taken from a native checkbox `<input>` element.
*
* @internal
*/
static override _validationFallbackMessage: string = '';

/**
* The validation message. Uses the browser's default validation message for native checkboxes if not otherwise
* specified (e.g., via `setCustomValidity`).
*
* @public
* @remarks
* Reflects the
* {@link https://developer.mozilla.org/docs/Web/API/ElementInternals/validationMessage | `ElementInternals.validationMessage`}
* property.
*/
public override get validationMessage(): string {
if (this.elementInternals.validationMessage) {
return this.elementInternals.validationMessage;
}

if (!Radio._validationFallbackMessage) {
const validationMessageFallbackControl = document.createElement('input');
validationMessageFallbackControl.type = 'radio';
validationMessageFallbackControl.required = true;
validationMessageFallbackControl.checked = false;
validationMessageFallbackControl.name = '##--ThisShouldBeSufficientForATemporaryString--##';

Radio._validationFallbackMessage = validationMessageFallbackControl.validationMessage;
}

return Radio._validationFallbackMessage;
}
}
Loading