diff --git a/docs/components/radio-button.md b/docs/components/radio-button.md index df6de475..87297f21 100644 --- a/docs/components/radio-button.md +++ b/docs/components/radio-button.md @@ -7,8 +7,8 @@ Radios buttons allow the user to select a single option from a group using a but Radio buttons are designed to be used with [radio groups](/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 preview - - Option 1 + + Option 1 Option 2 Option 3 @@ -18,8 +18,8 @@ Radio buttons are designed to be used with [radio groups](/components/radio-grou import { SlRadioButton, SlRadioGroup } from '@shoelace-style/shoelace/dist/react'; const App = () => ( - - + + Option 1 @@ -34,43 +34,13 @@ const App = () => ( ## Examples -### Checked - -To set the initial checked state, use the `checked` attribute. - -```html preview - - Option 1 - Option 2 - Option 3 - -``` - -```jsx react -import { SlRadioButton, SlRadioGroup } from '@shoelace-style/shoelace/dist/react'; - -const App = () => ( - - - Option 1 - - - Option 2 - - - Option 3 - - -); -``` - ### Disabled Use the `disabled` attribute to disable a radio button. ```html preview - - Option 1 + + Option 1 Option 2 Option 3 @@ -80,8 +50,8 @@ Use the `disabled` attribute to disable a radio button. import { SlRadioButton, SlRadioGroup } from '@shoelace-style/shoelace/dist/react'; const App = () => ( - - + + Option 1 @@ -99,24 +69,24 @@ const App = () => ( Use the `size` attribute to change a radio button's size. ```html preview - - Option 1 + + Option 1 Option 2 Option 3
- - Option 1 + + Option 1 Option 2 Option 3
- - Option 1 + + Option 1 Option 2 Option 3 @@ -126,24 +96,24 @@ Use the `size` attribute to change a radio button's size. import { SlRadioButton, SlRadioGroup } from '@shoelace-style/shoelace/dist/react'; const App = () => ( - - Option 1 + + Option 1 Option 2 Option 3
- - Option 1 + + Option 1 Option 2 Option 3
- - Option 1 + + Option 1 Option 2 Option 3 @@ -155,24 +125,24 @@ const App = () => ( Use the `pill` attribute to give radio buttons rounded edges. ```html preview - - Option 1 + + Option 1 Option 2 Option 3
- - Option 1 + + Option 1 Option 2 Option 3
- - Option 1 + + Option 1 Option 2 Option 3 @@ -182,24 +152,24 @@ Use the `pill` attribute to give radio buttons rounded edges. import { SlRadioButton, SlRadioGroup } from '@shoelace-style/shoelace/dist/react'; const App = () => ( - - Option 1 + + Option 1 Option 2 Option 3
- - Option 1 + + Option 1 Option 2 Option 3
- - Option 1 + + Option 1 Option 2 Option 3 @@ -211,8 +181,8 @@ const App = () => ( Use the `prefix` and `suffix` slots to add icons. ```html preview - - + + Option 1 @@ -234,8 +204,8 @@ Use the `prefix` and `suffix` slots to add icons. import { SlIcon, SlRadioButton, SlRadioGroup } from '@shoelace-style/shoelace/dist/react'; const App = () => ( - - + + Option 1 @@ -259,7 +229,7 @@ const App = () => ( 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 preview - + @@ -268,7 +238,7 @@ You can omit button labels and use icons instead. Make sure to set a `label` att - + @@ -286,7 +256,7 @@ You can omit button labels and use icons instead. Make sure to set a `label` att import { SlIcon, SlRadioButton, SlRadioGroup } from '@shoelace-style/shoelace/dist/react'; const App = () => ( - + @@ -295,7 +265,7 @@ const App = () => (
- + @@ -310,77 +280,4 @@ const App = () => ( ); ``` -### Custom Validity - -Use the `setCustomValidity()` method to set a custom validation message. This will prevent the form from submitting and make the browser display the error message you provide. To clear the error, call this function with an empty string. - -```html preview -
- - Not me - Me neither - Choose me - -
- Submit -
- -``` - -```jsx react -import { useEffect, useRef } from 'react'; -import { SlButton, SlIcon, SlRadioButton, SlRadioGroup } from '@shoelace-style/shoelace/dist/react'; -const App = () => { - const radio = useRef(null); - const errorMessage = 'You must choose this option'; - function handleChange(event) { - radio.current.setCustomValidity(radio.current.checked ? '' : errorMessage); - } - function handleSubmit(event) { - event.preventDefault(); - alert('All fields are valid!'); - } - useEffect(() => { - radio.current.setCustomValidity(errorMessage); - }, []); - return ( -
- - - Not me - - - Me neither - - - Choose me - - -
- - Submit - -
- ); -}; -``` - [component-metadata:sl-radio-button] diff --git a/docs/components/radio-group.md b/docs/components/radio-group.md index d860877d..e22eefc7 100644 --- a/docs/components/radio-group.md +++ b/docs/components/radio-group.md @@ -86,4 +86,77 @@ const App = () => ( ); ``` +### Custom Validity + +Use the `setCustomValidity()` method to set a custom validation message. This will prevent the form from submitting and make the browser display the error message you provide. To clear the error, call this function with an empty string. + +```html preview +
+ + Not me + Me neither + Choose me + +
+ Submit +
+ +``` + +```jsx react +import { useEffect, useRef } from 'react'; +import { SlButton, SlIcon, SlRadio, SlRadioGroup } from '@shoelace-style/shoelace/dist/react'; +const App = () => { + const radio = useRef(null); + const errorMessage = 'You must choose this option'; + function handleChange(event) { + radio.current.setCustomValidity(radio.current.checked ? '' : errorMessage); + } + function handleSubmit(event) { + event.preventDefault(); + alert('All fields are valid!'); + } + useEffect(() => { + radio.current.setCustomValidity(errorMessage); + }, []); + return ( +
+ + + Not me + + + Me neither + + + Choose me + + +
+ + Submit + +
+ ); +}; +``` + [component-metadata:sl-radio-group] diff --git a/docs/components/radio.md b/docs/components/radio.md index 25b481ed..33773de9 100644 --- a/docs/components/radio.md +++ b/docs/components/radio.md @@ -7,8 +7,8 @@ Radios allow the user to select a single option from a group. Radios are designed to be used with [radio groups](/components/radio-group). ```html preview - - Option 1 + + Option 1 Option 2 Option 3 @@ -18,8 +18,8 @@ Radios are designed to be used with [radio groups](/components/radio-group). import { SlRadio, SlRadioGroup } from '@shoelace-style/shoelace/dist/react'; const App = () => ( - - + + Option 1 @@ -41,8 +41,8 @@ const App = () => ( To set the initial checked state, use the `checked` attribute. ```html preview - - Option 1 + + Option 1 Option 2 Option 3 @@ -52,8 +52,8 @@ To set the initial checked state, use the `checked` attribute. import { SlRadio, SlRadioGroup } from '@shoelace-style/shoelace/dist/react'; const App = () => ( - - + + Option 1 @@ -71,8 +71,8 @@ const App = () => ( Use the `disabled` attribute to disable a radio. ```html preview - - Option 1 + + Option 1 Option 2 Option 3 @@ -82,8 +82,8 @@ Use the `disabled` attribute to disable a radio. import { SlRadio, SlRadioGroup } from '@shoelace-style/shoelace/dist/react'; const App = () => ( - - + + Option 1 @@ -96,77 +96,4 @@ const App = () => ( ); ``` -### Custom Validity - -Use the `setCustomValidity()` method to set a custom validation message. This will prevent the form from submitting and make the browser display the error message you provide. To clear the error, call this function with an empty string. - -```html preview -
- - Not me - Me neither - Choose me - -
- Submit -
- -``` - -```jsx react -import { useEffect, useRef } from 'react'; -import { SlButton, SlIcon, SlRadio, SlRadioGroup } from '@shoelace-style/shoelace/dist/react'; -const App = () => { - const radio = useRef(null); - const errorMessage = 'You must choose this option'; - function handleChange(event) { - radio.current.setCustomValidity(radio.current.checked ? '' : errorMessage); - } - function handleSubmit(event) { - event.preventDefault(); - alert('All fields are valid!'); - } - useEffect(() => { - radio.current.setCustomValidity(errorMessage); - }, []); - return ( -
- - - Not me - - - Me neither - - - Choose me - - -
- - Submit - -
- ); -}; -``` - [component-metadata:sl-radio]