Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: adds lion-select docs about the repeat directive #2402

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions docs/components/select/use-cases.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,63 @@ Or by setting the `disabled` attribute on the entire `lion-select` field.
## Validation

A validator can be used to make it e.g. `required`. If you want to know how to do that, please take a look at our [validation examples](../../fundamentals/systems/form/validate.md).

## Rendering and updating select options

A very common pattern is to keep the options in an array, and render them through a repeating template.

```js
import { html, LitElement } from 'lit';

class MyOptions extends LitElement {
static properties = {
colours: Array,
};

constructor() {
super();

this.colours = ['red', 'hotpink', 'teal'];
}

render() {
return html`
<lion-select name="favoriteColor" label="Favorite color" disabled>
<select slot="input">
<option selected hidden value>Please select</option>
${this.colours.map(colour => html`<option value="${colour}">${colour}</option>`)}
</select>
</lion-select>
`;
}
}
```

Using the above method to build repeating templates is usually efficient. However, there are cases where you might want to update the list of options after an option has already been selected.

For example, if we add a button to sort the array alphabetically, pressing it would update the list. However, the selected state and index would not reset or update accordingly.

Lit is intelligent and updates the DOM with minimal effort. During a rerender, it only updates the values inside the template and doesn't check state values outside the template, such as the selected item from the parent element.

So, if the first option was selected, the newly selected option would be whatever ends up in that position after sorting. This creates a confusing user experience where a seemingly random value is selected.

The Lit `repeat` directive fixes this problem by attaching a unique ID to each DOM element using an item as the identifier, `colour` in our example. Any changes will then result in a complete rerender or reordering of the DOM nodes, rather than just changing the values of those nodes.

```js
import { repeat } from 'lit/directives/repeat.js';

render() {
return html`
<lion-select name="favoriteColor" label="Favorite color" disabled>
<select slot="input">
<option selected hidden value>Please select</option>
${repeat(
this.colours,
(colour) => colour,
(colour, index) => html`<option value="${colour}">${colour}</option>`
)}
</select>
</lion-select>
`;
}
```
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.