shoelace/docs/components/input.md

164 wiersze
4.2 KiB
Markdown
Czysty Zwykły widok Historia

2020-07-15 21:30:37 +00:00
# Input
[component-header:sl-input]
Inputs collect data from the user.
```html preview
<sl-input></sl-input>
```
?> This component doesn't work with standard forms. Use [`<sl-form>`](/components/form.md) instead.
2020-08-28 20:14:39 +00:00
?> Please refer to the section on [form control validation](/components/form?id=form-control-validation) to learn how to do client-side validation.
2020-07-15 21:30:37 +00:00
## Examples
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
```
2020-08-25 20:23:33 +00:00
### Clearable
2020-07-15 21:30:37 +00:00
2020-08-25 20:23:33 +00:00
Add the `clearable` prop 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
```
2020-08-25 20:23:33 +00:00
### Toggle Password
2020-07-15 21:30:37 +00:00
2020-08-25 20:23:33 +00:00
Add the `toggle-password` prop to add a toggle button that will show the password when activated.
2020-07-15 21:30:37 +00:00
```html preview
2020-08-25 20:23:33 +00:00
<sl-input type="password" placeholder="Password Toggle" size="small" toggle-password></sl-input>
2020-07-15 21:30:37 +00:00
<br>
2020-08-25 20:23:33 +00:00
<sl-input type="password" placeholder="Password Toggle" size="medium" toggle-password></sl-input>
2020-07-15 21:30:37 +00:00
<br>
2020-08-25 20:23:33 +00:00
<sl-input type="password" placeholder="Password Toggle" size="large" toggle-password></sl-input>
2020-07-15 21:30:37 +00:00
```
### Pill
Use the `pill` prop to give inputs rounded edges.
```html preview
<sl-input placeholder="Small" size="small" pill></sl-input>
<br>
<sl-input placeholder="Medium" size="medium" pill></sl-input>
<br>
<sl-input placeholder="Large" size="large" pill></sl-input>
```
2020-08-25 20:23:33 +00:00
### Disabled
Use the `disabled` attribute to disable an input.
```html preview
<sl-input placeholder="Disabled" size="small" disabled></sl-input>
<br>
<sl-input placeholder="Disabled" size="medium" disabled></sl-input>
<br>
<sl-input placeholder="Disabled" size="large" disabled></sl-input>
```
### Sizes
Use the `size` attribute to change an input's size.
```html preview
<sl-input placeholder="Small" size="small"></sl-input>
<br>
<sl-input placeholder="Medium" size="medium"></sl-input>
<br>
<sl-input placeholder="Large" size="large"></sl-input>
```
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">
<sl-icon name="tag" slot="prefix"></sl-icon>
<sl-icon name="gear" slot="suffix"></sl-icon>
</sl-input>
<br>
<sl-input placeholder="Medium" size="medium">
<sl-icon name="tag" slot="prefix"></sl-icon>
<sl-icon name="gear" slot="suffix"></sl-icon>
</sl-input>
<br>
<sl-input placeholder="Large" size="large">
<sl-icon name="tag" slot="prefix"></sl-icon>
<sl-icon name="gear" slot="suffix"></sl-icon>
</sl-input>
```
2020-08-25 20:23:33 +00:00
### Labels
2020-07-15 21:30:37 +00:00
2020-08-25 20:23:33 +00:00
Use the `label` attribute to give the input an accessible label.
2020-07-15 21:30:37 +00:00
```html preview
2020-08-25 20:23:33 +00:00
<sl-input label="Name"></sl-input>
2020-07-15 21:30:37 +00:00
<br>
2020-08-25 20:23:33 +00:00
<sl-input type="email" label="Email" placeholder="bob@example.com"></sl-input>
2020-07-15 21:30:37 +00:00
```
2020-08-25 20:23:33 +00:00
### Help Text
2020-07-15 21:30:37 +00:00
2020-08-25 20:23:33 +00:00
Add descriptive help text to an input with the `help-text` slot.
2020-07-15 21:30:37 +00:00
```html preview
2020-08-25 20:23:33 +00:00
<sl-input label="Nickname">
<div slot="help-text">What would you like people to call you?</div>
</sl-input>
2020-07-15 21:30:37 +00:00
```
### Inputs with Dropdowns
Dropdowns can be used in the `prefix` or `suffix` slot to make inputs more versatile. Make sure to use the `hoist` prop so the dropdown breaks out of the input's overflow.
```html preview
<div class="input-dropdowns">
<sl-input type="tel" label="Phone">
<sl-icon slot="prefix" name="telephone"></sl-icon>
<sl-dropdown slot="suffix" placement="bottom-end" hoist>
<sl-button slot="trigger" caret type="text" size="small">Home</sl-button>
<sl-menu>
<sl-menu-item checked>Home</sl-menu-item>
<sl-menu-item>Mobile</sl-menu-item>
<sl-menu-item>Work</sl-menu-item>
</sl-menu>
</sl-dropdown>
<div slot="help-text">
Please enter a phone number where we can reach you.
</div>
</sl-input>
</div>
<script>
const container = document.querySelector('.input-dropdowns');
const dropdown = container.querySelector('sl-dropdown');
const trigger = dropdown.querySelector('sl-button');
2020-10-09 21:45:16 +00:00
dropdown.addEventListener('sl-select', event => {
const selectedItem = event.detail.item;
trigger.textContent = selectedItem.textContent;
[...dropdown.querySelectorAll('sl-menu-item')].map(item => item.checked = item === selectedItem);
});
</script>
<style>
.input-dropdowns sl-dropdown {
margin-right: .25rem;
}
</style>
```
2020-07-15 21:30:37 +00:00
[component-metadata:sl-input]