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

Adding props to Checkbox component #1371

Merged
merged 49 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
e312ac8
checkbox wrapper props input
xman343 Jun 20, 2023
f8d1184
label prop modified
xman343 Jun 20, 2023
d75903c
Merge branch 'dev' of https://github.com/iTwin/iTwinUI into adding-in…
xman343 Jun 21, 2023
14f1802
added box props
xman343 Jun 21, 2023
e71378c
removed boxProps
xman343 Jun 22, 2023
35e1aab
react unit test added
xman343 Jun 22, 2023
e76e086
isomorphically applies style on label & input
xman343 Jun 22, 2023
07d8b42
iso applies classname and stops prop correctly
xman343 Jun 22, 2023
d03f5e3
updated docs
xman343 Jun 23, 2023
a859357
Merge branch 'dev' of https://github.com/iTwin/iTwinUI into adding-in…
xman343 Jun 26, 2023
269c05b
Merge branch 'dev' into adding-input-props-to-components
xman343 Jun 26, 2023
6074638
Merge branch 'dev' into adding-input-props-to-components
xman343 Jun 26, 2023
0bc5750
Merge branch 'dev' into adding-input-props-to-components
xman343 Jun 28, 2023
66c5e0b
Merge branch 'dev' into adding-input-props-to-components
xman343 Jun 28, 2023
8250455
added changeset
xman343 Jun 28, 2023
993e337
Merge branch 'adding-input-props-to-components' of https://github.com…
xman343 Jun 28, 2023
8e12a2a
redundant style dec removed
xman343 Jun 28, 2023
b6bf85b
removed wrapperstyle and className check
xman343 Jun 28, 2023
84c5f27
removed comment
xman343 Jun 28, 2023
af03a20
concise style dec
xman343 Jun 29, 2023
761f12d
readded className
xman343 Jun 29, 2023
99bdb04
added labelProps
xman343 Jun 29, 2023
4fc3b6c
updated unit testing
xman343 Jun 29, 2023
08e9218
Merge branch 'dev' into adding-input-props-to-components
xman343 Jun 29, 2023
4492d0a
Merge branch 'adding-input-props-to-components' of https://github.com…
xman343 Jun 29, 2023
004c51d
Merge branch 'dev' into adding-input-props-to-components
xman343 Jun 30, 2023
903041f
Merge branch 'adding-input-props-to-components' of https://github.com…
xman343 Jun 30, 2023
cba1dec
added inputProps
xman343 Jun 30, 2023
6465595
Merge branch 'dev' into adding-input-props-to-components
xman343 Jun 30, 2023
c720554
removed input props
xman343 Jun 30, 2023
5047c83
Merge branch 'dev' into adding-input-props-to-components
xman343 Jun 30, 2023
cc873d9
removed unneeded wrapperProps call from final test
xman343 Jul 3, 2023
dc0a725
changed labelProps el from label to span
xman343 Jul 3, 2023
3991a19
Merge branch 'dev' of https://github.com/iTwin/iTwinUI into adding-in…
xman343 Jul 3, 2023
8508b5b
Testing image changes
adamhock Jul 3, 2023
1146989
react test image approval
xman343 Jul 3, 2023
38aad1e
image approvals
xman343 Jul 3, 2023
ed153b1
updated changeset
xman343 Jul 5, 2023
f6ca047
removed checks
xman343 Jul 5, 2023
cc09c83
Update packages/itwinui-react/src/core/Checkbox/Checkbox.tsx
xman343 Jul 5, 2023
af3d20c
updated unit tests
xman343 Jul 5, 2023
9ad1f9d
combined className wrapperProps & labelProps tests
xman343 Jul 5, 2023
1ced362
Merge branch 'dev' of https://github.com/iTwin/iTwinUI into adding-in…
xman343 Jul 6, 2023
af2176b
Merge branch 'dev' into adding-input-props-to-components
xman343 Jul 6, 2023
8b6050b
className case added
xman343 Jul 6, 2023
8599475
reverted react visual test images
xman343 Jul 6, 2023
e568114
updated className test
xman343 Jul 6, 2023
c77d7f3
updated style test
xman343 Jul 6, 2023
44f56d3
Update .changeset/empty-rockets-try.md
xman343 Jul 6, 2023
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/empty-rockets-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@itwin/itwinui-react': major
---

Checkbox: `className` and `style` will now always be applied on the checkbox input element. Added new `wrapperProps` and `labelProps` to allow for styling of wrapper and label elements.
97 changes: 38 additions & 59 deletions packages/itwinui-react/src/core/Checkbox/Checkbox.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import * as React from 'react';

import { Checkbox } from './Checkbox.js';
Expand All @@ -19,7 +18,7 @@ it('renders correctly with label', () => {

assertBaseElements(container);
expect(container.querySelector('label')).toHaveClass('iui-checkbox-wrapper');
expect(screen.getByText('Some checkbox')).toHaveClass('iui-checkbox-label');
expect(container.querySelector('label')).toHaveTextContent('Some checkbox');
});

it('renders correctly indeterminate state', () => {
Expand Down Expand Up @@ -109,35 +108,6 @@ it('renders negative component', () => {
).toBeTruthy();
});

it.each(['label', 'input'] as const)(
'should isomorphically apply style on %s',
(el) => {
const { container } = render(
<Checkbox
label={el === 'label' ? 'Some label' : undefined}
style={{ color: 'blue' }}
/>,
);

assertBaseElements(container);
expect(container.querySelector(el)).toHaveStyle('color: blue;');
},
);
it.each(['label', 'input'] as const)(
'should isomorphically apply class on %s',
(el) => {
const { container } = render(
<Checkbox
label={el === 'label' ? 'Some label' : undefined}
className='customClass'
/>,
);

assertBaseElements(container);
expect(container.querySelector(el)).toHaveClass('customClass');
},
);

it('should set focus', () => {
let element: HTMLInputElement | null = null;
const onRef = (ref: HTMLInputElement) => {
Expand Down Expand Up @@ -187,31 +157,40 @@ it('renders correctly with visibility checkbox', () => {
expect(container.querySelector('.iui-checkbox-visibility')).toBeTruthy();
});

it.each(['', 'not'] as const)(
'should %s stop propagation correctly if %s used with label',
async (labelPresent) => {
const wrapperOnClick = jest.fn();
const checkboxOnChange = jest.fn();
const { container } = render(
<div onClick={wrapperOnClick}>
<Checkbox
label={labelPresent && 'label'}
className='my-checkbox'
onClick={(e) => e.stopPropagation()}
onChange={checkboxOnChange}
/>
</div>,
);
const checkboxComponent = container.querySelector(
'.my-checkbox',
) as HTMLElement;
await userEvent.click(checkboxComponent);

expect(checkboxOnChange).toBeCalled();
if (labelPresent) {
expect(wrapperOnClick).toBeCalled();
} else {
expect(wrapperOnClick).not.toBeCalled();
}
},
);
it('correctly passes className through wrapperProps and labelProps', () => {
const { container } = render(
<Checkbox
label='some label'
wrapperProps={{ className: 'some-wrapper' }}
labelProps={{ className: 'some-label' }}
className='some-input'
/>,
);

assertBaseElements(container);
expect(container.querySelector('label')).toHaveClass(
'iui-checkbox-wrapper some-wrapper',
);
expect(container.querySelector('span')).toHaveClass(
'iui-checkbox-label some-label',
);
expect(container.querySelector('.iui-checkbox')).toHaveClass(
'iui-checkbox some-input',
);
});

it('correctly passes style through wrapperProps and labelProps', () => {
const { container } = render(
<Checkbox
label='some label'
wrapperProps={{ style: { color: 'blue' } }}
labelProps={{ style: { color: 'orange' } }}
style={{ color: 'yellow' }}
/>,
);

assertBaseElements(container);
expect(container.querySelector('label')).toHaveStyle('color: blue');
expect(container.querySelector('span')).toHaveStyle('color: orange');
expect(container.querySelector('.iui-checkbox')).toHaveStyle('color: yellow');
});
28 changes: 23 additions & 5 deletions packages/itwinui-react/src/core/Checkbox/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ type CheckboxProps = {
* @default false
*/
isLoading?: boolean;
/**
* Passes properties for checkbox wrapper.
*/
wrapperProps?: React.ComponentProps<'label'>;
/**
* Passes properties for checkbox label.
*/
labelProps?: React.ComponentProps<'span'>;
};

/**
Expand All @@ -60,6 +68,8 @@ export const Checkbox = React.forwardRef((props, ref) => {
variant = 'default',
setFocus,
isLoading = false,
wrapperProps = {},
labelProps = {},
style,
...rest
} = props;
Expand Down Expand Up @@ -92,9 +102,9 @@ export const Checkbox = React.forwardRef((props, ref) => {
'iui-checkbox-visibility': variant === 'eyeball',
'iui-loading': isLoading,
},
className && { [className]: !label },
className,
)}
style={!label ? style : undefined}
style={style}
disabled={disabled || isLoading}
type='checkbox'
ref={refs}
Expand All @@ -104,6 +114,10 @@ export const Checkbox = React.forwardRef((props, ref) => {
</>
);

const { className: wrapperClassName, ...restWrapperProps } = wrapperProps;

const { className: labelClassName, ...restLabelProps } = labelProps;

return !label ? (
checkbox
) : (
Expand All @@ -116,13 +130,17 @@ export const Checkbox = React.forwardRef((props, ref) => {
[`iui-${status}`]: !!status,
'iui-loading': isLoading,
},
className,
wrapperClassName,
)}
style={style}
{...restWrapperProps}
>
{checkbox}
{label && (
<Box as='span' className='iui-checkbox-label'>
<Box
as='span'
className={cx('iui-checkbox-label', labelClassName)}
{...restLabelProps}
>
{label}
</Box>
)}
Expand Down