未验证 提交 006abe5e 编写于 作者: K Kermit Xuan 提交者: GitHub

chore(slider): optimize type definition (#26884)

上级 aac1d4e4
import * as React from 'react'; import * as React from 'react';
import RcSlider from 'rc-slider/lib/Slider'; import RcSlider, { Range as RcRange, Handle as RcHandle } from 'rc-slider';
import RcRange from 'rc-slider/lib/Range';
import RcHandle from 'rc-slider/lib/Handle';
import classNames from 'classnames'; import classNames from 'classnames';
import { TooltipPlacement } from '../tooltip'; import { TooltipPlacement } from '../tooltip';
import SliderTooltip from './SliderTooltip'; import SliderTooltip from './SliderTooltip';
...@@ -18,16 +16,15 @@ export interface SliderMarks { ...@@ -18,16 +16,15 @@ export interface SliderMarks {
interface HandleGeneratorInfo { interface HandleGeneratorInfo {
value?: number; value?: number;
dragging: boolean; dragging?: boolean;
index: number; index: number;
rest: any[];
} }
export type HandleGeneratorFn = (config: { export type HandleGeneratorFn = (config: {
tooltipPrefixCls?: string; tooltipPrefixCls?: string;
prefixCls?: string; prefixCls?: string;
info: HandleGeneratorInfo; info: HandleGeneratorInfo;
}) => React.ReactNode; }) => React.ReactElement;
export interface SliderBaseProps { export interface SliderBaseProps {
prefixCls?: string; prefixCls?: string;
...@@ -35,7 +32,7 @@ export interface SliderBaseProps { ...@@ -35,7 +32,7 @@ export interface SliderBaseProps {
reverse?: boolean; reverse?: boolean;
min?: number; min?: number;
max?: number; max?: number;
step?: number | null; step?: number;
marks?: SliderMarks; marks?: SliderMarks;
dots?: boolean; dots?: boolean;
included?: boolean; included?: boolean;
...@@ -70,81 +67,98 @@ export interface SliderRangeProps extends SliderBaseProps { ...@@ -70,81 +67,98 @@ export interface SliderRangeProps extends SliderBaseProps {
export type Visibles = { [index: number]: boolean }; export type Visibles = { [index: number]: boolean };
const Slider = React.forwardRef<unknown, SliderSingleProps | SliderRangeProps>((props, ref) => { const Slider = React.forwardRef<unknown, SliderSingleProps | SliderRangeProps>(
const { getPrefixCls, direction, getPopupContainer } = React.useContext(ConfigContext); (props, ref: any) => {
const [visibles, setVisibles] = React.useState<Visibles>({}); const { getPrefixCls, direction, getPopupContainer } = React.useContext(ConfigContext);
const [visibles, setVisibles] = React.useState<Visibles>({});
const toggleTooltipVisible = (index: number, visible: boolean) => { const toggleTooltipVisible = (index: number, visible: boolean) => {
setVisibles((prev: Visibles) => { setVisibles((prev: Visibles) => {
return { ...prev, [index]: visible }; return { ...prev, [index]: visible };
}); });
}; };
const getTooltipPlacement = (tooltipPlacement?: TooltipPlacement, vertical?: boolean) => { const getTooltipPlacement = (tooltipPlacement?: TooltipPlacement, vertical?: boolean) => {
if (tooltipPlacement) { if (tooltipPlacement) {
return tooltipPlacement; return tooltipPlacement;
} }
if (!vertical) { if (!vertical) {
return 'top'; return 'top';
} }
return direction === 'rtl' ? 'left' : 'right'; return direction === 'rtl' ? 'left' : 'right';
}; };
const handleWithTooltip: HandleGeneratorFn = ({
tooltipPrefixCls,
prefixCls,
info: { value, dragging, index, ...restProps },
}) => {
const {
tipFormatter,
tooltipVisible,
tooltipPlacement,
getTooltipPopupContainer,
vertical,
} = props;
const isTipFormatter = tipFormatter ? visibles[index] || dragging : false;
const visible = tooltipVisible || (tooltipVisible === undefined && isTipFormatter);
return (
<SliderTooltip
prefixCls={tooltipPrefixCls}
title={tipFormatter ? tipFormatter(value) : ''}
visible={visible}
placement={getTooltipPlacement(tooltipPlacement, vertical)}
transitionName="zoom-down"
key={index}
overlayClassName={`${prefixCls}-tooltip`}
getPopupContainer={getTooltipPopupContainer || getPopupContainer || (() => document.body)}
>
<RcHandle
{...restProps}
value={value}
onMouseEnter={() => toggleTooltipVisible(index, true)}
onMouseLeave={() => toggleTooltipVisible(index, false)}
/>
</SliderTooltip>
);
};
const handleWithTooltip: HandleGeneratorFn = ({
tooltipPrefixCls,
prefixCls,
info: { value, dragging, index, ...restProps },
}) => {
const { const {
tipFormatter, prefixCls: customizePrefixCls,
tooltipVisible, tooltipPrefixCls: customizeTooltipPrefixCls,
tooltipPlacement, range,
getTooltipPopupContainer, className,
vertical, ...restProps
} = props; } = props;
const isTipFormatter = tipFormatter ? visibles[index] || dragging : false; const prefixCls = getPrefixCls('slider', customizePrefixCls);
const visible = tooltipVisible || (tooltipVisible === undefined && isTipFormatter); const tooltipPrefixCls = getPrefixCls('tooltip', customizeTooltipPrefixCls);
return ( const cls = classNames(className, {
<SliderTooltip [`${prefixCls}-rtl`]: direction === 'rtl',
prefixCls={tooltipPrefixCls} });
title={tipFormatter ? tipFormatter(value) : ''} // make reverse default on rtl direction
visible={visible} if (direction === 'rtl' && !restProps.vertical) {
placement={getTooltipPlacement(tooltipPlacement, vertical)} restProps.reverse = !restProps.reverse;
transitionName="zoom-down" }
key={index} if (range) {
overlayClassName={`${prefixCls}-tooltip`} return (
getPopupContainer={getTooltipPopupContainer || getPopupContainer || (() => document.body)} <RcRange
> {...(restProps as SliderRangeProps)}
<RcHandle className={cls}
{...restProps} ref={ref}
value={value} handle={(info: HandleGeneratorInfo) =>
onMouseEnter={() => toggleTooltipVisible(index, true)} handleWithTooltip({
onMouseLeave={() => toggleTooltipVisible(index, false)} tooltipPrefixCls,
prefixCls,
info,
})
}
prefixCls={prefixCls}
/> />
</SliderTooltip> );
); }
};
const {
prefixCls: customizePrefixCls,
tooltipPrefixCls: customizeTooltipPrefixCls,
range,
className,
...restProps
} = props;
const prefixCls = getPrefixCls('slider', customizePrefixCls);
const tooltipPrefixCls = getPrefixCls('tooltip', customizeTooltipPrefixCls);
const cls = classNames(className, {
[`${prefixCls}-rtl`]: direction === 'rtl',
});
// make reverse default on rtl direction
if (direction === 'rtl' && !restProps.vertical) {
restProps.reverse = !restProps.reverse;
}
if (range) {
return ( return (
<RcRange <RcSlider
{...restProps} {...(restProps as SliderSingleProps)}
className={cls} className={cls}
ref={ref} ref={ref}
handle={(info: HandleGeneratorInfo) => handle={(info: HandleGeneratorInfo) =>
...@@ -155,27 +169,10 @@ const Slider = React.forwardRef<unknown, SliderSingleProps | SliderRangeProps>(( ...@@ -155,27 +169,10 @@ const Slider = React.forwardRef<unknown, SliderSingleProps | SliderRangeProps>((
}) })
} }
prefixCls={prefixCls} prefixCls={prefixCls}
tooltipPrefixCls={tooltipPrefixCls}
/> />
); );
} },
return ( );
<RcSlider
{...restProps}
className={cls}
ref={ref}
handle={(info: HandleGeneratorInfo) =>
handleWithTooltip({
tooltipPrefixCls,
prefixCls,
info,
})
}
prefixCls={prefixCls}
tooltipPrefixCls={tooltipPrefixCls}
/>
);
});
Slider.displayName = 'Slider'; Slider.displayName = 'Slider';
......
...@@ -46,14 +46,6 @@ declare module 'rc-rate'; ...@@ -46,14 +46,6 @@ declare module 'rc-rate';
declare module 'rc-queue-anim'; declare module 'rc-queue-anim';
declare module 'rc-slider';
declare module 'rc-slider/lib/Slider';
declare module 'rc-slider/lib/Range';
declare module 'rc-slider/lib/Handle';
declare module 'rc-steps'; declare module 'rc-steps';
declare module 'rc-switch'; declare module 'rc-switch';
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册