diff --git a/docs/docs/components/radio-button.md b/docs/docs/components/radio-button.md index 7a38d07d6..850412018 100644 --- a/docs/docs/components/radio-button.md +++ b/docs/docs/components/radio-button.md @@ -9,7 +9,7 @@ icon: radio-group Radio buttons are designed to be used with [radio groups](/docs/components/radio-group). When a radio button has focus, the arrow keys can be used to change the selected option just like standard radio controls. ```html {.example} - + Option 1 Option 2 Option 3 @@ -23,7 +23,7 @@ Radio buttons are designed to be used with [radio groups](/docs/components/radio To set the initial value and checked state, use the `value` attribute on the containing radio group. ```html {.example} - + Option 1 Option 2 Option 3 @@ -35,7 +35,7 @@ To set the initial value and checked state, use the `value` attribute on the con Use the `disabled` attribute to disable a radio button. ```html {.example} - + Option 1 Option 2 Option 3 @@ -47,7 +47,7 @@ Use the `disabled` attribute to disable a radio button. Use the `size` attribute to change a radio button's size. ```html {.example} - + Option 1 Option 2 Option 3 @@ -55,7 +55,7 @@ Use the `size` attribute to change a radio button's size.
- + Option 1 Option 2 Option 3 @@ -63,7 +63,7 @@ Use the `size` attribute to change a radio button's size.
- + Option 1 Option 2 Option 3 @@ -75,7 +75,7 @@ Use the `size` attribute to change a radio button's size. Use the `pill` attribute to give radio buttons rounded edges. ```html {.example} - + Option 1 Option 2 Option 3 @@ -83,7 +83,7 @@ Use the `pill` attribute to give radio buttons rounded edges.
- + Option 1 Option 2 Option 3 @@ -91,7 +91,7 @@ Use the `pill` attribute to give radio buttons rounded edges.
- + Option 1 Option 2 Option 3 @@ -103,7 +103,7 @@ Use the `pill` attribute to give radio buttons rounded edges. Use the `prefix` and `suffix` slots to add icons. ```html {.example} - + Option 1 @@ -127,7 +127,7 @@ Use the `prefix` and `suffix` slots to add icons. You can omit button labels and use icons instead. Make sure to set a `label` attribute on each icon so screen readers will announce each option correctly. ```html {.example} - + diff --git a/docs/docs/components/radio-group.md b/docs/docs/components/radio-group.md index 863ba5d03..bb4a6d0aa 100644 --- a/docs/docs/components/radio-group.md +++ b/docs/docs/components/radio-group.md @@ -7,8 +7,8 @@ icon: radio-group ```html {.example} - Option 1
- Option 2
+ Option 1 + Option 2 Option 3
``` @@ -21,8 +21,8 @@ Add descriptive hint to a radio group with the `hint` attribute. For hints that ```html {.example} - Option 1
- Option 2
+ Option 1 + Option 2 Option 3
``` @@ -32,7 +32,28 @@ Add descriptive hint to a radio group with the `hint` attribute. For hints that [Radio buttons](/docs/components/radio-button) offer an alternate way to display radio controls. In this case, an internal [button group](/docs/components/button-group) is used to group the buttons into a single, cohesive control. ```html {.example} - + + Option 1 + Option 2 + Option 3 + + +
+ + Option 1 Option 2 Option 3 @@ -45,8 +66,26 @@ Radios and radio buttons can be disabled by adding the `disabled` attribute to t ```html {.example} - Option 1
- Option 2
+ Option 1 + Option 2 + Option 3 +
+``` + +### Orientation + +The default orientation for radio items is `vertical`. Set the `orientation` to `horizontal` to items on the same row. + +```html {.example} + + Option 1 + Option 2 Option 3 ``` @@ -57,8 +96,8 @@ The size of [Radios](/docs/components/radio) and [Radio Buttons](/docs/component ```html preview - Small
- Medium
+ Small + Medium Large
@@ -82,8 +121,8 @@ Setting the `required` attribute to make selecting an option mandatory. If a val ```html {.example}
- Option 1
- Option 2
+ Option 1 + Option 2 Option 3

@@ -108,8 +147,8 @@ Use the `setCustomValidity()` method to set a custom validation message. This wi ```html {.example} - Not me
- Me neither
+ Not me + Me neither Choose me

@@ -127,7 +166,7 @@ Use the `setCustomValidity()` method to set a custom validation message. This wi }); // Update validity when a selection is made - form.addEventlistener('change', () => { + form.addEventListener('change', () => { const isValid = radioGroup.value === '3'; radioGroup.setCustomValidity(isValid ? '' : errorMessage); }); diff --git a/docs/docs/components/radio.md b/docs/docs/components/radio.md index b83d27238..828c6c059 100644 --- a/docs/docs/components/radio.md +++ b/docs/docs/components/radio.md @@ -11,8 +11,8 @@ Radios are designed to be used with [radio groups](/docs/components/radio-group) ```html {.example} - Option 1
- Option 2
+ Option 1 + Option 2 Option 3
``` @@ -29,8 +29,8 @@ To set the initial value and checked state, use the `value` attribute on the con ```html {.example} - Option 1
- Option 2
+ Option 1 + Option 2 Option 3
``` @@ -41,8 +41,8 @@ Use the `disabled` attribute to disable a radio. ```html {.example} - Option 1
- Option 2
+ Option 1 + Option 2 Option 3
``` @@ -53,24 +53,24 @@ Add the `size` attribute to the [Radio Group](/docs/components/radio-group) to c ```html {.example} - Small 1
- Small 2
+ Small 1 + Small 2 Small 3

- Medium 1
- Medium 2
+ Medium 1 + Medium 2 Medium 3

- Large 1
- Large 2
+ Large 1 + Large 2 Large 3
``` diff --git a/docs/docs/resources/changelog.md b/docs/docs/resources/changelog.md index 195c36b46..8ed6d487b 100644 --- a/docs/docs/resources/changelog.md +++ b/docs/docs/resources/changelog.md @@ -20,6 +20,7 @@ During the alpha period, things might break! We take breaking changes very serio - `wa-blur` => `blur` (this event will no longer bubble, use `focusout` for a bubbling version) - `wa-focus` => `focus` (this event will no longer bubble) - Added `.wa-callout` utility class +- Added the `orientation` attribute to `` to support vertical and horizontal radio items - Fixed a bug in `` that prevented nested tab groups from working properly - Fixed slot names for `show-password-icon` and `hide-password-icon` in `` to more intuitively represent their functions diff --git a/src/components/radio-group/radio-group.css b/src/components/radio-group/radio-group.css index 32071dc91..d3e93faa9 100644 --- a/src/components/radio-group/radio-group.css +++ b/src/components/radio-group/radio-group.css @@ -22,6 +22,26 @@ display: flex; } +[part~='form-control-input'] { + display: flex; + flex-direction: column; + flex-wrap: wrap; + gap: var(--wa-space-s); + margin-block-end: var(--wa-space-xs); +} + +/* Horizontal */ +:host([orientation='horizontal']) [part~='form-control-input'] { + flex-direction: row; + gap: var(--wa-space-m); +} + +/* When radio buttons are slotted */ +:host([orientation]) .form-control--has-radio-buttons [part~='form-control-input'] { + gap: 0; + flex-wrap: nowrap; +} + /* Help text */ [part~='hint'] { margin-block-start: var(--wa-space-2xs); diff --git a/src/components/radio-group/radio-group.ts b/src/components/radio-group/radio-group.ts index 7b36829e3..8d55692d7 100644 --- a/src/components/radio-group/radio-group.ts +++ b/src/components/radio-group/radio-group.ts @@ -9,6 +9,7 @@ import { WebAwesomeFormAssociatedElement } from '../../internal/webawesome-form- import formControlStyles from '../../styles/shadow/form-control.css'; import buttonGroupStyles from '../../styles/utilities/button-group.css'; import sizeStyles from '../../styles/utilities/size.css'; +import '../radio-button/radio-button.js'; import type WaRadioButton from '../radio-button/radio-button.js'; import '../radio/radio.js'; import type WaRadio from '../radio/radio.js'; @@ -20,7 +21,8 @@ import styles from './radio-group.css'; * @status stable * @since 2.0 * - * @dependency wa-button-group + * @dependency wa-radio + * @dependency wa-radio-button * * @slot - The default slot where `` or `` elements are placed. * @slot label - The radio group's label. Required for proper accessibility. Alternatively, you can use the `label` @@ -34,6 +36,7 @@ import styles from './radio-group.css'; * @csspart form-control - The form control that wraps the label, input, and hint. * @csspart form-control-label - The label's wrapper. * @csspart form-control-input - The input's wrapper. + * @csspart radios - The wrapper than surrounds radio items, styled as a flex container by default. * @csspart hint - The hint's wrapper. * @csspart button-group - The button group that wraps radio buttons. * @csspart button-group__base - The button group's `base` part. @@ -63,6 +66,7 @@ export default class WaRadioGroup extends WebAwesomeFormAssociatedElement { @query('slot:not([name])') defaultSlot: HTMLSlotElement; @state() private hasButtonGroup = false; + @state() private hasRadioButtons = false; /** * The radio group's label. Required for proper accessibility. If you need to display HTML, use the `label` slot @@ -76,6 +80,9 @@ export default class WaRadioGroup extends WebAwesomeFormAssociatedElement { /** The name of the radio group, submitted as a name/value pair with form data. */ @property({ reflect: true }) name: string | null = null; + /** The orientation in which to show radio items. */ + @property({ reflect: true }) orientation: 'horizontal' | 'vertical' = 'vertical'; + private _value: string | null = null; get value() { @@ -173,6 +180,9 @@ export default class WaRadioGroup extends WebAwesomeFormAssociatedElement { private async syncRadioElements() { const radios = this.getAllRadios(); + // Detect the presence of radio buttons + this.hasRadioButtons = radios.some(radio => radio.localName === 'wa-radio-button'); + await Promise.all( // Sync the checked state and size radios.map(async radio => { @@ -324,11 +334,13 @@ export default class WaRadioGroup extends WebAwesomeFormAssociatedElement { 'form-control--large': this.size === 'large', 'form-control--radio-group': true, 'form-control--has-label': hasLabel, + 'form-control--has-radio-buttons': this.hasRadioButtons, })} role="radiogroup" aria-labelledby="label" aria-describedby="hint" aria-errormessage="error-message" + aria-orientation=${this.orientation} >