shoelace/docs/components/input.md

277 wiersze
6.8 KiB
Markdown
Czysty Zwykły widok Historia

2020-07-15 21:30:37 +00:00
# Input
[component-header:sl-input]
```html preview
2022-08-30 21:08:40 +00:00
<sl-input></sl-input>
2020-07-15 21:30:37 +00:00
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlInput } from '@shoelace-style/shoelace/dist/react';
2022-03-02 15:10:41 +00:00
const App = () => <SlInput />;
2021-11-04 22:12:47 +00:00
```
?> 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-08-28 20:14:39 +00:00
2020-07-15 21:30:37 +00:00
## Examples
2022-03-18 19:58:25 +00:00
### Labels
Use the `label` attribute to give the input an accessible label. For labels that contain HTML, use the `label` slot instead.
```html preview
<sl-input label="What is your name?"></sl-input>
```
```jsx react
import { SlIcon, SlInput } from '@shoelace-style/shoelace/dist/react';
const App = () => <SlInput label="What is your name?" />;
```
### Help Text
Add descriptive help text to an input with the `help-text` attribute. For help texts that contain HTML, use the `help-text` slot instead.
```html preview
<sl-input label="Nickname" help-text="What would you like people to call you?"></sl-input>
```
```jsx react
import { SlIcon, SlInput } from '@shoelace-style/shoelace/dist/react';
const App = () => <SlInput label="Nickname" help-text="What would you like people to call you?" />;
```
2020-08-25 20:23:33 +00:00
### Placeholders
2020-07-15 21:30:37 +00:00
2020-08-25 20:23:33 +00:00
Use the `placeholder` attribute to add a placeholder.
2020-07-15 21:30:37 +00:00
```html preview
2020-08-25 20:23:33 +00:00
<sl-input placeholder="Type something"></sl-input>
2020-07-15 21:30:37 +00:00
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlInput } from '@shoelace-style/shoelace/dist/react';
2022-03-02 15:10:41 +00:00
const App = () => <SlInput placeholder="Type something" />;
2021-11-04 22:12:47 +00:00
```
2020-08-25 20:23:33 +00:00
### Clearable
2020-07-15 21:30:37 +00:00
2021-07-08 21:41:10 +00:00
Add the `clearable` attribute to add a clear button when the input has content.
2020-07-15 21:30:37 +00:00
```html preview
2020-08-25 20:23:33 +00:00
<sl-input placeholder="Clearable" clearable></sl-input>
2020-07-15 21:30:37 +00:00
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlInput } from '@shoelace-style/shoelace/dist/react';
2022-03-02 15:10:41 +00:00
const App = () => <SlInput placeholder="Clearable" clearable />;
2021-11-04 22:12:47 +00:00
```
2020-08-25 20:23:33 +00:00
### Toggle Password
2020-07-15 21:30:37 +00:00
2022-09-16 19:27:10 +00:00
Add the `password-toggle` attribute to add a toggle button that will show the password when activated.
2020-07-15 21:30:37 +00:00
```html preview
2022-12-20 22:43:50 +00:00
<sl-input type="password" placeholder="Password Toggle" password-toggle></sl-input>
2020-07-15 21:30:37 +00:00
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlInput } from '@shoelace-style/shoelace/dist/react';
2022-12-20 22:43:50 +00:00
const App = () => <SlInput type="password" placeholder="Password Toggle" size="medium" password-toggle />;
2021-11-04 22:12:47 +00:00
```
2021-09-25 02:28:14 +00:00
### Filled Inputs
Add the `filled` attribute to draw a filled input.
```html preview
<sl-input placeholder="Type something" filled></sl-input>
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlInput } from '@shoelace-style/shoelace/dist/react';
2022-03-02 15:10:41 +00:00
const App = () => <SlInput placeholder="Type something" filled />;
2021-11-04 22:12:47 +00:00
```
2022-12-20 22:43:50 +00:00
### Disabled
2020-07-15 21:30:37 +00:00
2022-12-20 22:43:50 +00:00
Use the `disabled` attribute to disable an input.
2020-07-15 21:30:37 +00:00
```html preview
2022-12-20 22:43:50 +00:00
<sl-input placeholder="Disabled" disabled></sl-input>
2020-07-15 21:30:37 +00:00
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlInput } from '@shoelace-style/shoelace/dist/react';
2022-12-20 22:43:50 +00:00
const App = () => <SlInput placeholder="Disabled" disabled />;
2021-11-04 22:12:47 +00:00
```
2022-12-20 22:43:50 +00:00
### Sizes
2021-08-24 12:13:36 +00:00
2022-12-20 22:43:50 +00:00
Use the `size` attribute to change an input's size.
2021-08-24 12:13:36 +00:00
```html preview
2022-12-20 22:43:50 +00:00
<sl-input placeholder="Small" size="small"></sl-input>
2022-03-02 15:10:41 +00:00
<br />
2022-12-20 22:43:50 +00:00
<sl-input placeholder="Medium" size="medium"></sl-input>
2022-03-02 15:10:41 +00:00
<br />
2022-12-20 22:43:50 +00:00
<sl-input placeholder="Large" size="large"></sl-input>
2021-08-24 12:13:36 +00:00
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlInput } from '@shoelace-style/shoelace/dist/react';
const App = () => (
<>
2022-12-20 22:43:50 +00:00
<SlInput placeholder="Small" size="small" />
2021-11-04 22:12:47 +00:00
<br />
2022-12-20 22:43:50 +00:00
<SlInput placeholder="Medium" size="medium" />
2021-11-04 22:12:47 +00:00
<br />
2022-12-20 22:43:50 +00:00
<SlInput placeholder="Large" size="large" />
2021-11-04 22:12:47 +00:00
</>
);
```
2022-12-20 22:43:50 +00:00
### Pill
2020-08-25 20:23:33 +00:00
2022-12-20 22:43:50 +00:00
Use the `pill` attribute to give inputs rounded edges.
2020-08-25 20:23:33 +00:00
```html preview
2022-12-20 22:43:50 +00:00
<sl-input placeholder="Small" size="small" pill></sl-input>
2022-03-02 15:10:41 +00:00
<br />
2022-12-20 22:43:50 +00:00
<sl-input placeholder="Medium" size="medium" pill></sl-input>
2022-03-02 15:10:41 +00:00
<br />
2022-12-20 22:43:50 +00:00
<sl-input placeholder="Large" size="large" pill></sl-input>
2020-08-25 20:23:33 +00:00
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlInput } from '@shoelace-style/shoelace/dist/react';
const App = () => (
<>
2022-12-20 22:43:50 +00:00
<SlInput placeholder="Small" size="small" pill />
2021-11-04 22:12:47 +00:00
<br />
2022-12-20 22:43:50 +00:00
<SlInput placeholder="Medium" size="medium" pill />
2021-11-04 22:12:47 +00:00
<br />
2022-12-20 22:43:50 +00:00
<SlInput placeholder="Large" size="large" pill />
2021-11-04 22:12:47 +00:00
</>
);
```
2022-12-20 22:43:50 +00:00
### Input Types
2020-08-25 20:23:33 +00:00
2022-12-20 22:43:50 +00:00
The `type` attribute controls the type of input the browser renders.
2020-08-25 20:23:33 +00:00
```html preview
2022-12-20 22:43:50 +00:00
<sl-input type="email" placeholder="Email"></sl-input>
2022-03-02 15:10:41 +00:00
<br />
2022-12-20 22:43:50 +00:00
<sl-input type="number" placeholder="Number"></sl-input>
2022-03-02 15:10:41 +00:00
<br />
2022-12-20 22:43:50 +00:00
<sl-input type="date" placeholder="Date"></sl-input>
2020-08-25 20:23:33 +00:00
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlInput } from '@shoelace-style/shoelace/dist/react';
const App = () => (
<>
2022-12-20 22:43:50 +00:00
<SlInput type="email" placeholder="Email" />
2021-11-04 22:12:47 +00:00
<br />
2022-12-20 22:43:50 +00:00
<SlInput type="number" placeholder="Number" />
2021-11-04 22:12:47 +00:00
<br />
2022-12-20 22:43:50 +00:00
<SlInput type="date" placeholder="Date" />
2021-11-04 22:12:47 +00:00
</>
);
```
2020-07-15 21:30:37 +00:00
### Prefix & Suffix Icons
Use the `prefix` and `suffix` slots to add icons.
```html preview
<sl-input placeholder="Small" size="small">
2021-08-16 21:05:28 +00:00
<sl-icon name="house" slot="prefix"></sl-icon>
<sl-icon name="chat" slot="suffix"></sl-icon>
2020-07-15 21:30:37 +00:00
</sl-input>
2022-03-02 15:10:41 +00:00
<br />
2020-07-15 21:30:37 +00:00
<sl-input placeholder="Medium" size="medium">
2021-08-16 21:05:28 +00:00
<sl-icon name="house" slot="prefix"></sl-icon>
<sl-icon name="chat" slot="suffix"></sl-icon>
2020-07-15 21:30:37 +00:00
</sl-input>
2022-03-02 15:10:41 +00:00
<br />
2020-07-15 21:30:37 +00:00
<sl-input placeholder="Large" size="large">
2021-08-16 21:05:28 +00:00
<sl-icon name="house" slot="prefix"></sl-icon>
<sl-icon name="chat" slot="suffix"></sl-icon>
2020-07-15 21:30:37 +00:00
</sl-input>
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlIcon, SlInput } from '@shoelace-style/shoelace/dist/react';
const App = () => (
<>
<SlInput placeholder="Small" size="small">
<SlIcon name="house" slot="prefix"></SlIcon>
<SlIcon name="chat" slot="suffix"></SlIcon>
</SlInput>
<br />
<SlInput placeholder="Medium" size="medium">
<SlIcon name="house" slot="prefix"></SlIcon>
<SlIcon name="chat" slot="suffix"></SlIcon>
</SlInput>
<br />
<SlInput placeholder="Large" size="large">
<SlIcon name="house" slot="prefix"></SlIcon>
<SlIcon name="chat" slot="suffix"></SlIcon>
</SlInput>
</>
);
```
2022-03-18 21:33:23 +00:00
### Customizing Label Position
2022-11-10 22:12:29 +00:00
Use [CSS parts](#css-parts) to customize the way form controls are drawn. This example uses CSS grid to position the label to the left of the control, but the possible orientations are nearly endless. The same technique works for inputs, textareas, radio groups, and similar form controls.
2022-03-18 21:33:23 +00:00
```html preview
2022-11-10 22:12:29 +00:00
<sl-input class="label-on-left" label="Name" help-text="Enter your name""></sl-input>
<sl-input class="label-on-left" label="Email" type="email" help-text="Enter your email"></sl-input>
<sl-textarea class="label-on-left" label="Bio" help-text="Tell us something about yourself"></sl-textarea>
2022-03-18 21:33:23 +00:00
<style>
2022-11-10 22:12:29 +00:00
.label-on-left {
--label-width: 60px;
--gap-width: 1rem;
}
.label-on-left + .label-on-left {
margin-top: var(--sl-spacing-medium);
}
2022-03-18 21:33:23 +00:00
.label-on-left::part(form-control) {
2022-11-10 22:12:29 +00:00
display: grid;
grid: auto / var(--label-width) 1fr;
gap: var(--sl-spacing-3x-small) var(--gap-width);
2022-03-18 21:33:23 +00:00
align-items: center;
}
.label-on-left::part(form-control-label) {
text-align: right;
}
2022-11-10 22:12:29 +00:00
.label-on-left::part(form-control-help-text) {
grid-column: span 2;
padding-left: calc(var(--label-width) + var(--gap-width));
2022-03-18 21:33:23 +00:00
}
</style>
```
2020-07-15 21:30:37 +00:00
[component-metadata:sl-input]