Skip to content

Commit e6cfe8c

Browse files
committed
fix: update segment component apis
1 parent 2c602d0 commit e6cfe8c

File tree

3 files changed

+37
-82
lines changed

3 files changed

+37
-82
lines changed
Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,38 @@
1-
import { compoundComponents } from '../../utils';
2-
import { SegmentFrame } from './SegmentFrame';
3-
import { SegmentItem } from './SegmentItem';
1+
import { type ReactElement, useEffect, useState } from 'react';
2+
import { createVariants } from '../../utils';
3+
import styles from './Segment.module.css';
44

5-
export const Segment = compoundComponents(SegmentFrame, {
6-
Item: SegmentItem,
7-
});
5+
type Props<Value> = {
6+
className?: string;
7+
options: {
8+
value: Value;
9+
label?: ReactElement;
10+
}[];
11+
value: Value;
12+
onChange: (value: Value) => void;
13+
};
14+
15+
const variants = createVariants(styles);
16+
17+
export const Segment = <Value,>({ className, options, onChange, ...props }: Props<Value>) => {
18+
const [value, setValue] = useState(options[0]?.value);
19+
20+
useEffect(() => {
21+
onChange?.(value);
22+
}, [value, onChange]);
23+
24+
return (
25+
<div className={variants('frame', { className })} {...props}>
26+
{options.map((option) => (
27+
<button
28+
type="button"
29+
key={String(option.value)}
30+
className={variants('item', { active: option.value === value })}
31+
onClick={() => setValue(option.value)}
32+
>
33+
{option.label}
34+
</button>
35+
))}
36+
</div>
37+
);
38+
};

packages/react/src/preset/Segment/SegmentFrame.tsx

Lines changed: 0 additions & 50 deletions
This file was deleted.

packages/react/src/preset/Segment/SegmentItem.tsx

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)