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

fix(checkbox): fix validation state of checkbox #904

Merged
merged 1 commit into from
Aug 15, 2024
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
10 changes: 3 additions & 7 deletions src/components/checkbox-group/checkbox/bl-checkbox.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ import { Meta, Canvas, ArgsTable, Story } from '@storybook/addon-docs';
indeterminate: {
control: 'boolean',
default: false
},
value: {
control: 'text',
},
required: {
control: 'boolean',
Expand All @@ -34,7 +31,6 @@ export const CheckboxTemplate = (args) => html`<bl-checkbox
?disabled=${args.disabled}
?checked=${args.checked}
name='${ifDefined(args.name)}'
value='${ifDefined(args.value)}'
?indeterminate=${args.indeterminate}
?required=${args.required}
invalid-text='${ifDefined(args.customInvalidText)}'>${args.label}</bl-checkbox>`;
Expand Down Expand Up @@ -135,10 +131,10 @@ Disabled state can be set via `disabled` attribute. A checkbox can be `disabled`
Checkbox component has 'required' validation rule. Also custom invalid text can be passed by 'invalid-text' attribute

<Canvas isColumn>
<Story name="Validation" args={{ label: 'Checkbox with default validation message',value:'1',required:true,name:"accept_term" }}>
<Story name="Validation" args={{ label: 'Checkbox with default validation message',required:true,name:"accept_term" }}>
{CheckboxTemplate.bind({})}
</Story>
<Story name="Validation with Custom Text" args={{ label: 'Checkbox with default custom validation message',value:'1',required:true,name:"accept_term",customInvalidText:'This field is mandatory' }}>
<Story name="Validation with Custom Text" args={{ label: 'Checkbox with default custom validation message',required:true,name:"accept_term",customInvalidText:'This field is mandatory' }}>
{CheckboxTemplate.bind({})}
</Story>
</Canvas>
Expand All @@ -148,7 +144,7 @@ Checkbox component has 'required' validation rule. Also custom invalid text can
Provide the name and the value of the checkbox element, so that its value can be set on the form element

<Canvas>
<Story name="Form Usage" args={{ label: 'Terms and conditions',value:'1',required:true,name:"accept_term" }}>
<Story name="Form Usage" args={{ label: 'Terms and conditions',required:true,name:"accept_term" }}>
{FormTemplate.bind({})}
</Story>
</Canvas>
Expand Down
9 changes: 1 addition & 8 deletions src/components/checkbox-group/checkbox/bl-checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,10 @@ export default class BlCheckbox extends FormControlMixin(LitElement) {
this.form?.removeEventListener("submit", e => this.handleSubmit(e));
}

protected firstUpdated(changedProperties: Map<string, unknown>) {
if (changedProperties.has("checked") && this.checked) {
this.value = "on";
this.setValue(this.value);
}
}

protected async updated(changedProperties: Map<string, unknown>): Promise<void> {
if (changedProperties.has("checked") && this.required) {
if (this.checked) {
this.setValue(this.value);
this.setValue("on");
} else if (!this.checked) {
this.setValue("");
}
Expand Down
Loading