Skip to content

Commit

Permalink
feat: support name prop to enable grouping (#284)
Browse files Browse the repository at this point in the history
* feat: support  prop to enable grouping

* add test
  • Loading branch information
thinkasany authored Nov 21, 2024
1 parent 6bedd52 commit daedf88
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
17 changes: 17 additions & 0 deletions docs/demo/name.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Segmented from 'rc-segmented';
import React from 'react';
import '../../assets/style.less';

export default function App() {
return (
<div>
<div className="wrapper">
<Segmented
name="group"
options={['iOS', 'Android', 'Web']}
onChange={(value) => console.log(value, typeof value)}
/>
</div>
</div>
);
}
4 changes: 4 additions & 0 deletions docs/example.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ nav:
## rtl

<code src="./demo/rtl.tsx"></code>

## name

<code src="./demo/name.tsx"></code>
6 changes: 6 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface SegmentedProps<ValueType = SegmentedValue>
direction?: 'ltr' | 'rtl';
motionName?: string;
vertical?: boolean;
name?: string;
}

function getValidTitle(option: SegmentedLabeledOption) {
Expand Down Expand Up @@ -78,6 +79,7 @@ const InternalSegmentedOption: React.FC<{
label: React.ReactNode;
title?: string;
value: SegmentedRawOption;
name?: string;
onChange: (
e: React.ChangeEvent<HTMLInputElement>,
value: SegmentedRawOption,
Expand All @@ -90,6 +92,7 @@ const InternalSegmentedOption: React.FC<{
label,
title,
value,
name,
onChange,
}) => {
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
Expand All @@ -106,6 +109,7 @@ const InternalSegmentedOption: React.FC<{
})}
>
<input
name={name}
className={`${prefixCls}-item-input`}
aria-hidden="true"
type="radio"
Expand Down Expand Up @@ -135,6 +139,7 @@ const Segmented = React.forwardRef<HTMLDivElement, SegmentedProps>(
disabled,
defaultValue,
value,
name,
onChange,
className = '',
motionName = 'thumb-motion',
Expand Down Expand Up @@ -208,6 +213,7 @@ const Segmented = React.forwardRef<HTMLDivElement, SegmentedProps>(
{segmentedOptions.map((segmentedOption) => (
<InternalSegmentedOption
{...segmentedOption}
name={name}
key={segmentedOption.value}
prefixCls={prefixCls}
className={classNames(
Expand Down
13 changes: 13 additions & 0 deletions tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -650,4 +650,17 @@ describe('rc-segmented', () => {

offsetParentSpy.mockRestore();
});

it('all children should have a name property', () => {
const GROUP_NAME = 'GROUP_NAME';
const { container } = render(
<Segmented options={['iOS', 'Android', 'Web']} name={GROUP_NAME} />,
);

container
.querySelectorAll<HTMLInputElement>('input[type="radio"]')
.forEach((el) => {
expect(el.name).toEqual(GROUP_NAME);
});
});
});

0 comments on commit daedf88

Please sign in to comment.