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

feat(design): add vertical layout support for CheckboxGroup and RadioGroup #2252

Merged
merged 1 commit into from
May 16, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,20 @@ export const Playground = {
);
},
};

export const CheckboxGroupVertical = {
render() {
const [value, setValue] = useState<string[]>([]);

function handleChange(value: Array<string | number | boolean>) {
setValue(value as string[]);
}

return (
<CheckboxGroup value={value} onChange={handleChange} direction="vertical">
<Checkbox value="test">test</Checkbox>
<Checkbox value="test1">test1</Checkbox>
</CheckboxGroup>
);
},
};
12 changes: 10 additions & 2 deletions packages/design/src/components/checkbox-group/CheckboxGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ export interface ICheckboxGroupProps {
*/
disabled?: boolean;

/**
* Direction of the radio group
* @default 'horizontal'
*/
direction?: 'horizontal' | 'vertical';

/**
* The callback function triggered when switching options
*/
Expand All @@ -54,7 +60,7 @@ export interface ICheckboxGroupProps {
* CheckboxGroup Component
*/
export function CheckboxGroup(props: ICheckboxGroupProps) {
const { children, className, style, value, disabled, onChange } = props;
const { children, className, style, value, disabled, direction = 'horizontal', onChange } = props;

const handleChange = (item: string | number | boolean) => {
if (value.includes(item)) {
Expand All @@ -64,7 +70,9 @@ export function CheckboxGroup(props: ICheckboxGroupProps) {
}
};

const _className = clsx(className, styles.checkboxGroup);
const _className = clsx(className, styles.checkboxGroup, {
[styles.checkboxGroupDirectionVertical]: direction === 'vertical',
});

return (
<div className={_className} style={style}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
.checkbox-group {
display: flex;
gap: var(--margin-sm);

&-direction {
&-vertical {
flex-direction: column;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,20 @@ export const Playground = {
);
},
};

export const RadioGroupVertical = {
render() {
const [value, setValue] = useState('');

function handleChange(value: string | number | boolean) {
setValue(value as string);
}

return (
<RadioGroup value={value} onChange={handleChange} direction="vertical">
<Radio value="test">test</Radio>
<Radio value="test1">test1</Radio>
</RadioGroup>
);
},
};
25 changes: 23 additions & 2 deletions packages/design/src/components/radio-group/RadioGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,23 @@

import React from 'react';

import clsx from 'clsx';
import type { IRadioProps } from '../radio/Radio';
import styles from './index.module.less';

export interface IRadioGroupProps {
children: React.ReactNode[];

/**
* The class name of the checkbox group
*/
className?: string;

/**
* The style of the checkbox group
*/
style?: React.CSSProperties;

/**
* Define which radio is selected
*/
Expand All @@ -33,6 +44,12 @@ export interface IRadioGroupProps {
*/
disabled?: boolean;

/**
* Direction of the radio group
* @default 'horizontal'
*/
direction?: 'horizontal' | 'vertical';

/**
* The callback function triggered when switching options
*/
Expand All @@ -43,14 +60,18 @@ export interface IRadioGroupProps {
* RadioGroup Component
*/
export function RadioGroup(props: IRadioGroupProps) {
const { children, value, disabled = false, onChange } = props;
const { children, className, style, value, disabled = false, direction = 'horizontal', onChange } = props;

const handleChange = (value: string | number | boolean) => {
onChange(value);
};

const _className = clsx(className, styles.radioGroup, {
[styles.radioGroupDirectionVertical]: direction === 'vertical',
});

return (
<div className={styles.radioGroup}>
<div className={_className} style={style}>
{React.Children.map(children, (child, index) => {
if (React.isValidElement<IRadioProps>(child)) {
return React.cloneElement(child, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
.radio-group {
display: flex;
gap: var(--margin-sm);

&-direction {
&-vertical {
flex-direction: column;
}
}
}
Loading