Skip to content

feat: support name prop to enable grouping #284

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

Merged
merged 2 commits into from
Nov 21, 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
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)}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

优化 onChange 处理函数

建议移除 console.log,改用更实用的状态管理方式来展示选中值的变化。

-          onChange={(value) => console.log(value, typeof value)}
+          onChange={(value) => {
+            // 在实际应用中更新状态
+            setSelectedPlatform(value);
+          }}

Committable suggestion skipped: line range outside the PR's diff.

/>
</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);
});
});
});
Loading