Skip to content

Commit

Permalink
feat: ✨ add optional prop defaultValue (#1560)
Browse files Browse the repository at this point in the history
Co-authored-by: Maximilian Franzke <[email protected]>
  • Loading branch information
annsch and mfranzke authored Sep 14, 2023
1 parent 611f792 commit 5d357b7
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,14 @@ export default function DBCheckbox(props: DBCheckboxProps) {
// It has no accessibility or UX implications. (https://mui.com/material-ui/react-checkbox/)
checkboxElement.indeterminate = props.indeterminate;
}

if (props.defaultChecked !== undefined) {
// only set by JS: https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement#instance_properties_that_apply_only_to_elements_of_type_checkbox_or_radio
checkboxElement.defaultChecked = props.defaultChecked;
}
}
}
}, [state.initialized, props.indeterminate, props.checked]);
}, [state.initialized, props.indeterminate, props.checked, props.defaultChecked]);

return (
<>
Expand Down
7 changes: 7 additions & 0 deletions packages/components/src/shared/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ export type FormCheckProps = {
* Define the radio or checkbox elements checked state
*/
checked?: boolean;

/**
* Returns / Sets the default state of a radio button or checkbox as originally specified in HTML that created this object.
* Vue: according to our research this property should not be used. Please refer to v-model instead.
* cf. https://react.carbondesignsystem.com/?path=/docs/components-checkbox--overview#component-api vs. https://vue.carbondesignsystem.com/?path=/story/components-cvcheckbox--default
*/
defaultChecked?: boolean;
};

export type FormMessageProps = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<!-- [html-validate-disable attr-case] -->
<div class="form-container">
<div>
<db-input
Expand Down Expand Up @@ -60,7 +61,7 @@
<h6>Reactive Forms Example:</h6>
<form [formGroup]="form" (submit)="onFormSubmit()">
<fieldset>
<p>Input:</p>
<legend>Input:</legend>

<db-input
name="form"
Expand Down Expand Up @@ -137,6 +138,7 @@ <h6>Reactive Forms Example:</h6>
(change)="handleChange1($event)"
></db-checkbox>
<fieldset>
<legend>Checkboxes</legend>
<db-checkbox
[checked]="checked[0]"
(change)="handleChange2($event)"
Expand All @@ -150,6 +152,11 @@ <h6>Reactive Forms Example:</h6>
label="Checkbox"
></db-checkbox>
</fieldset>
<db-checkbox
name="checkbox4"
label="Checkbox with defaultValue"
[defaultChecked]="true"
></db-checkbox>
<p>Select:</p>
<db-select
name="select"
Expand Down
6 changes: 6 additions & 0 deletions showcases/react-showcase/src/components/form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ const FormComponent = () => {
onChange={handleChange3}>
Checkbox
</DBCheckbox>
<DBCheckbox
name="checkbox-3"
value="Irgendwas"
defaultChecked={false}>
DefaultChecked
</DBCheckbox>
</fieldset>
<p>DBSelect:</p>
<DBSelect
Expand Down

0 comments on commit 5d357b7

Please sign in to comment.