shoelace/docs/components/radio-group.md

233 wiersze
6.8 KiB
Markdown
Czysty Zwykły widok Historia

2021-04-09 12:15:33 +00:00
# Radio Group
[component-header:sl-radio-group]
```html preview
2022-08-03 15:55:24 +00:00
<sl-radio-group label="Select an option" name="a" value="1">
2022-08-02 17:16:19 +00:00
<sl-radio value="1">Option 1</sl-radio>
<sl-radio value="2">Option 2</sl-radio>
<sl-radio value="3">Option 3</sl-radio>
2022-03-14 13:05:26 +00:00
</sl-radio-group>
2021-04-09 12:15:33 +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-03 15:55:24 +00:00
<SlRadioGroup label="Select an option" name="a" value="1">
2022-08-02 17:16:19 +00:00
<SlRadio value="1">Option 1</SlRadio>
<SlRadio value="2">Option 2</SlRadio>
<SlRadio value="3">Option 3</SlRadio>
2021-11-04 22:12:47 +00:00
</SlRadioGroup>
);
```
2021-04-09 12:15:33 +00:00
## Examples
2022-11-09 20:27:51 +00:00
### Help Text
2021-04-09 12:15:33 +00:00
2022-11-09 20:27:51 +00:00
Add descriptive help text to a radio group with the `help-text` attribute. For help texts that contain HTML, use the `help-text` slot instead.
2021-04-09 12:15:33 +00:00
```html preview
2022-11-09 20:27:51 +00:00
<sl-radio-group label="Select an option" help-text="Choose the most appropriate option." name="a" value="1">
2022-08-03 15:55:24 +00:00
<sl-radio value="1">Option 1</sl-radio>
<sl-radio value="2">Option 2</sl-radio>
<sl-radio value="3">Option 3</sl-radio>
2021-04-09 12:15:33 +00:00
</sl-radio-group>
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlRadio, SlRadioGroup } from '@shoelace-style/shoelace/dist/react';
const App = () => (
2022-11-09 20:27:51 +00:00
<SlRadioGroup label="Select an option" help-text="Choose the most appropriate option." name="a" value="1">
2022-08-03 15:55:24 +00:00
<SlRadio value="1">Option 1</SlRadio>
<SlRadio value="2">Option 2</SlRadio>
<SlRadio value="3">Option 3</SlRadio>
2021-11-04 22:12:47 +00:00
</SlRadioGroup>
);
```
2022-03-02 15:10:41 +00:00
2022-03-15 21:42:59 +00:00
### Radio Buttons
[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
2022-11-09 20:27:51 +00:00
<sl-radio-group label="Select an option" help-text="Select an option that makes you proud." name="a" value="1">
2022-08-03 15:55:24 +00:00
<sl-radio-button value="1">Option 1</sl-radio-button>
<sl-radio-button value="2">Option 2</sl-radio-button>
<sl-radio-button value="3">Option 3</sl-radio-button>
2022-03-15 21:42:59 +00:00
</sl-radio-group>
```
```jsx react
import { SlRadioButton, SlRadioGroup } from '@shoelace-style/shoelace/dist/react';
const App = () => (
2022-08-03 15:55:24 +00:00
<SlRadioGroup label="Select an option" name="a" value="1">
<SlRadioButton value="1">Option 1</SlRadioButton>
<SlRadioButton value="2">Option 2</SlRadioButton>
<SlRadioButton value="3">Option 3</SlRadioButton>
2022-03-15 21:42:59 +00:00
</SlRadioGroup>
);
```
2022-08-04 12:23:50 +00:00
### Disabling Options
Radios and radio buttons can be disabled by adding the `disabled` attribute to the respective options inside the radio group.
```html preview
<sl-radio-group label="Select an option" name="a" value="1">
<sl-radio value="1">Option 1</sl-radio>
<sl-radio value="2" disabled>Option 2</sl-radio>
<sl-radio value="3">Option 3</sl-radio>
</sl-radio-group>
```
```jsx react
import { SlRadio, SlRadioGroup } from '@shoelace-style/shoelace/dist/react';
const App = () => (
<SlRadioGroup label="Select an option" name="a" value="1">
<SlRadio value="1">Option 1</SlRadio>
<SlRadio value="2" disabled>
Option 2
</SlRadio>
<SlRadio value="3">Option 3</SlRadio>
</SlRadioGroup>
);
```
2022-08-02 17:16:19 +00:00
### 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
<form class="validation">
2022-08-03 15:55:24 +00:00
<sl-radio-group label="Select an option" name="a" required>
<sl-radio value="1">Option 1</sl-radio>
<sl-radio value="2">Option 2</sl-radio>
<sl-radio value="3">Option 3</sl-radio>
2022-08-02 17:16:19 +00:00
</sl-radio-group>
<br />
<sl-button type="submit" variant="primary">Submit</sl-button>
</form>
<script>
const form = document.querySelector('.validation');
// Handle form submit
form.addEventListener('submit', event => {
event.preventDefault();
alert('All fields are valid!');
});
</script>
```
```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 (
2022-08-02 17:20:02 +00:00
<form class="custom-validity" onSubmit={handleSubmit}>
2022-08-03 15:55:24 +00:00
<SlRadioGroup label="Select an option" name="a" required onSlChange={handleChange}>
<SlRadio value="1">
2022-08-02 17:20:02 +00:00
Option 1
2022-08-02 17:16:19 +00:00
</SlRadio>
2022-08-03 15:55:24 +00:00
<SlRadiovalue="2">
2022-08-02 17:20:02 +00:00
Option 2
2022-08-02 17:16:19 +00:00
</SlRadio>
2022-08-03 15:55:24 +00:00
<SlRadio value="3">
2022-08-02 17:20:02 +00:00
Option 3
2022-08-02 17:16:19 +00:00
</SlRadio>
</SlRadioGroup>
<br />
<SlButton type="submit" variant="primary">
Submit
</SlButton>
</form>
);
};
```
2022-11-09 20:27:51 +00:00
### Custom Validity
2022-08-02 17:16:19 +00:00
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">
2022-08-03 15:55:24 +00:00
<sl-radio-group label="Select an option" name="a" value="1">
<sl-radio value="1">Not me</sl-radio>
<sl-radio value="2">Me neither</sl-radio>
<sl-radio value="3">Choose me</sl-radio>
2022-08-02 17:16:19 +00:00
</sl-radio-group>
<br />
<sl-button type="submit" variant="primary">Submit</sl-button>
</form>
<script>
const form = document.querySelector('.custom-validity');
const radioGroup = form.querySelector('sl-radio-group');
const errorMessage = 'You must choose the last option';
// Set initial validity as soon as the element is defined
customElements.whenDefined('sl-radio-group').then(() => {
radioGroup.setCustomValidity(errorMessage);
});
// Update validity when a selection is made
form.addEventListener('sl-change', () => {
const isValid = radioGroup.value === '3';
radioGroup.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 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 (
<form class="custom-validity" onSubmit={handleSubmit}>
2022-08-03 15:55:24 +00:00
<SlRadioGroup ref={radioGroup} label="Select an option" name="a" value="1" onSlChange={handleChange}>
<SlRadio value="1">Not me</SlRadio>
<SlRadio value="2">Me neither</SlRadio>
<SlRadio value="3">Choose me</SlRadio>
2022-08-02 17:16:19 +00:00
</SlRadioGroup>
<br />
<SlButton type="submit" variant="primary">
Submit
</SlButton>
</form>
);
};
```
2021-04-09 12:15:33 +00:00
[component-metadata:sl-radio-group]