Co-authored-by: Joren Broekema <Joren.Broekema@ing.com> Co-authored-by: Alex Ghiu <Alex.Ghiu@ing.com> Co-authored-by: Thijs Louisse <Thijs.Louisse@ing.com>
342 lines
9.6 KiB
Text
342 lines
9.6 KiB
Text
import { Story, Meta, html } from '@open-wc/demoing-storybook';
|
|
import { Required, Validator, loadDefaultFeedbackMessages } from '@lion/validate';
|
|
import '@lion/checkbox/lion-checkbox.js';
|
|
|
|
import '../lion-checkbox-group.js';
|
|
|
|
<Meta title="Forms/Checkbox Group" parameters={{ component: 'lion-checkbox-group' }} />
|
|
|
|
# Checkbox Group
|
|
|
|
`lion-checkbox-group` component enhances the functionality of the native `<input type="checkbox">` element.
|
|
Its purpose is to provide a way for users to check **multiple** options amongst a set of choices, or to function as a single toggle.
|
|
|
|
> You should use <a href="https://github.com/ing-bank/lion/tree/master/packages/checkbox" target="_blank">`<lion-checkbox>`</a> elements as the children of the `<lion-checkbox-group>`.
|
|
|
|
<Story name="Default">
|
|
{html`
|
|
<lion-checkbox-group
|
|
name="scientistsGroup"
|
|
label="Favorite scientists"
|
|
>
|
|
<lion-checkbox name="scientists[]" label="Archimedes" .choiceValue=${'Archimedes'}></lion-checkbox>
|
|
<lion-checkbox name="scientists[]" label="Francis Bacon" .choiceValue=${'Francis Bacon'}></lion-checkbox>
|
|
<lion-checkbox name="scientists[]" label="Marie Curie" .choiceValue=${'Marie Curie'}></lion-checkbox>
|
|
</lion-checkbox-group>
|
|
`}
|
|
</Story>
|
|
|
|
```html
|
|
<lion-checkbox-group
|
|
name="scientistsGroup"
|
|
label="Favourite scientists"
|
|
>
|
|
<lion-checkbox name="scientists[]" label="Archimedes" .choiceValue=${'Archimedes'}></lion-checkbox>
|
|
<lion-checkbox name="scientists[]" label="Francis Bacon" .choiceValue=${'Francis Bacon'}></lion-checkbox>
|
|
<lion-checkbox name="scientists[]" label="Marie Curie" .choiceValue=${'Marie Curie'}></lion-checkbox>
|
|
</lion-checkbox-group>
|
|
```
|
|
|
|
> Make sure that the checkbox-group also has a name attribute, this is necessary for the [lion-form](?path=/docs/forms-form)'s serialization result.
|
|
|
|
## Features
|
|
|
|
Since it extends from [lion-fieldset](?path=/docs/forms-fieldset), it has all the features a fieldset has.
|
|
|
|
## How to use
|
|
|
|
### Installation
|
|
|
|
```sh
|
|
npm i --save @lion/checkbox @lion/checkbox-group
|
|
```
|
|
|
|
```js
|
|
import '@lion/checkbox/lion-checkbox.js';
|
|
import '@lion/checkbox-group/lion-checkbox-group.js';
|
|
```
|
|
|
|
## Pre-select
|
|
|
|
You can pre-select options by targeting the `modelValue` object of the option and setting the `checked` property to `true`.
|
|
|
|
<Story name="Pre-select">
|
|
{html`
|
|
<lion-checkbox-group name="scientistsGroup" label="Favorite scientists">
|
|
<lion-checkbox
|
|
name="scientists[]"
|
|
label="Archimedes"
|
|
.choiceValue=${'Archimedes'}
|
|
></lion-checkbox>
|
|
<lion-checkbox
|
|
name="scientists[]"
|
|
label="Francis Bacon"
|
|
.choiceValue=${'Francis Bacon'}
|
|
checked
|
|
></lion-checkbox>
|
|
<lion-checkbox
|
|
name="scientists[]"
|
|
label="Marie Curie"
|
|
.modelValue=${{ value: 'Marie Curie', checked: true }}
|
|
></lion-checkbox>
|
|
</lion-checkbox-group>
|
|
`}
|
|
</Story>
|
|
|
|
```html
|
|
<lion-checkbox-group name="scientistsGroup" label="Favorite scientists">
|
|
<lion-checkbox
|
|
name="scientists[]"
|
|
label="Archimedes"
|
|
.choiceValue=${'Archimedes'}
|
|
></lion-checkbox>
|
|
<lion-checkbox
|
|
name="scientists[]"
|
|
label="Francis Bacon"
|
|
.choiceValue=${'Francis Bacon'}
|
|
checked
|
|
></lion-checkbox>
|
|
<lion-checkbox
|
|
name="scientists[]"
|
|
label="Marie Curie"
|
|
.modelValue=${{ value: 'Marie Curie', checked: true }}
|
|
></lion-checkbox>
|
|
</lion-checkbox-group>
|
|
```
|
|
|
|
## Disabled
|
|
|
|
You can disable the entire group by setting the `disabled` attribute on the `<lion-checkbox-group>`.
|
|
|
|
<Story name="Disabled">
|
|
{html`
|
|
<lion-checkbox-group name="scientistsGroup" label="Favorite scientists" disabled>
|
|
<lion-checkbox
|
|
name="scientists[]"
|
|
label="Archimedes"
|
|
.choiceValue=${'Archimedes'}
|
|
></lion-checkbox>
|
|
<lion-checkbox
|
|
name="scientists[]"
|
|
label="Francis Bacon"
|
|
.choiceValue=${'Francis Bacon'}
|
|
></lion-checkbox>
|
|
<lion-checkbox
|
|
name="scientists[]"
|
|
label="Marie Curie"
|
|
.modelValue=${{ value: 'Marie Curie', checked: true }}
|
|
></lion-checkbox>
|
|
</lion-checkbox-group>
|
|
`}
|
|
</Story>
|
|
|
|
```html
|
|
<lion-checkbox-group name="scientistsGroup" label="Favorite scientists" disabled>
|
|
<lion-checkbox
|
|
name="scientists[]"
|
|
label="Archimedes"
|
|
.choiceValue=${'Archimedes'}
|
|
></lion-checkbox>
|
|
<lion-checkbox
|
|
name="scientists[]"
|
|
label="Francis Bacon"
|
|
.choiceValue=${'Francis Bacon'}
|
|
></lion-checkbox>
|
|
<lion-checkbox
|
|
name="scientists[]"
|
|
label="Marie Curie"
|
|
.modelValue=${{ value: 'Marie Curie', checked: true }}
|
|
></lion-checkbox>
|
|
</lion-checkbox-group>
|
|
```
|
|
|
|
## Validation
|
|
|
|
You can apply validation to the `<lion-checkbox-group>`, similar to how you would do so in any fieldset.
|
|
The interaction states of the `<lion-checkbox-group>` are evaluated in order to hide or show feedback messages.
|
|
|
|
<Story name="Validation">
|
|
{() => {
|
|
loadDefaultFeedbackMessages();
|
|
const validate = () => {
|
|
const checkboxGroup = document.querySelector('#scientistsGroup');
|
|
checkboxGroup.submitted = !checkboxGroup.submitted;
|
|
};
|
|
return html`
|
|
<lion-checkbox-group
|
|
id="scientistsGroup"
|
|
name="scientistsGroup"
|
|
label="Favorite scientists"
|
|
.validators=${[new Required()]}
|
|
>
|
|
<lion-checkbox
|
|
name="scientists[]"
|
|
label="Archimedes"
|
|
.choiceValue=${'Archimedes'}
|
|
></lion-checkbox>
|
|
<lion-checkbox
|
|
name="scientists[]"
|
|
label="Francis Bacon"
|
|
.choiceValue=${'Francis Bacon'}
|
|
></lion-checkbox>
|
|
<lion-checkbox
|
|
name="scientists[]"
|
|
label="Marie Curie"
|
|
.choiceValue=${'Marie Curie'}
|
|
></lion-checkbox>
|
|
</lion-checkbox-group>
|
|
<button @click="${() => validate()}">Validate</button>
|
|
`;
|
|
}}
|
|
</Story>
|
|
|
|
```js
|
|
import { Required, loadDefaultFeedbackMessages } from '@lion/validate';
|
|
loadDefaultFeedbackMessages();
|
|
const validate = () => {
|
|
const checkboxGroup = document.querySelector('#scientistsGroup');
|
|
checkboxGroup.submitted = !checkboxGroup.submitted;
|
|
};
|
|
```
|
|
|
|
```html
|
|
<lion-checkbox-group
|
|
id="scientistsGroup"
|
|
name="scientistsGroup"
|
|
label="Favorite scientists"
|
|
.validators=${[new Required()]}
|
|
>
|
|
<lion-checkbox
|
|
name="scientists[]"
|
|
label="Archimedes"
|
|
.choiceValue=${'Archimedes'}
|
|
></lion-checkbox>
|
|
<lion-checkbox
|
|
name="scientists[]"
|
|
label="Francis Bacon"
|
|
.choiceValue=${'Francis Bacon'}
|
|
></lion-checkbox>
|
|
<lion-checkbox
|
|
name="scientists[]"
|
|
label="Marie Curie"
|
|
.choiceValue=${'Marie Curie'}
|
|
></lion-checkbox>
|
|
</lion-checkbox-group>
|
|
<button @click="${() => validate()}">Validate</button>
|
|
```
|
|
|
|
|
|
## Validation advanced
|
|
|
|
Below is a more advanced validator on the group that evaluates the children checkboxes' checked states.
|
|
|
|
<Story name="Validation Advanced">
|
|
{() => {
|
|
loadDefaultFeedbackMessages();
|
|
class HasMinTwoChecked extends Validator {
|
|
constructor(...args) {
|
|
super(...args);
|
|
this.name = 'HasMinTwoChecked';
|
|
}
|
|
execute(value) {
|
|
let hasError = false;
|
|
const selectedValues = value['scientists[]'].filter(v => v.checked === true);
|
|
if (!(selectedValues.length >= 2)) {
|
|
hasError = true;
|
|
}
|
|
return hasError;
|
|
}
|
|
static async getMessage() {
|
|
return 'You need to select at least 2 values.';
|
|
}
|
|
}
|
|
const validate = () => {
|
|
const checkboxGroup = document.querySelector('#scientistsGroup2');
|
|
checkboxGroup.submitted = !checkboxGroup.submitted;
|
|
};
|
|
return html`
|
|
<lion-checkbox-group
|
|
id="scientistsGroup2"
|
|
name="scientistsGroup"
|
|
label="Favorite scientists"
|
|
help-text="You should have at least 2 of those"
|
|
.validators=${[new Required(), new HasMinTwoChecked()]}
|
|
>
|
|
<lion-checkbox
|
|
name="scientists[]"
|
|
label="Archimedes"
|
|
.choiceValue=${'Archimedes'}
|
|
></lion-checkbox>
|
|
<lion-checkbox
|
|
name="scientists[]"
|
|
label="Francis Bacon"
|
|
.choiceValue=${'Francis Bacon'}
|
|
></lion-checkbox>
|
|
<lion-checkbox
|
|
name="scientists[]"
|
|
label="Marie Curie"
|
|
.choiceValue=${'Marie Curie'}
|
|
></lion-checkbox>
|
|
</lion-checkbox-group>
|
|
<button @click="${() => validate()}">Validate</button>
|
|
`;
|
|
}}
|
|
</Story>
|
|
|
|
```js
|
|
import { Required, Validator, loadDefaultFeedbackMessages } from '@lion/validate';
|
|
|
|
loadDefaultFeedbackMessages();
|
|
|
|
class HasMinTwoChecked extends Validator {
|
|
constructor(...args) {
|
|
super(...args);
|
|
this.name = 'HasMinTwoChecked';
|
|
}
|
|
|
|
execute(value) {
|
|
let hasError = false;
|
|
const selectedValues = value['scientists[]'].filter(v => v.checked === true);
|
|
if (!(selectedValues.length >= 2)) {
|
|
hasError = true;
|
|
}
|
|
return hasError;
|
|
}
|
|
|
|
static async getMessage() {
|
|
return 'You need to select at least 2 values.';
|
|
}
|
|
}
|
|
|
|
const validate = () => {
|
|
const checkboxGroup = document.querySelector('#scientistsGroup');
|
|
checkboxGroup.submitted = !checkboxGroup.submitted;
|
|
};
|
|
```
|
|
|
|
```html
|
|
<lion-checkbox-group
|
|
id="scientistsGroup"
|
|
name="scientistsGroup"
|
|
label="Favorite scientists"
|
|
help-text="You should have at least 2 of those"
|
|
.validators=${[new Required(), new HasMinTwoChecked()]}
|
|
>
|
|
<lion-checkbox
|
|
name="scientists[]"
|
|
label="Archimedes"
|
|
.choiceValue=${'Archimedes'}
|
|
></lion-checkbox>
|
|
<lion-checkbox
|
|
name="scientists[]"
|
|
label="Francis Bacon"
|
|
.choiceValue=${'Francis Bacon'}
|
|
></lion-checkbox>
|
|
<lion-checkbox
|
|
name="scientists[]"
|
|
label="Marie Curie"
|
|
.choiceValue=${'Marie Curie'}
|
|
></lion-checkbox>
|
|
</lion-checkbox-group>
|
|
<button @click="${() => validate()}">Validate</button>
|
|
```
|