Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: Add attribute itemRender to 'segmented' #301

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
zombieJ merged 9 commits into react-component:master from EmilyyyLiu:segmented-itemRender
Jul 2, 2025
Merged
Show file tree
Hide file tree
Changes from 8 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
96 changes: 62 additions & 34 deletions src/index.tsx
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ export interface SegmentedLabeledOption<ValueType = SegmentedRawOption> {
title?: string;
}

type ItemRender = (
node: React.ReactNode,
info: { item: SegmentedLabeledOption },
) => React.ReactNode;

type SegmentedOptions<T = SegmentedRawOption> = (
| T
| SegmentedLabeledOption<T>
Expand All @@ -44,6 +49,7 @@ export interface SegmentedProps<ValueType = SegmentedValue>
name?: string;
classNames?: Partial<Record<SemanticName, string>>;
styles?: Partial<Record<SemanticName, React.CSSProperties>>;
itemRender?: ItemRender;
}

function getValidTitle(option: SegmentedLabeledOption) {
Expand Down Expand Up @@ -80,6 +86,7 @@ const InternalSegmentedOption: React.FC<{
style?: React.CSSProperties;
classNames?: Partial<Record<SemanticName, string>>;
styles?: Partial<Record<SemanticName, React.CSSProperties>>;
data: SegmentedLabeledOption;
disabled?: boolean;
checked: boolean;
label: React.ReactNode;
Expand All @@ -95,12 +102,14 @@ const InternalSegmentedOption: React.FC<{
onKeyDown: (e: React.KeyboardEvent) => void;
onKeyUp: (e: React.KeyboardEvent) => void;
onMouseDown: () => void;
itemRender?: ItemRender;
}> = ({
prefixCls,
className,
style,
styles,
classNames: segmentedClassNames,
data,
disabled,
checked,
label,
Expand All @@ -113,15 +122,15 @@ const InternalSegmentedOption: React.FC<{
onKeyDown,
onKeyUp,
onMouseDown,
itemRender = (node: React.ReactNode) => node,
}) => {
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
if (disabled) {
return;
}
onChange(event, value);
};

return (
const itemContent: React.ReactNode = (
<label
className={classNames(className, {
[`${prefixCls}-item-disabled`]: disabled,
Expand Down Expand Up @@ -155,6 +164,7 @@ const InternalSegmentedOption: React.FC<{
</div>
</label>
);
return itemRender(itemContent, { item: data });
};

const Segmented = React.forwardRef<HTMLDivElement, SegmentedProps>(
Expand All @@ -174,6 +184,7 @@ const Segmented = React.forwardRef<HTMLDivElement, SegmentedProps>(
styles,
classNames: segmentedClassNames,
motionName = 'thumb-motion',
itemRender,
...restProps
} = props;

Expand Down Expand Up @@ -258,6 +269,54 @@ const Segmented = React.forwardRef<HTMLDivElement, SegmentedProps>(
break;
}
};

const renderOption = (segmentedOption: SegmentedLabeledOption) => {
const {
label,
value: optionValue,
disabled: optionDisabled,
title,
} = segmentedOption;
const optionData: SegmentedLabeledOption = {
label,
value: optionValue,
disabled: optionDisabled,
title,
};
return (
<InternalSegmentedOption
{...segmentedOption}
name={name}
data={optionData}
itemRender={itemRender}
key={optionValue}
prefixCls={prefixCls}
className={classNames(
segmentedOption.className,
`${prefixCls}-item`,
segmentedClassNames?.item,
{
[`${prefixCls}-item-selected`]:
optionValue === rawValue && !thumbShow,
[`${prefixCls}-item-focused`]:
isFocused && isKeyboard && optionValue === rawValue,
},
)}
style={styles?.item}
classNames={segmentedClassNames}
styles={styles}
checked={optionValue === rawValue}
onChange={handleChange}
onFocus={handleFocus}
onBlur={handleBlur}
onKeyDown={handleKeyDown}
onKeyUp={handleKeyUp}
onMouseDown={handleMouseDown}
disabled={!!disabled || !!optionDisabled}
/>
);
};

return (
<div
role="radiogroup"
Expand Down Expand Up @@ -294,38 +353,7 @@ const Segmented = React.forwardRef<HTMLDivElement, SegmentedProps>(
setThumbShow(false);
}}
/>
{segmentedOptions.map((segmentedOption) => (
<InternalSegmentedOption
{...segmentedOption}
name={name}
key={segmentedOption.value}
prefixCls={prefixCls}
className={classNames(
segmentedOption.className,
`${prefixCls}-item`,
segmentedClassNames?.item,
{
[`${prefixCls}-item-selected`]:
segmentedOption.value === rawValue && !thumbShow,
[`${prefixCls}-item-focused`]:
isFocused &&
isKeyboard &&
segmentedOption.value === rawValue,
},
)}
style={styles?.item}
classNames={segmentedClassNames}
styles={styles}
checked={segmentedOption.value === rawValue}
onChange={handleChange}
onFocus={handleFocus}
onBlur={handleBlur}
onKeyDown={handleKeyDown}
onKeyUp={handleKeyUp}
onMouseDown={handleMouseDown}
disabled={!!disabled || !!segmentedOption.disabled}
/>
))}
{segmentedOptions.map(renderOption)}
</div>
</div>
);
Expand Down
19 changes: 19 additions & 0 deletions tests/index.test.tsx
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -799,4 +799,23 @@ describe('Segmented keyboard navigation', () => {
expect(itemElement.style.color).toBe('yellow');
expect(labelElement.style.backgroundColor).toBe('black');
});
describe('itemRender', () => {
it('When "itemRender" is not configured, render the original "label"', () => {
const { container } = render(
<Segmented options={['iOS', 'Android', 'Web']} />,
);
const label = container.querySelector('.rc-segmented-item-label');
expect(label).toHaveTextContent('iOS');
});
it('Configure "itemRender" to render the return value', () => {
const { container } = render(
<Segmented
options={['iOS', 'Android', 'Web']}
itemRender={(node) => <div className="test-title">{node}</div>}
/>,
);
const labels = container.querySelectorAll('.test-title');
expect(labels).toHaveLength(3);
});
});
});

AltStyle によって変換されたページ (->オリジナル) /