Skip to content
Closed
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
33 changes: 25 additions & 8 deletions docs/demo/basic.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import '../../assets/style.less';
import React from 'react';
import Segmented from 'rc-segmented';
import React from 'react';
import '../../assets/style.less';

enum Platform {
iOS = 'iOS',
Android = 'Android',
Web = 'Web',
}

export default function App() {
const [platform, setPlatform] = React.useState<Platform>(Platform.iOS);

return (
<div>
<div className="wrapper">
<Segmented
options={['iOS', 'Android', 'Web']}
onChange={(value) => console.log(value, typeof value)}
value={platform}
options={Object.values(Platform)}
onChange={setPlatform}
/>
</div>
<div className="wrapper">
Expand All @@ -21,11 +30,19 @@ export default function App() {
<Segmented options={['iOS', 'Android', 'Web']} disabled />
</div>
<div className="wrapper">
<Segmented
<Segmented<Platform>
options={[
Platform.iOS,
{ label: 'Android', value: Platform.Android, disabled: true },
Platform.Web,
]}
/>
<hr />
<Segmented<Platform, string>
options={[
'iOS',
{ label: 'Android', value: 'Android', disabled: true },
'Web',
Platform.iOS,
{ label: 'Android', value: 'Linux', disabled: true },
Platform.Web,
]}
/>
</div>
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@
"react": ">=16.0.0",
"react-dom": ">=16.0.0"
},
"resolutions": {
"types-ramda": "0.29.4"
},
Comment on lines +87 to +89
Copy link
Member

Choose a reason for hiding this comment

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

这个是什么原因

Copy link
Author

Choose a reason for hiding this comment

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

貌似跑 tsc 会挂,但是今天看 181 pr 又是好的,估计是 tsconfig include有点问题吧

"cnpm": {
"mode": "npm"
},
Expand Down
277 changes: 151 additions & 126 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,44 @@
import * as React from 'react';
import classNames from 'classnames';
import useMergedState from 'rc-util/lib/hooks/useMergedState';
import { composeRef } from 'rc-util/lib/ref';
import omit from 'rc-util/lib/omit';
import { composeRef } from 'rc-util/lib/ref';
import * as React from 'react';

import MotionThumb from './MotionThumb';

export type SegmentedValue = string | number;

export type SegmentedRawOption = SegmentedValue;

export interface SegmentedLabeledOption {
export interface SegmentedLabeledOption<
Value extends SegmentedRawOption = SegmentedRawOption,
> {
className?: string;
disabled?: boolean;
label: React.ReactNode;
value: SegmentedRawOption;
value: Value;
/**
* html `title` property for label
*/
title?: string;
}

type SegmentedOptions = (SegmentedRawOption | SegmentedLabeledOption)[];

export interface SegmentedProps
extends Omit<React.HTMLProps<HTMLDivElement>, 'onChange'> {
options: SegmentedOptions;
defaultValue?: SegmentedValue;
value?: SegmentedValue;
onChange?: (value: SegmentedValue) => void;
type SegmentedOptions<
PlainValue extends SegmentedRawOption = SegmentedRawOption,
Value extends SegmentedRawOption = PlainValue,
> = (PlainValue | SegmentedLabeledOption<Value>)[];

/**
* Generic parameter `Value` is supported since 2.3.0
*/
export interface SegmentedProps<
PlainValue extends SegmentedRawOption = SegmentedRawOption,
Value extends SegmentedRawOption = PlainValue,
> extends Omit<React.HTMLProps<HTMLDivElement>, 'onChange'> {
options: SegmentedOptions<Value>;
defaultValue?: Value;
value?: Value;
onChange?: (value: Value) => void;
disabled?: boolean;
prefixCls?: string;
direction?: 'ltr' | 'rtl';
Expand All @@ -46,8 +56,10 @@ function getValidTitle(option: SegmentedLabeledOption) {
}
}

function normalizeOptions(options: SegmentedOptions): SegmentedLabeledOption[] {
return options.map((option) => {
function normalizeOptions<
Value extends SegmentedRawOption = SegmentedRawOption,
>(options: SegmentedOptions<Value>) {
return options.map<SegmentedLabeledOption<Value>>((option) => {
if (typeof option === 'object' && option !== null) {
const validTitle = getValidTitle(option);

Expand All @@ -65,7 +77,7 @@ function normalizeOptions(options: SegmentedOptions): SegmentedLabeledOption[] {
});
}

const InternalSegmentedOption: React.FC<{
interface InternalSegmentedOptionProps {
prefixCls: string;
className?: string;
disabled?: boolean;
Expand All @@ -77,16 +89,20 @@ const InternalSegmentedOption: React.FC<{
e: React.ChangeEvent<HTMLInputElement>,
value: SegmentedRawOption,
) => void;
}> = ({
prefixCls,
className,
disabled,
checked,
label,
title,
value,
onChange,
}) => {
}

const InternalSegmentedOption = (props: InternalSegmentedOptionProps) => {
const {
prefixCls,
className,
disabled,
checked,
label,
title,
value,
onChange,
} = props;

const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
if (disabled) {
return;
Expand Down Expand Up @@ -115,110 +131,119 @@ const InternalSegmentedOption: React.FC<{
);
};

const Segmented = React.forwardRef<HTMLDivElement, SegmentedProps>(
(props, ref) => {
const {
prefixCls = 'rc-segmented',
direction,
options = [],
disabled,
defaultValue,
value,
onChange,
className = '',
motionName = 'thumb-motion',
...restProps
} = props;

const containerRef = React.useRef<HTMLDivElement>(null);
const mergedRef = React.useMemo(
() => composeRef<HTMLDivElement>(containerRef, ref),
[containerRef, ref],
);

const segmentedOptions = React.useMemo(() => {
return normalizeOptions(options);
}, [options]);

// Note: We should not auto switch value when value not exist in options
// which may break single source of truth.
const [rawValue, setRawValue] = useMergedState(segmentedOptions[0]?.value, {
value,
defaultValue,
});

// ======================= Change ========================
const [thumbShow, setThumbShow] = React.useState(false);

const handleChange = (
event: React.ChangeEvent<HTMLInputElement>,
val: SegmentedRawOption,
) => {
if (disabled) {
return;
}

setRawValue(val);

onChange?.(val);
};
const Segmented = (
props: SegmentedProps,
ref: React.ForwardedRef<HTMLDivElement>,
) => {
const {
prefixCls = 'rc-segmented',
direction,
options = [],
disabled,
defaultValue,
value,
onChange,
className = '',
motionName = 'thumb-motion',
...restProps
} = props;

const containerRef = React.useRef<HTMLDivElement>(null);
const mergedRef = React.useMemo(
() => composeRef<HTMLDivElement>(containerRef, ref),
[containerRef, ref],
);

const segmentedOptions = React.useMemo(() => {
return normalizeOptions(options);
}, [options]);

// Note: We should not auto switch value when value not exist in options
// which may break single source of truth.
const [rawValue, setRawValue] = useMergedState(segmentedOptions[0]?.value, {
value,
defaultValue,
});

// ======================= Change ========================
const [thumbShow, setThumbShow] = React.useState(false);

const divProps = omit(restProps, ['children']);

return (
<div
{...divProps}
className={classNames(
prefixCls,
{
[`${prefixCls}-rtl`]: direction === 'rtl',
[`${prefixCls}-disabled`]: disabled,
},
className,
)}
ref={mergedRef}
>
<div className={`${prefixCls}-group`}>
<MotionThumb
const handleChange = (
event: React.ChangeEvent<HTMLInputElement>,
val: SegmentedRawOption,
) => {
if (disabled) {
return;
}

setRawValue(val);

onChange?.(val);
};

const divProps = omit(restProps, ['children']);

return (
<div
{...divProps}
className={classNames(
prefixCls,
{
[`${prefixCls}-rtl`]: direction === 'rtl',
[`${prefixCls}-disabled`]: disabled,
},
className,
)}
ref={mergedRef}
>
<div className={`${prefixCls}-group`}>
<MotionThumb
prefixCls={prefixCls}
value={rawValue}
containerRef={containerRef}
motionName={`${prefixCls}-${motionName}`}
direction={direction}
getValueIndex={(val) =>
segmentedOptions.findIndex((n) => n.value === val)
}
onMotionStart={() => {
setThumbShow(true);
}}
onMotionEnd={() => {
setThumbShow(false);
}}
/>
{segmentedOptions.map((segmentedOption) => (
<InternalSegmentedOption
{...segmentedOption}
key={segmentedOption.value}
prefixCls={prefixCls}
value={rawValue}
containerRef={containerRef}
motionName={`${prefixCls}-${motionName}`}
direction={direction}
getValueIndex={(val) =>
segmentedOptions.findIndex((n) => n.value === val)
}
onMotionStart={() => {
setThumbShow(true);
}}
onMotionEnd={() => {
setThumbShow(false);
}}
className={classNames(
segmentedOption.className,
`${prefixCls}-item`,
{
[`${prefixCls}-item-selected`]:
segmentedOption.value === rawValue && !thumbShow,
},
)}
checked={segmentedOption.value === rawValue}
onChange={handleChange}
disabled={!!disabled || !!segmentedOption.disabled}
/>
{segmentedOptions.map((segmentedOption) => (
<InternalSegmentedOption
{...segmentedOption}
key={segmentedOption.value}
prefixCls={prefixCls}
className={classNames(
segmentedOption.className,
`${prefixCls}-item`,
{
[`${prefixCls}-item-selected`]:
segmentedOption.value === rawValue && !thumbShow,
},
)}
checked={segmentedOption.value === rawValue}
onChange={handleChange}
disabled={!!disabled || !!segmentedOption.disabled}
/>
))}
</div>
))}
</div>
);
},
);
</div>
);
};

Segmented.displayName = 'Segmented';
const RefSegmented = React.forwardRef(Segmented);
RefSegmented.displayName = 'Segmented';

export default Segmented;
export default RefSegmented as <
PlainValue extends SegmentedRawOption = SegmentedRawOption,
Value extends SegmentedRawOption = PlainValue,
>(
props: SegmentedProps<PlainValue, Value> & {
ref?: React.Ref<HTMLDivElement>;
},
) => React.ReactElement;
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
"rc-segmented": ["src/index.tsx"]
}
},
"include": [".dumi/**/*", ".dumirc.ts", "src", "tests", "docs/examples"],
"include": [".dumi/**/*", ".dumirc.ts", "src", "tests", "docs/demo"]
}