fix(checkbox-group): throw if name does not contain []
This commit is contained in:
parent
4eed787dea
commit
5957cd9165
5 changed files with 66 additions and 3 deletions
|
|
@ -35,3 +35,23 @@ import '@lion/checkbox-group/lion-checkbox-group.js';
|
|||
<lion-checkbox label="Marie Curie" .choiceValue=${'Marie Curie'}></lion-checkbox>
|
||||
</lion-checkbox-group>
|
||||
```
|
||||
|
||||
### Model value
|
||||
|
||||
The `modelValue` of a `lion-checkbox-group` is an array containing the `choiceValues` of the `lion-checkbox` elements that have been checked.
|
||||
|
||||
Given the scientists example above, say that we were to select the first and last options (Archimedes & Marie Curie).
|
||||
|
||||
Then the `modelValue` of the `lion-checkbox-group` will look as follows:
|
||||
|
||||
```js
|
||||
const groupElement = [parent].querySelector('lion-checkbox-group');
|
||||
groupElement.modelValue;
|
||||
=> ["Archimedes", "Marie Curie"];
|
||||
```
|
||||
|
||||
### The `name` attribute
|
||||
|
||||
The `name` attribute of a `lion-checkbox-group` automatically gets assigned to its `lion-checkbox` children. You can also specify names for the `lion-checkbox` elements, but if this name is different from the name assigned to `lion-checkbox-group`, then an exception will be thrown.
|
||||
|
||||
Our recommendation would be to set the `name` attribute only on the `lion-checkbox-group` and not on the `lion-checkbox` elements.
|
||||
|
|
|
|||
|
|
@ -6,4 +6,11 @@ export class LionCheckboxGroup extends ChoiceGroupMixin(LionFieldset) {
|
|||
super();
|
||||
this.multipleChoice = true;
|
||||
}
|
||||
|
||||
updated(changedProperties) {
|
||||
super.updated(changedProperties);
|
||||
if (changedProperties.has('name') && !String(this.name).match(/\[\]$/)) {
|
||||
throw new Error('Names should end in "[]".');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -53,4 +53,21 @@ describe('<lion-checkbox-group>', () => {
|
|||
`);
|
||||
await expect(el).to.be.accessible();
|
||||
});
|
||||
|
||||
it("should throw exception if name doesn't end in []", async () => {
|
||||
const el = await fixture(
|
||||
html`
|
||||
<lion-checkbox-group name="woof[]"></lion-checkbox-group>
|
||||
`,
|
||||
);
|
||||
el.name = 'woof';
|
||||
let err;
|
||||
try {
|
||||
await el.updateComplete;
|
||||
} catch (e) {
|
||||
err = e;
|
||||
}
|
||||
expect(err).to.be.an.instanceof(Error);
|
||||
expect(err.message).to.equal('Names should end in "[]".');
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { Story, Preview, Meta, html } from '@open-wc/demoing-storybook';
|
||||
import '@lion/checkbox-group/lion-checkbox-group.js';
|
||||
import { Story, Meta, html } from '@open-wc/demoing-storybook';
|
||||
import '@lion/checkbox/lion-checkbox.js';
|
||||
import '@lion/fieldset/lion-fieldset.js';
|
||||
import '@lion/form/lion-form.js';
|
||||
|
|
@ -59,7 +58,7 @@ For usage and installation please see the appropriate packages.
|
|||
<lion-input-email name="email" label="Email"></lion-input-email>
|
||||
<lion-checkbox-group
|
||||
label="What do you like?"
|
||||
name="checkers"
|
||||
name="checkers[]"
|
||||
.validators="${[new Required()]}"
|
||||
>
|
||||
<lion-checkbox .choiceValue=${'foo'} label="I like foo"></lion-checkbox>
|
||||
|
|
|
|||
|
|
@ -35,3 +35,23 @@ import '@lion/radio-group/lion-radio-group.js';
|
|||
|
||||
- Make sure that to use a name attribute as it is necessary for the [lion-form](../form)'s serialization result.
|
||||
- If you have many options for a user to pick from, consider using [`lion-select`](../select) instead
|
||||
|
||||
### Model value
|
||||
|
||||
The `modelValue` of a `lion-radio-group` is string equal to the `choiceValue` of the `lion-radio` element that has been checked.
|
||||
|
||||
Given the dinosaur example above, say that we were to select the last option (diplodocus).
|
||||
|
||||
Then the `modelValue` of the `lion-radio-group` will look as follows:
|
||||
|
||||
```js
|
||||
const groupElement = [parent].querySelector('lion-radio-group');
|
||||
groupElement.modelValue;
|
||||
=> "diplodocus";
|
||||
```
|
||||
|
||||
### The `name` attribute
|
||||
|
||||
The `name` attribute of a `lion-radio-group` automatically gets assigned to its `lion-radio` children. You can also specify names for the `lion-radio` elements, but if this name is different from the name assigned to `lion-radio-group`, then an exception will be thrown.
|
||||
|
||||
Our recommendation would be to set the `name` attribute only on the `lion-radio-group` and not on the `lion-checkbox` elements.
|
||||
|
|
|
|||
Loading…
Reference in a new issue