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

Adds horizontal variant in CheckboxGroup #16583

Merged
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'],
},
};
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`
);
});
});
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;
2nikhiltom marked this conversation as resolved.
Show resolved Hide resolved

.#{$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;
}
}
}
Loading