logo dile-components dile-components

dile-select-boolean

This component wraps dile-select to handle boolean form fields. It solves the problem that occurs when DileForm sets a boolean value (true/false) on a select field: dile-select declares its value as a String and cannot match the option correctly.

dile-select-boolean exposes a boolean value property and renders two fixed options internally. The display text for each option is configurable via properties.

Installation

npm i @dile/ui

Usage

Import the component.

import '@dile/ui/components/select/select-boolean.js';

Use the component.

<dile-select-boolean
  name="active"
  label="Active"
  trueOption="Yes"
  falseOption="No"
></dile-select-boolean>

Properties

Methods

Events

DileForm integration

dile-select-boolean is designed to work with DileForm:

Native form behavior

When used inside a native HTML <form>, the component behaves like a checkbox:

CSS custom properties

The component delegates all rendering to the inner dile-select. See the dile-select documentation for the full list of available CSS custom properties.

dile-select-boolean demos

Default (Yes / No)

<script type="module">
import { LitElement, html } from 'lit';
import '@dile/ui/components/select/select-boolean.js';

class MyComponent extends LitElement {
  render() {
    return html`
      <dile-select-boolean id="sb1" name="active" label="Active"></dile-select-boolean>
      <p id="msg1">Select a value</p>
    `;
  }
  firstUpdated() {
    this.shadowRoot.getElementById('sb1').addEventListener('element-changed', (e) => {
      const msg = this.shadowRoot.getElementById('msg1');
      msg.innerText = 'Value: ' + e.detail.value + ' (type: ' + typeof e.detail.value + ')';
    });
  }
}
customElements.define('my-component', MyComponent);
</script>
<my-component></my-component>

Custom option labels

<script type="module">
import '@dile/ui/components/select/select-boolean.js';
</script>
<dile-select-boolean
  name="published"
  label="Status"
  trueOption="Published"
  falseOption="Draft"
></dile-select-boolean>

Set boolean value programmatically

<script type="module">
import { LitElement, html } from 'lit';
import '@dile/ui/components/select/select-boolean.js';

class MyComponent2 extends LitElement {
  render() {
    return html`
      <dile-select-boolean id="sb3" name="enabled" label="Enabled"></dile-select-boolean>
      <p>
        <button @click="${() => this.shadowRoot.getElementById('sb3').value = true}">Set true</button>
        <button @click="${() => this.shadowRoot.getElementById('sb3').value = false}">Set false</button>
      </p>
    `;
  }
}
customElements.define('my-component-2', MyComponent2);
</script>
<my-component-2></my-component-2>

Errored state

<script type="module">
import '@dile/ui/components/select/select-boolean.js';
</script>
<dile-select-boolean
  name="confirm"
  label="Confirm"
  errored
  hideErrorOnInput
  message="This field is required"
></dile-select-boolean>

Disabled

<script type="module">
import '@dile/ui/components/select/select-boolean.js';
</script>
<dile-select-boolean name="status" label="Status" disabled></dile-select-boolean>