Skip to content

Commit

Permalink
Adds horizontal variant in CheckboxGroup (#16583)
Browse files Browse the repository at this point in the history
* feat(16486): adds horizontal variant in CheckboxGroup

* feat(16486): add prop to playground & styles on validation messages

* Update packages/react/src/components/CheckboxGroup/CheckboxGroup.js

* Update packages/styles/scss/components/checkbox/_checkbox.scss

* Update packages/styles/scss/components/checkbox/_checkbox.scss

* Update packages/styles/scss/components/checkbox/_checkbox.scss

* fix: updates flex styles

---------

Co-authored-by: Alison Joseph <[email protected]>
  • Loading branch information
2nikhiltom and alisonjoseph authored May 28, 2024
1 parent f7065bc commit 89907da
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,15 @@ Map {
"isRequired": true,
"type": "node",
},
"orientation": Object {
"args": Array [
Array [
"horizontal",
"vertical",
],
],
"type": "oneOf",
},
"readOnly": Object {
"type": "bool",
},
Expand Down
18 changes: 18 additions & 0 deletions packages/react/src/components/Checkbox/Checkbox.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ export const Default = () => {
);
};

export const Horizontal = () => {
return (
<CheckboxGroup
orientation="horizontal"
{...fieldsetCheckboxProps()}
helperText="Helper text goes here">
<Checkbox labelText={`Checkbox label`} id="checkbox-label-1" />
<Checkbox labelText={`Checkbox label`} id="checkbox-label-2" />
<Checkbox labelText={`Checkbox label`} id="checkbox-label-3" />
</CheckboxGroup>
);
};

export const Single = () => {
return (
<>
Expand Down Expand Up @@ -186,4 +199,9 @@ Playground.argTypes = {
disable: true,
},
},
orientation: {
description: 'Provide how checkbox should be displayed',
control: 'select',
options: ['horizontal', 'vertical'],
},
};
13 changes: 13 additions & 0 deletions packages/react/src/components/CheckboxGroup/CheckboxGroup-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,17 @@ describe('CheckboxGroup', () => {

expect(container.firstChild).toHaveClass(`${prefix}--checkbox-group--slug`);
});
it('should render checkboxes horizontally', () => {
const { container } = render(
<CheckboxGroup orientation="horizontal" legendText="test-horizental-prop">
<Checkbox labelText="Checkbox label 1" id="checkbox-label-1" />
<Checkbox labelText="Checkbox label 2" id="checkbox-label-2" />
<Checkbox labelText="Checkbox label 3" id="checkbox-label-3" />
</CheckboxGroup>
);

expect(container.firstChild).toHaveClass(
`${prefix}--checkbox-group--horizontal`
);
});
});
8 changes: 8 additions & 0 deletions packages/react/src/components/CheckboxGroup/CheckboxGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function CheckboxGroup({
warn,
warnText,
slug,
orientation = 'vertical',
...rest
}) {
const prefix = usePrefix();
Expand All @@ -46,6 +47,7 @@ function CheckboxGroup({
) : null;

const fieldsetClasses = cx(`${prefix}--checkbox-group`, className, {
[`${prefix}--checkbox-group--${orientation}`]: orientation === 'horizontal',
[`${prefix}--checkbox-group--readonly`]: readOnly,
[`${prefix}--checkbox-group--invalid`]: !readOnly && invalid,
[`${prefix}--checkbox-group--warning`]: showWarning,
Expand All @@ -67,6 +69,7 @@ function CheckboxGroup({
data-invalid={invalid ? true : undefined}
aria-labelledby={rest['aria-labelledby'] || legendId}
aria-readonly={readOnly}
orientation="vertical"
aria-describedby={!invalid && !warn && helper ? helperId : undefined}
{...rest}>
<legend
Expand Down Expand Up @@ -134,6 +137,11 @@ CheckboxGroup.propTypes = {
*/
legendText: PropTypes.node.isRequired,

/**
* Provide the orientation for how the checkbox should be displayed
*/
orientation: PropTypes.oneOf(['horizontal', 'vertical']),

/**
* Whether the CheckboxGroup should be read-only
*/
Expand Down
25 changes: 25 additions & 0 deletions packages/styles/scss/components/checkbox/_checkbox.scss
Original file line number Diff line number Diff line change
Expand Up @@ -385,4 +385,29 @@
line-height: inherit;
margin-block-start: convert.to-rem(-1px);
}

// horizontal checkbox group
.#{$prefix}--checkbox-group--horizontal {
position: relative;
display: flex;
flex-flow: row wrap;
justify-content: flex-start;

.#{$prefix}--form-item {
flex: none;
margin-block-end: 0;

&:not(:last-of-type) {
margin-inline-end: $spacing-05;
}
}

.#{$prefix}--checkbox-label-text {
padding-inline-start: $spacing-03;
}

.#{$prefix}--label + .#{$prefix}--form-item.#{$prefix}--checkbox-wrapper {
margin-block-start: 0;
}
}
}

0 comments on commit 89907da

Please sign in to comment.