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

Allow defaultChecked to be used in Checkbox #2600

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/eleven-monkeys-teach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@leafygreen-ui/checkbox': patch
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be a minor change, since it adds a feature

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay!! Should I change it manually to minor in this file or should I run the script again to change it?

---

Allow defaultChecked to be used in Checkbox
16 changes: 16 additions & 0 deletions packages/checkbox/src/Checkbox/Checkbox.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ describe('packages/checkbox', () => {
expect(checkbox.getAttribute('aria-checked')).toBe('false');
});

test('renders as checked when defaultChecked prop is set', () => {
const { checkbox, getInputValue } = renderCheckbox({
defaultChecked: true,
});
expect(getInputValue()).toBe(true);
expect(checkbox.getAttribute('aria-checked')).toBe('true');
});

test('renders as checked when the prop is set', () => {
const { checkbox, getInputValue } = renderCheckbox({ checked: true });
expect(getInputValue()).toBe(true);
Expand Down Expand Up @@ -144,5 +152,13 @@ describe('packages/checkbox', () => {
fireEvent.click(checkbox);
expect(getInputValue()).toBe(true);
});

test('checkbox becomes unchecked when clicked if defaultChecked is set', () => {
const { checkbox, getInputValue } = renderCheckbox({
defaultChecked: true,
});
fireEvent.click(checkbox);
expect(getInputValue()).toBe(false);
});
});
});
3 changes: 2 additions & 1 deletion packages/checkbox/src/Checkbox/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function Checkbox({
id: idProp,
indeterminate: indeterminateProp,
label = '',
defaultChecked = false,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe other components use initialValue (e.g. Combobox). Unsure whether consistency with other LG components or with HTML checkbox api is more valuable

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Text Area uses defaultValue. I would vote to use defaultValue across all components to meet HTML naming rule.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a strong opinion; I'll defer to @TheSonOfThomp since you regularly work in this repo.

onClick: onClickProp,
onChange: onChangeProp,
name,
Expand All @@ -59,7 +60,7 @@ function Checkbox({
const { darkMode, theme } = useDarkMode(darkModeProp);
const baseFontSize = useUpdatedBaseFontSize(baseFontSizeProp);

const [checked, setChecked] = React.useState(false);
const [checked, setChecked] = React.useState(defaultChecked);
const isChecked = React.useMemo(
() => (checkedProp != null ? checkedProp : checked),
[checkedProp, checked],
Expand Down
Loading