- @lion/button@0.7.7 - @lion/checkbox-group@0.11.0 - @lion/dialog@0.7.7 - @lion/fieldset@0.13.7 - @lion/form-core@0.2.0 - @lion/form-integrations@0.1.12 - @lion/form@0.6.7 - @lion/input-amount@0.7.7 - @lion/input-date@0.7.7 - @lion/input-datepicker@0.14.9 - @lion/input-email@0.8.7 - @lion/input-iban@0.9.7 - @lion/input-range@0.4.7 - @lion/input@0.7.7 - @lion/overlays@0.16.7 - @lion/radio-group@0.11.0 - @lion/select-rich@0.18.11 - @lion/select@0.7.7 - @lion/switch@0.10.8 - @lion/textarea@0.7.7 - @lion/tooltip@0.12.2 - @lion/validate-messages@0.1.7 |
||
|---|---|---|
| .. | ||
| src | ||
| test | ||
| CHANGELOG.md | ||
| index.js | ||
| lion-select.js | ||
| package.json | ||
| README.md | ||
Select
lion-select component is a wrapper around the native select.
You cannot use interactive elements inside the options. Avoid very long names to facilitate the understandability and perceivability for screen reader users. Sets of options where each option name starts with the same word or phrase can also significantly degrade usability for keyboard and screen reader users.
import { html } from 'lit-html';
import { Required } from '@lion/form-core';
import { loadDefaultFeedbackMessages } from '@lion/validate-messages';
import './lion-select.js';
export default {
title: 'Forms/Select',
};
loadDefaultFeedbackMessages();
export const main = () => html`
<lion-select name="favoriteColor" label="Favorite color">
<select slot="input">
<option selected hidden value>Please select</option>
<option value="red">Red</option>
<option value="hotpink">Hotpink</option>
<option value="teal">Teal</option>
</select>
</lion-select>
`;
For this form element it is important to put the slot="input" with the native select yourself, because you are responsible for filling it with <option>s.
For most other form elements in lion we do this for you, because there's no need to put html inside the native form inputs.
Features
- Catches and forwards the select events
- Can be set to required or disabled
How to use
Installation
npm i --save @lion/select
import { LionSelect } from '@lion/select';
// or
import '@lion/select/lion-select.js';
Examples
Pre-select
You can preselect an option by setting the property modelValue.
export const preSelect = () => html`
<lion-select name="favoriteColor" label="Favorite color" .modelValue=${'hotpink'}>
<select slot="input">
<option selected hidden value>Please select</option>
<option value="red">Red</option>
<option value="hotpink">Hotpink</option>
<option value="teal">Teal</option>
</select>
</lion-select>
`;
Disabled
You can disable an option by adding the disabled attribute to an option.
export const disabledOption = () => html`
<lion-select name="favoriteColor" label="Favorite color">
<select slot="input">
<option selected hidden value>Please select</option>
<option value="red">Red</option>
<option value="hotpink" disabled>Hotpink</option>
<option value="teal">Teal</option>
</select>
</lion-select>
`;
Or by setting the disabled attribute on the entire lion-select field.
export const disabledSelect = () => html`
<lion-select name="favoriteColor" label="Favorite color" disabled>
<select slot="input">
<option selected hidden value>Please select</option>
<option value="red">Red</option>
<option value="hotpink">Hotpink</option>
<option value="teal">Teal</option>
</select>
</lion-select>
`;
Validation
export const validation = () => {
const validate = () => {
const select = document.querySelector('#color');
select.submitted = !select.submitted;
};
return html`
<lion-select id="color" name="color" .validators="${[new Required()]}">
<label slot="label">Favorite color</label>
<select slot="input">
<option selected hidden value>Please select</option>
<option value="red">Red</option>
<option value="hotpink">Hotpink</option>
<option value="teal">Teal</option>
</select>
</lion-select>
<button @click="${() => validate()}">Validate</button>
`;
};