Skip to content

Commit c8f9978

Browse files
committed
Add RadioCards and RadioGroup components with styles
1 parent bc8edbf commit c8f9978

File tree

13 files changed

+221
-2
lines changed

13 files changed

+221
-2
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import React, { useState } from 'react';
2+
import SandboxEditor from '~/components/tools/SandboxEditor/SandboxEditor';
3+
import RadioCards from './RadioCards';
4+
5+
const RadioCardsTemplate = (args) => {
6+
const options = [
7+
{ id: 'config-1', value: '8-core CPU', label: '8-core CPU' },
8+
{ id: 'config-2', value: '16-core CPU', label: '16-core CPU' },
9+
{ id: 'config-3', value: '32-core CPU', label: '32-core CPU' }];
10+
11+
const [language, setLanguage] = useState('css');
12+
13+
const handleChange = (e) => {
14+
setLanguage(e.target.value);
15+
};
16+
return (
17+
<SandboxEditor>
18+
19+
<RadioCards.Root defaultChecked={language} onChange={handleChange} >
20+
{options.map((option) => (
21+
<RadioCards.Item key={option.id} value={option.value}>
22+
<div>
23+
{option.label}
24+
</div>
25+
</RadioCards.Item>
26+
))}
27+
</RadioCards.Root>
28+
</SandboxEditor>
29+
);
30+
};
31+
32+
export default {
33+
title: 'Components/RadioCards',
34+
component: RadioCards,
35+
render: (args) => <RadioCardsTemplate {...args}/>
36+
};
37+
38+
export const All = {};
39+
All.args = {};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import React from 'react';
2+
import RadioCardsRoot from './fragments/RadioCardsRoot';
3+
import RadioCardsItem from './fragments/RadioCardsItem';
4+
5+
const RadioCards = {
6+
Root: RadioCardsRoot,
7+
Item: RadioCardsItem
8+
} as const;
9+
10+
export default RadioCards;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { createContext } from 'react';
2+
3+
export const RadioCardsContext = createContext({
4+
defaultChecked: null,
5+
rootClass: '',
6+
onChange: null
7+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import React, { useContext } from 'react';
2+
import RadioGroupPrimitive from '~/core/primitives/RadioGroup/RadioGroupPrimitive';
3+
import { RadioCardsContext } from '../context/RadioCardsContext';
4+
5+
import clsx from 'clsx';
6+
7+
const RadioCardsItem = ({ children, className = '', ...props }: { children: React.ReactNode } & RadioGroupPrimitive.ItemProps) => {
8+
const { defaultChecked, onChange, rootClass } = useContext(RadioCardsContext);
9+
return <RadioGroupPrimitive.Item className={clsx(`${rootClass}-item`, className)} {...props}>{children}</RadioGroupPrimitive.Item>;
10+
};
11+
12+
export default RadioCardsItem;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import React from 'react';
2+
import RadioGroupPrimitive from '~/core/primitives/RadioGroup/RadioGroupPrimitive';
3+
4+
import clsx from 'clsx';
5+
import { customClassSwitcher } from '~/core';
6+
7+
import { RadioCardsContext } from '../context/RadioCardsContext';
8+
9+
const COMPONENT_NAME = 'RadioCards';
10+
11+
type RadioCardsRootProps = {
12+
children: React.ReactNode;
13+
className?: string;
14+
defaultChecked?: string | null;
15+
onChange?: (e: React.ChangeEvent<HTMLInputElement>) => void;
16+
customRootClass?: string;
17+
};
18+
19+
const RadioCardsRoot = ({ children, className = '', defaultChecked = null, onChange = null, customRootClass = '' }: RadioCardsRootProps) => {
20+
const rootClass = customClassSwitcher(customRootClass, COMPONENT_NAME);
21+
22+
return <RadioCardsContext.Provider value={{ defaultChecked, rootClass, onChange }}>
23+
<RadioGroupPrimitive.Root className={clsx(rootClass, className)} customRootClass={customRootClass}>{children}</RadioGroupPrimitive.Root>
24+
</RadioCardsContext.Provider>;
25+
};
26+
27+
export default RadioCardsRoot;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import RadioGroup from './RadioGroup';
2+
import SandboxEditor from '~/components/tools/SandboxEditor/SandboxEditor';
3+
import { useState } from 'react';
4+
5+
const RadioButton = (args) => {
6+
const options = [
7+
{ id: 'html', value: 'html', label: 'HTML' },
8+
{ id: 'css', value: 'css', label: 'CSS' },
9+
{ id: 'javascript', value: 'javascript', label: 'JavaScript' }];
10+
11+
const [language, setLanguage] = useState('css');
12+
13+
const handleChange = (e) => {
14+
setLanguage(e.target.value);
15+
};
16+
return (
17+
<SandboxEditor>
18+
<RadioGroup.Root defaultChecked={language} items={options} onChange={handleChange} >
19+
{options.map((option) => (
20+
<RadioGroup.Item key={option.id} value={option.value}>
21+
{option.label}
22+
</RadioGroup.Item>
23+
))}
24+
</RadioGroup.Root>
25+
</SandboxEditor>
26+
);
27+
};
28+
29+
export default {
30+
title: 'Components/RadioGroup',
31+
component: RadioGroup,
32+
render: (args) => <RadioButton {...args}/>
33+
};
34+
35+
export const All = {};
36+
All.args = {};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from 'react';
2+
3+
import RadioGroupRoot from './fragments/RadioGroupRoot';
4+
import RadioGroupItem from './fragments/RadioGroupItem';
5+
6+
const RadioGroup = {} as const;
7+
8+
RadioGroup.Root = RadioGroupRoot;
9+
RadioGroup.Item = RadioGroupItem;
10+
11+
export default RadioGroup;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { createContext } from 'react';
2+
3+
export const RadioGroupContext = createContext({
4+
defaultChecked: null,
5+
customRootClass: null,
6+
onChange: null
7+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React, { useContext } from 'react';
2+
import RadioGroupPrimitive from '~/core/primitives/RadioGroup/RadioGroupPrimitive';
3+
import { RadioGroupContext } from '../context/RadioGroupContext';
4+
import clsx from 'clsx';
5+
6+
const RadioGroupItem = ({ children, className = '', ...props }: { children: React.ReactNode } & RadioGroupPrimitive.ItemProps) => {
7+
const { defaultChecked, onChange, rootClass } = useContext(RadioGroupContext);
8+
return <RadioGroupPrimitive.Item className={clsx(`${rootClass}-item`, className)} {...props}>{children}</RadioGroupPrimitive.Item>;
9+
};
10+
11+
export default RadioGroupItem;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import React from 'react';
2+
import RadioGroupPrimitive from '~/core/primitives/RadioGroup/RadioGroupPrimitive';
3+
4+
import clsx from 'clsx';
5+
import { customClassSwitcher } from '~/core';
6+
7+
import { RadioGroupContext } from '../context/RadioGroupContext';
8+
9+
const COMPONENT_NAME = 'RadioGroup';
10+
11+
type RadioGroupRootProps = {
12+
children: React.ReactNode;
13+
className?: string;
14+
defaultChecked?: string | null;
15+
onChange?: (e: React.ChangeEvent<HTMLInputElement>) => void;
16+
customRootClass?: string;
17+
};
18+
19+
const RadioGroupRoot = ({ children, className = '', defaultChecked = null, onChange = null, customRootClass = '' }: RadioGroupRootProps) => {
20+
const rootClass = customClassSwitcher(customRootClass, COMPONENT_NAME);
21+
22+
return <RadioGroupContext.Provider value={{ defaultChecked, rootClass, onChange }}>
23+
<RadioGroupPrimitive.Root className={clsx(rootClass, className)} customRootClass={customRootClass}>{children}</RadioGroupPrimitive.Root>
24+
</RadioGroupContext.Provider>;
25+
};
26+
27+
export default RadioGroupRoot;

0 commit comments

Comments
 (0)