mirror of
https://github.com/ant-design/ant-design.git
synced 2025-06-07 09:26:06 +08:00
chore(slider): optimize type definition (#26884)
This commit is contained in:
parent
aac1d4e434
commit
006abe5e92
@ -1,7 +1,5 @@
|
|||||||
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 {
|
|||||||
|
|
||||||
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 {
|
|||||||
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 {
|
|||||||
|
|
||||||
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>((
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
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';
|
||||||
|
|
||||||
|
@ -137,7 +137,7 @@
|
|||||||
"rc-rate": "~2.8.2",
|
"rc-rate": "~2.8.2",
|
||||||
"rc-resize-observer": "^0.2.3",
|
"rc-resize-observer": "^0.2.3",
|
||||||
"rc-select": "~11.3.2",
|
"rc-select": "~11.3.2",
|
||||||
"rc-slider": "~9.4.1",
|
"rc-slider": "~9.5.1",
|
||||||
"rc-steps": "~4.1.0",
|
"rc-steps": "~4.1.0",
|
||||||
"rc-switch": "~3.2.0",
|
"rc-switch": "~3.2.0",
|
||||||
"rc-table": "~7.9.2",
|
"rc-table": "~7.9.2",
|
||||||
|
8
typings/custom-typings.d.ts
vendored
8
typings/custom-typings.d.ts
vendored
@ -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';
|
||||||
|
Loading…
Reference in New Issue
Block a user