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

Handle checkbox indeterminate state at the DOM level #73

Merged
merged 2 commits into from Jan 22, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/flame/CHANGELOG.md
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

Refer to the [CONTRIBUTING guide](https://github.com/lightspeed/flame/blob/master/.github/CONTRIBUTING.md) for more info.

## [Unreleased]

### Fixed

- Add in indeterminate state at the DOM level for Checkbox component ([#73](https://github.com/lightspeed/flame/pull/73))

## 1.2.10 - 2019-12-17

### Fixed
Expand Down
9 changes: 9 additions & 0 deletions packages/flame/src/Checkbox/Checkbox.test.tsx
Expand Up @@ -89,4 +89,13 @@ describe('<Checkbox />', () => {
getByText('my description');
});
});

describe('Behaviours', () => {
it('handles indeterminate state at the DOM level', () => {
const ref = React.createRef<HTMLInputElement>();
customRender(<Checkbox indeterminate ref={ref} />);

expect(ref.current.indeterminate).toBeTruthy();
});
});
});
42 changes: 27 additions & 15 deletions packages/flame/src/Checkbox/Checkbox.tsx
Expand Up @@ -100,21 +100,33 @@ export interface BaseCheckboxProps extends React.InputHTMLAttributes<HTMLInputEl
checked?: boolean;
}
export const BaseCheckbox = React.forwardRef<HTMLInputElement, BaseCheckboxProps>(
({ indeterminate, checked, css, className, ...restProps }, ref) => (
<Wrapper css={css} className={className}>
<CheckboxInput
ref={ref}
type="checkbox"
indeterminate={indeterminate}
checked={checked}
{...restProps}
/>
<CheckboxCheckmarkWrapper indeterminate={indeterminate}>
<StyledIcon size="0.65rem" />
{indeterminate && !checked && <CheckboxIndeterminate />}
</CheckboxCheckmarkWrapper>
</Wrapper>
),
({ indeterminate, checked, css, className, ...restProps }, ref) => {
return (
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there a need to go from an implicit to explicit return?

<Wrapper css={css} className={className}>
<CheckboxInput
ref={innerRef => {
if (innerRef) {
// eslint-disable-next-line no-param-reassign
innerRef.indeterminate = !checked && indeterminate;
}

if (ref) {
// @ts-ignore
typeof ref === 'function' ? ref(innerRef) : (ref.current = innerRef); // eslint-disable-line no-param-reassign
}
}}
type="checkbox"
indeterminate={indeterminate}
checked={checked}
{...restProps}
/>
<CheckboxCheckmarkWrapper indeterminate={indeterminate}>
<StyledIcon size="0.65rem" />
{indeterminate && !checked && <CheckboxIndeterminate />}
</CheckboxCheckmarkWrapper>
</Wrapper>
);
},
);

export interface CheckboxProps extends BaseCheckboxProps {
Expand Down