Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 20 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as React from 'react';

import MotionThumb from './MotionThumb';

export type SemanticName = 'item' | 'label';
export type SegmentedValue = string | number;

export type SegmentedRawOption = SegmentedValue;
Expand Down Expand Up @@ -41,6 +42,8 @@ export interface SegmentedProps<ValueType = SegmentedValue>
motionName?: string;
vertical?: boolean;
name?: string;
classNames?: Partial<Record<SemanticName, string>>;
styles?: Partial<Record<SemanticName, React.CSSProperties>>;
}

function getValidTitle(option: SegmentedLabeledOption) {
Expand Down Expand Up @@ -74,6 +77,8 @@ function normalizeOptions(options: SegmentedOptions): SegmentedLabeledOption[] {
const InternalSegmentedOption: React.FC<{
prefixCls: string;
className?: string;
classNames?: Partial<Record<SemanticName, string>>;
styles?: Partial<Record<SemanticName, React.CSSProperties>>;
disabled?: boolean;
checked: boolean;
label: React.ReactNode;
Expand All @@ -92,6 +97,8 @@ const InternalSegmentedOption: React.FC<{
}> = ({
prefixCls,
className,
styles,
classNames: segmentedClassNames,
disabled,
checked,
label,
Expand All @@ -117,6 +124,7 @@ const InternalSegmentedOption: React.FC<{
className={classNames(className, {
[`${prefixCls}-item-disabled`]: disabled,
})}
style={styles?.item}
Copy link
Member

Choose a reason for hiding this comment

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

感觉有点怪,这里写了 styles.item,但是 classNames.item 却在下面的文件里。这个看起来是应该放一起的,否则不够内聚。

onMouseDown={onMouseDown}
>
<input
Expand All @@ -132,9 +140,13 @@ const InternalSegmentedOption: React.FC<{
onKeyUp={onKeyUp}
/>
<div
className={`${prefixCls}-item-label`}
className={classNames(
`${prefixCls}-item-label`,
segmentedClassNames?.label,
)}
title={title}
aria-selected={checked}
style={styles?.label}
>
{label}
</div>
Expand All @@ -155,6 +167,9 @@ const Segmented = React.forwardRef<HTMLDivElement, SegmentedProps>(
name,
onChange,
className = '',
style,
styles,
classNames: segmentedClassNames,
motionName = 'thumb-motion',
...restProps
} = props;
Expand Down Expand Up @@ -240,12 +255,12 @@ const Segmented = React.forwardRef<HTMLDivElement, SegmentedProps>(
break;
}
};

return (
<div
role="radiogroup"
aria-label="segmented control"
tabIndex={disabled ? undefined : 0}
style={style}
{...divProps}
className={classNames(
prefixCls,
Expand Down Expand Up @@ -285,6 +300,7 @@ const Segmented = React.forwardRef<HTMLDivElement, SegmentedProps>(
className={classNames(
segmentedOption.className,
`${prefixCls}-item`,
segmentedClassNames?.item,
{
[`${prefixCls}-item-selected`]:
segmentedOption.value === rawValue && !thumbShow,
Expand All @@ -294,6 +310,8 @@ const Segmented = React.forwardRef<HTMLDivElement, SegmentedProps>(
segmentedOption.value === rawValue,
},
)}
classNames={segmentedClassNames}
styles={styles}
checked={segmentedOption.value === rawValue}
onChange={handleChange}
onFocus={handleFocus}
Expand Down
34 changes: 34 additions & 0 deletions tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -765,4 +765,38 @@ describe('Segmented keyboard navigation', () => {
'rc-segmented-item-input-focused',
);
});
it('should apply custom styles to Segmented', () => {
const customClassNames = {
item: 'custom-item',
label: 'custom-label',
};

const customStyles = {
item: { color: 'yellow' },
label: { backgroundColor: 'black' },
};

const { container } = render(
<Segmented
options={['iOS', 'Android', 'Web']}
classNames={customClassNames}
styles={customStyles}
/>,
);

const itemElement = container.querySelector(
'.rc-segmented-item',
) as HTMLElement;
const labelElement = container.querySelector(
'.rc-segmented-item-label',
) as HTMLElement;

// check classNames
expect(itemElement.classList).toContain('custom-item');
expect(labelElement.classList).toContain('custom-label');

// check styles
expect(itemElement.style.color).toBe('yellow');
expect(labelElement.style.backgroundColor).toBe('black');
});
});
Loading