shoelace/docs/components/radio.md

106 wiersze
2.6 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 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-03 15:55:24 +00:00
<sl-radio-group label="Select an option" name="a" value="1">
<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>
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-03 15:55:24 +00:00
<SlRadioGroup label="Select an option" name="a" value="1">
<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>
);
```
?> 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-08-03 15:55:24 +00:00
### Initial Value
2020-07-15 21:30:37 +00:00
2022-08-03 15:55:24 +00:00
To set the initial value and checked state, use the `value` attribute on the containing radio group.
2020-07-15 21:30:37 +00:00
```html preview
2022-08-04 12:23:50 +00:00
<sl-radio-group label="Select an option" name="a" value="3">
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>
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-04 12:23:50 +00:00
<SlRadioGroup label="Select an option" name="a" value="3">
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>
2022-03-15 21:42:59 +00:00
</SlRadioGroup>
);
```
### Disabled
Use the `disabled` attribute to disable a radio.
```html preview
2022-08-03 15:55:24 +00:00
<sl-radio-group label="Select an option" name="a" value="1">
<sl-radio value="1">Option 1</sl-radio>
2022-08-04 12:23:50 +00:00
<sl-radio value="2" disabled>Option 2</sl-radio>
<sl-radio value="3">Option 3</sl-radio>
2022-03-15 21:42:59 +00:00
</sl-radio-group>
```
```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">
<SlRadio value="1">Option 1</SlRadio>
2022-08-04 12:23:50 +00:00
<SlRadio value="2" disabled>
Option 2
2022-03-02 15:10:41 +00:00
</SlRadio>
2022-08-04 12:23:50 +00:00
<SlRadio value="3">Option 3</SlRadio>
2021-11-04 22:12:47 +00:00
</SlRadioGroup>
);
```
2022-12-13 14:28:12 +00:00
## Sizes
Use the `size` attribute to change a radio's size.
```html preview
<sl-radio size="small">Small</sl-radio>
<sl-radio size="medium">Medium</sl-radio>
<sl-radio size="large">Large</sl-radio>
```
```jsx react
import { SlRadio } from '@shoelace-style/shoelace/dist/react';
const App = () => (
<>
<SlRadio size="small">Small</SlRadio>
<br />
<SlRadio size="medium">Medium</SlRadio>
<br />
<SlRadio size="large">Large</SlRadio>
</>
);
```
2020-07-15 21:30:37 +00:00
[component-metadata:sl-radio]