diff --git a/docs/components/radio-group.md b/docs/components/radio-group.md index 49ad989bc..dd930b7f1 100644 --- a/docs/components/radio-group.md +++ b/docs/components/radio-group.md @@ -86,7 +86,60 @@ const App = () => ( ); ``` -### Custom Validity +### Validation + +Setting the `required` attribute to make selecting a an option mandatory. If a value has not been selected, it will prevent the form from submitting and display an error message. + +```html preview +
+ +``` + +```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 ( + + ); +}; +``` + +#### 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. @@ -125,10 +178,10 @@ Use the `setCustomValidity()` method to set a custom validation message. This wi import { useEffect, useRef } from 'react'; import { SlButton, SlIcon, SlRadio, SlRadioGroup } from '@shoelace-style/shoelace/dist/react'; const App = () => { - const radio = useRef(null); + const radioGroup = useRef(null); const errorMessage = 'You must choose this option'; - function handleChange(event) { - radio.current.setCustomValidity(radio.current.checked ? '' : errorMessage); + function handleChange() { + radioGroup.current.setCustomValidity(radioGroup.current.value === '3' ? '' : errorMessage); } function handleSubmit(event) { event.preventDefault(); @@ -139,14 +192,14 @@ const App = () => { }, []); return (