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(foundation): remove readonly from radio button #6438

Merged
merged 25 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
55944a9
fix(foundation): remove readonly from radio button
yinonov Oct 9, 2022
f68c163
remove any evidence of readonly from class
yinonov Oct 9, 2022
1b3d8d3
completely remove any readonly
yinonov Oct 9, 2022
4e7649a
Change files
yinonov Oct 9, 2022
cb14131
group slotted radio readonly handling
yinonov Oct 9, 2022
aad8b96
docs update
yinonov Oct 9, 2022
2471b58
Update change/@microsoft-fast-foundation-b9885563-e3e8-4c85-a55d-bac0…
yinonov Oct 10, 2022
d0e4dca
Update change/@microsoft-fast-foundation-b9885563-e3e8-4c85-a55d-bac0…
yinonov Oct 10, 2022
273912d
Merge branch 'master' into patch-5
yinonov Oct 11, 2022
11ee029
prevent check of radio buttons
yinonov Oct 11, 2022
10ec675
Merge branch 'master' into patch-5
yinonov Oct 12, 2022
be9ca1e
Update packages/web-components/fast-foundation/src/radio/radio.ts
yinonov Oct 12, 2022
07edb70
Merge branch 'master' into patch-5
yinonov Oct 20, 2022
a05a6c9
Merge branch 'master' into patch-5
yinonov Oct 21, 2022
56ae8a3
Merge branch 'master' into patch-5
yinonov Oct 25, 2022
de8dfe1
Merge branch 'master' into patch-5
EisenbergEffect Oct 25, 2022
f75cdca
Merge branch 'master' into patch-5
yinonov Nov 21, 2022
cafc397
Merge branch 'master' into patch-5
yinonov Feb 19, 2023
2ba7924
revert
Feb 19, 2023
f0c5760
remove unused
Feb 23, 2023
174a2cb
Merge branch 'master' into patch-5
chrisdholt Mar 7, 2023
5c72be4
FASTRadioGroup to be used as type only
Mar 7, 2023
2a0d518
fixup prettier and API report
chrisdholt Mar 10, 2023
e1f5b53
Merge pull request #1 from microsoft/users/chhol/fixup-yinonov-patch-…
yinonov Mar 10, 2023
86551be
Merge branch 'master' into patch-5
chrisdholt Mar 10, 2023
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "remove readonly support from fast radio",
"packageName": "@microsoft/fast-foundation",
"email": "[email protected]",
"dependentChangeType": "prerelease"
}
Original file line number Diff line number Diff line change
Expand Up @@ -1610,9 +1610,6 @@ export class FASTRadio extends FormAssociatedRadio implements RadioControl {
// @beta
keypressHandler(e: KeyboardEvent): boolean | void;
name: string;
readOnly: boolean;
// (undocumented)
protected readOnlyChanged(): void;
}

// @public
Expand All @@ -1637,8 +1634,6 @@ export class FASTRadioGroup extends FASTElement {
protected nameChanged(): void;
orientation: Orientation | "horizontal" | "vertical";
readOnly: boolean;
// (undocumented)
protected readOnlyChanged(): void;
// @internal (undocumented)
slottedRadioButtons: HTMLElement[];
// (undocumented)
Expand Down Expand Up @@ -2528,7 +2523,7 @@ export interface PropertyTarget {
export type ProxyElement = HTMLSelectElement | HTMLTextAreaElement | HTMLInputElement;

// @public
export type RadioControl = Pick<HTMLInputElement, "checked" | "disabled" | "readOnly" | "focus" | "setAttribute" | "getAttribute">;
export type RadioControl = Pick<HTMLInputElement, "checked" | "disabled" | "focus" | "setAttribute" | "getAttribute">;

// @public
export function radioGroupTemplate<T extends FASTRadioGroup>(): ElementViewTemplate<T>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ export const myRadioGroup = RadioGroup.compose({

| Name | Privacy | Description | Parameters | Return | Inherited From |
| ---------------------------- | --------- | ----------- | -------------------------------------------- | ------ | -------------- |
| `readOnlyChanged` | protected | | | `void` | |
| `disabledChanged` | protected | | | `void` | |
| `nameChanged` | protected | | | `void` | |
| `valueChanged` | protected | | | `void` | |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,32 +162,6 @@ test.describe("Radio Group", () => {
).toBeTruthy();
});

test("should set all child radio elements to readonly when the `readonly` property is true", async () => {
await root.evaluate(node => {
node.innerHTML = /* html */ `
<fast-radio-group readonly>
<fast-radio></fast-radio>
<fast-radio></fast-radio>
<fast-radio></fast-radio>
</fast-radio-group>
`;
});

await expect(element).toHaveBooleanAttribute("readonly");

expect(
await radios.evaluateAll(radios =>
radios.every(radio => radio.hasAttribute("readonly"))
)
).toBeTruthy();

expect(
await radios.evaluateAll(radios =>
radios.every(radio => radio.getAttribute("aria-readonly") === "true")
)
).toBeTruthy();
});

test("should set tabindex of 0 to a child radio with a matching `value`", async () => {
await root.evaluate(node => {
node.innerHTML = /* html */ `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,6 @@ export class FASTRadioGroup extends FASTElement {
*/
@attr({ attribute: "readonly", mode: "boolean" })
public readOnly: boolean;
protected readOnlyChanged(): void {
if (this.slottedRadioButtons !== undefined) {
this.slottedRadioButtons.forEach((radio: HTMLInputElement) => {
if (this.readOnly) {
radio.readOnly = true;
} else {
radio.readOnly = false;
}
});
}
}

/**
* Disables the radio group and child radios.
Expand Down Expand Up @@ -250,16 +239,10 @@ export class FASTRadioGroup extends FASTElement {
const radio: HTMLInputElement = group[index] as HTMLInputElement;
if (!this.isInsideToolbar) {
radio.setAttribute("tabindex", "0");
if (radio.readOnly) {
this.slottedRadioButtons.forEach((nextRadio: HTMLInputElement) => {
if (nextRadio !== radio) {
nextRadio.setAttribute("tabindex", "-1");
}
});
} else {
if (!this.readOnly) {
radio.checked = true;
this.selectedRadio = radio;
}
this.selectedRadio = radio;
}
this.focusedRadio = radio;
radio.focus();
Expand Down
24 changes: 8 additions & 16 deletions packages/web-components/fast-foundation/src/radio/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,32 +113,24 @@ export const myRadio = Radio.compose<RadioOptions>({

#### Fields

| Name | Privacy | Type | Default | Description | Inherited From |
| ---------- | ------- | --------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------- |
| `readOnly` | public | `boolean` | | When true, the control will be immutable by user interaction. See [readonly HTML attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/readonly) for more information. | |
| `name` | public | `string` | | The name of the radio. See [name attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#htmlattrdefname) for more info. | |
| `proxy` | | | | | FormAssociatedRadio |
| Name | Privacy | Type | Default | Description | Inherited From |
| ------- | ------- | -------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------- | ------------------- |
| `name` | public | `string` | | The name of the radio. See [name attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#htmlattrdefname) for more info. | |
| `proxy` | | | | | FormAssociatedRadio |

#### Methods

| Name | Privacy | Description | Parameters | Return | Inherited From |
| ----------------- | --------- | --------------------------------- | ------------------ | ----------------- | -------------- |
| `readOnlyChanged` | protected | | | `void` | |
| `keypressHandler` | public | Handles key presses on the radio. | `e: KeyboardEvent` | `boolean or void` | |
| `clickHandler` | public | Handles clicks on the radio. | `e: MouseEvent` | `boolean or void` | |
| Name | Privacy | Description | Parameters | Return | Inherited From |
| ----------------- | ------- | --------------------------------- | ------------------ | ----------------- | -------------- |
| `keypressHandler` | public | Handles key presses on the radio. | `e: KeyboardEvent` | `boolean or void` | |
| `clickHandler` | public | Handles clicks on the radio. | `e: MouseEvent` | `boolean or void` | |

#### Events

| Name | Type | Description | Inherited From |
| -------- | ---- | ---------------------------------------------------------- | -------------- |
| `change` | | Emits a custom change event when the checked state changes | |

#### Attributes

| Name | Field | Inherited From |
| ---------- | -------- | -------------- |
| `readonly` | readOnly | |

#### CSS Parts

| Name | Description |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,6 @@ test.describe("Radio", () => {
await element.evaluate((node: FASTRadio) => (node.disabled = false));

await expect(element).toHaveAttribute("aria-disabled", "false");

// Readonly
await expect(element).not.hasAttribute("aria-readonly");

await element.evaluate((node: FASTRadio) => (node.readOnly = true));

await expect(element).toHaveAttribute("aria-readonly", "true");

await element.evaluate((node: FASTRadio) => (node.readOnly = false));

await expect(element).toHaveAttribute("aria-readonly", "false");
});

test("should set a tabindex of 0 on the element", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ Extends [form associated custom element](../form-associated/form-associated-cust
- The current checked state of the radio

*Content attributes*
- `readonly`
- The radio should be submitted with the form but should not be editable.
- `disabled`
- The radio should be disabled from user interaction and will not be submitted with the form data.
- `value` - Not visible to the user, it's used for form data and to distinguish between other radio buttons of the same name attribute value.
Expand Down Expand Up @@ -72,7 +70,6 @@ Extends [form associated custom element](../form-associated/form-associated-cust
- checked
- disabled
- required
- readonly

*Slotted Content/Slotted Classes*
*CSS Parts*
Expand All @@ -89,9 +86,6 @@ The checked state can be toggled by:
**disabled**: `true` or `false`
When disabled, the value will not be changeable through user interaction. It should also not expose it's value to a form submission.

**readonly**: `true` or `false`
When readonly, the value will not be changeable through user interaction. The value will still be exposed to forms on submission.

### Accessibility
The root element inside the shadow-dom of the radio will be a focusable element with the following accessibility content attributes:
[MDN Web docs](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_radio_role)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export function radioTemplate<T extends FASTRadio>(
aria-checked="${x => x.checked}"
aria-required="${x => x.required}"
aria-disabled="${x => x.disabled}"
aria-readonly="${x => x.readOnly}"
@keypress="${(x, c) => x.keypressHandler(c.event as KeyboardEvent)}"
@click="${(x, c) => x.clickHandler(c.event as MouseEvent)}"
>
Expand Down
32 changes: 11 additions & 21 deletions packages/web-components/fast-foundation/src/radio/radio.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { attr, observable, SyntheticViewTemplate } from "@microsoft/fast-element";
import { keySpace } from "@microsoft/fast-web-utilities";
import type { FASTRadioGroup } from "src/radio-group/radio-group.js";
import { FormAssociatedRadio } from "./radio.form-associated.js";

/**
Expand All @@ -8,7 +9,7 @@ import { FormAssociatedRadio } from "./radio.form-associated.js";
*/
export type RadioControl = Pick<
HTMLInputElement,
"checked" | "disabled" | "readOnly" | "focus" | "setAttribute" | "getAttribute"
"checked" | "disabled" | "focus" | "setAttribute" | "getAttribute"
>;

/**
Expand All @@ -32,20 +33,6 @@ export type RadioOptions = {
* @public
*/
export class FASTRadio extends FormAssociatedRadio implements RadioControl {
/**
* When true, the control will be immutable by user interaction. See {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/readonly | readonly HTML attribute} for more information.
* @public
* @remarks
* HTML Attribute: readonly
*/
@attr({ attribute: "readonly", mode: "boolean" })
public readOnly: boolean; // Map to proxy element
protected readOnlyChanged(): void {
if (this.proxy instanceof HTMLInputElement) {
this.proxy.readOnly = this.readOnly;
}
}

/**
* The name of the radio. See {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#htmlattrdefname | name attribute} for more info.
*/
Expand All @@ -66,6 +53,12 @@ export class FASTRadio extends FormAssociatedRadio implements RadioControl {
@observable
public defaultSlottedNodes: Node[];

private get radioGroup() {
return (this as HTMLElement).closest(
"[role=radiogroup]"
) as FASTRadioGroup | null;
}

/**
* @internal
*/
Expand Down Expand Up @@ -116,10 +109,7 @@ export class FASTRadio extends FormAssociatedRadio implements RadioControl {
}

private isInsideRadioGroup(): boolean {
const parent: HTMLElement | null = (this as HTMLElement).closest(
"[role=radiogroup]"
);
return parent !== null;
return this.radioGroup !== null;
}

/**
Expand All @@ -129,7 +119,7 @@ export class FASTRadio extends FormAssociatedRadio implements RadioControl {
public keypressHandler(e: KeyboardEvent): boolean | void {
switch (e.key) {
case keySpace:
if (!this.checked && !this.readOnly) {
if (!this.checked && !this.radioGroup?.readOnly) {
this.checked = true;
}
return;
Expand All @@ -143,7 +133,7 @@ export class FASTRadio extends FormAssociatedRadio implements RadioControl {
* @beta
*/
public clickHandler(e: MouseEvent): boolean | void {
if (!this.disabled && !this.readOnly && !this.checked) {
if (!this.disabled && !this.radioGroup?.readOnly && !this.checked) {
this.checked = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ const styles = css`
}

:host([disabled]) .label,
:host([readonly]) .label,
:host([readonly]) .control,
:host([disabled]) .control {
cursor: not-allowed;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export const storyTemplate = html<StoryArgs<FASTRadio>>`
?checked="${x => x.checked}"
?disabled="${x => x.disabled}"
?required="${x => x.required}"
?readonly="${x => x.readOnly}"
name="${x => x.name}"
value="${x => x.value}"
>
Expand All @@ -23,7 +22,6 @@ export default {
checked: false,
disabled: false,
required: false,
readOnly: false,
storyContent: "Label",
},
argTypes: {
Expand Down