shoelace/docs/components/radio.md

173 wiersze
4.8 KiB
Markdown
Czysty Zwykły widok Historia

2020-07-15 21:30:37 +00:00
# Radio
[component-header:sl-radio]
2022-03-15 21:42:59 +00:00
Radios allow the user to select a single option from a group.
2020-07-15 21:30:37 +00:00
2022-03-15 21:42:59 +00:00
Radios are designed to be used with [radio groups](/components/radio-group).
2021-04-09 12:15:33 +00:00
2020-07-15 21:30:37 +00:00
```html preview
2022-08-01 16:08:31 +00:00
<sl-radio-group label="Select an option">
<sl-radio name="option" value="1" checked>Option 1</sl-radio>
2022-03-11 16:40:34 +00:00
<sl-radio name="option" value="2">Option 2</sl-radio>
<sl-radio name="option" value="3">Option 3</sl-radio>
2021-04-09 12:15:33 +00:00
</sl-radio-group>
2020-07-15 21:30:37 +00:00
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlRadio, SlRadioGroup } from '@shoelace-style/shoelace/dist/react';
const App = () => (
2022-08-01 16:08:31 +00:00
<SlRadioGroup label="Select an option">
<SlRadio name="option" value="1" checked>
2022-03-02 15:10:41 +00:00
Option 1
</SlRadio>
2022-03-11 16:40:34 +00:00
<SlRadio name="option" value="2">
Option 2
</SlRadio>
<SlRadio name="option" value="3">
Option 3
</SlRadio>
2021-11-04 22:12:47 +00:00
</SlRadioGroup>
);
```
?> This component works with standard `<form>` elements. Please refer to the section on [form controls](/getting-started/form-controls) to learn more about form submission and client-side validation.
2020-07-15 21:30:37 +00:00
## Examples
2022-03-15 21:42:59 +00:00
### Checked
2020-07-15 21:30:37 +00:00
2022-03-15 21:42:59 +00:00
To set the initial checked state, use the `checked` attribute.
2020-07-15 21:30:37 +00:00
```html preview
2022-08-01 16:08:31 +00:00
<sl-radio-group label="Select an option">
<sl-radio name="option" value="1" checked>Option 1</sl-radio>
2022-03-11 16:40:34 +00:00
<sl-radio name="option" value="2">Option 2</sl-radio>
<sl-radio name="option" value="3">Option 3</sl-radio>
2021-04-09 12:15:33 +00:00
</sl-radio-group>
2020-07-15 21:30:37 +00:00
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlRadio, SlRadioGroup } from '@shoelace-style/shoelace/dist/react';
const App = () => (
2022-08-01 16:08:31 +00:00
<SlRadioGroup label="Select an option">
<SlRadio name="option" value="1" checked>
2022-03-02 15:10:41 +00:00
Option 1
</SlRadio>
2022-03-11 16:40:34 +00:00
<SlRadio name="option" value="2">
Option 2
</SlRadio>
<SlRadio name="option" value="3">
Option 3
</SlRadio>
2022-03-15 21:42:59 +00:00
</SlRadioGroup>
);
```
### Disabled
Use the `disabled` attribute to disable a radio.
```html preview
2022-08-01 16:08:31 +00:00
<sl-radio-group label="Select an option">
<sl-radio name="option" value="1" checked>Option 1</sl-radio>
2022-03-15 21:42:59 +00:00
<sl-radio name="option" value="2">Option 2</sl-radio>
<sl-radio name="option" value="3" disabled>Option 3</sl-radio>
</sl-radio-group>
```
```jsx react
import { SlRadio, SlRadioGroup } from '@shoelace-style/shoelace/dist/react';
const App = () => (
2022-08-01 16:08:31 +00:00
<SlRadioGroup label="Select an option">
<SlRadio name="option" value="1" checked>
2022-03-15 21:42:59 +00:00
Option 1
</SlRadio>
<SlRadio name="option" value="2">
Option 2
</SlRadio>
<SlRadio name="option" value="3" disabled>
Option 3
2022-03-02 15:10:41 +00:00
</SlRadio>
2021-11-04 22:12:47 +00:00
</SlRadioGroup>
);
```
2022-08-01 16:08:31 +00:00
### 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
<form class="custom-validity">
<sl-radio-group label="Select an option">
<sl-radio name="a" value="1" checked>Not me</sl-radio>
<sl-radio name="a" value="2">Me neither</sl-radio>
<sl-radio name="a" value="3">Choose me</sl-radio>
</sl-radio-group>
<br />
<sl-button type="submit" variant="primary">Submit</sl-button>
</form>
<script>
const form = document.querySelector('.custom-validity');
const radio = form.querySelectorAll('sl-radio')[2];
const errorMessage = 'You must choose this option';
// Set initial validity as soon as the element is defined
customElements.whenDefined('sl-radio').then(() => {
radio.setCustomValidity(errorMessage);
});
// Update validity when a selection is made
form.addEventListener('sl-change', () => {
const isValid = radio.checked;
radio.setCustomValidity(isValid ? '' : errorMessage);
});
// Handle form submit
form.addEventListener('submit', event => {
event.preventDefault();
alert('All fields are valid!');
});
</script>
```
```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 (
<form class="custom-validity" onSubmit={handleSubmit}>
<SlRadioGroup label="Select an option">
<SlRadio name="a" value="1" checked onSlChange={handleChange}>
Not me
</SlRadio>
<SlRadio name="a" value="2" onSlChange={handleChange}>
Me neither
</SlRadio>
<SlRadio ref={radio} name="a" value="3" onSlChange={handleChange}>
Choose me
</SlRadio>
</SlRadioGroup>
<br />
<SlButton type="submit" variant="primary">
Submit
</SlButton>
</form>
);
};
```
2020-07-15 21:30:37 +00:00
[component-metadata:sl-radio]