diff --git a/docs/components/radio-group.md b/docs/components/radio-group.md index d5a28963d..b58d23072 100644 --- a/docs/components/radio-group.md +++ b/docs/components/radio-group.md @@ -5,10 +5,10 @@ Radio groups are used to group multiple [radios](/components/radio) or [radio buttons](/components/radio-button) so they function as a single form control. ```html preview - - Option 1 - Option 2 - Option 3 + + Option 1 + Option 2 + Option 3 ``` @@ -16,16 +16,10 @@ Radio groups are used to group multiple [radios](/components/radio) or [radio bu import { SlRadio, SlRadioGroup } from '@shoelace-style/shoelace/dist/react'; const App = () => ( - - - Option 1 - - - Option 2 - - - Option 3 - + + Option 1 + Option 2 + Option 3 ); ``` @@ -37,8 +31,8 @@ const App = () => ( You can show the fieldset and legend that wraps the radio group using the `fieldset` attribute. If you don't use this option, you should still provide a label so screen readers announce the control correctly. ```html preview - - Option 1 + + Option 1 Option 2 Option 3 @@ -48,8 +42,8 @@ You can show the fieldset and legend that wraps the radio group using the `field import { SlRadio, SlRadioGroup } from '@shoelace-style/shoelace/dist/react'; const App = () => ( - - + + Option 1 @@ -67,8 +61,8 @@ const App = () => ( [Radio buttons](/components/radio-button) offer an alternate way to display radio controls. In this case, an internal [button group](/components/button-group) is used to group the buttons into a single, cohesive control. ```html preview - - Option 1 + + Option 1 Option 2 Option 3 @@ -78,8 +72,8 @@ const App = () => ( import { SlRadioButton, SlRadioGroup } from '@shoelace-style/shoelace/dist/react'; const App = () => ( - - + + Option 1 @@ -92,4 +86,141 @@ const App = () => ( ); ``` +### Validation + +Setting the `required` attribute to make selecting an option mandatory. If a value has not been selected, it will prevent the form from submitting and display an error message. + +```html preview +
+ + Not me + Me neither + Choose me + +
+ Submit +
+ + +``` + +```jsx react +import { SlButton, SlIcon, SlRadio, SlRadioGroup } from '@shoelace-style/shoelace/dist/react'; +const App = () => { + function handleSubmit(event) { + event.preventDefault(); + alert('All fields are valid!'); + } + + return ( +
+ + + Not me + + + Me neither + + + Choose me + + +
+ + Submit + +
+ ); +}; +``` + +#### 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 radioGroup = useRef(null); + const errorMessage = 'You must choose this option'; + + function handleChange() { + radioGroup.current.setCustomValidity(radioGroup.current.value === '3' ? '' : 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]