From 006abe5e92007e6c01d5137bc8ba6750cb2b0302 Mon Sep 17 00:00:00 2001 From: Kermit Xuan Date: Thu, 24 Sep 2020 18:14:22 +0800 Subject: [PATCH] chore(slider): optimize type definition (#26884) --- components/slider/index.tsx | 185 ++++++++++++++++++------------------ package.json | 2 +- typings/custom-typings.d.ts | 8 -- 3 files changed, 92 insertions(+), 103 deletions(-) diff --git a/components/slider/index.tsx b/components/slider/index.tsx index 95121dd936..a8363f2824 100644 --- a/components/slider/index.tsx +++ b/components/slider/index.tsx @@ -1,7 +1,5 @@ import * as React from 'react'; -import RcSlider from 'rc-slider/lib/Slider'; -import RcRange from 'rc-slider/lib/Range'; -import RcHandle from 'rc-slider/lib/Handle'; +import RcSlider, { Range as RcRange, Handle as RcHandle } from 'rc-slider'; import classNames from 'classnames'; import { TooltipPlacement } from '../tooltip'; import SliderTooltip from './SliderTooltip'; @@ -18,16 +16,15 @@ export interface SliderMarks { interface HandleGeneratorInfo { value?: number; - dragging: boolean; + dragging?: boolean; index: number; - rest: any[]; } export type HandleGeneratorFn = (config: { tooltipPrefixCls?: string; prefixCls?: string; info: HandleGeneratorInfo; -}) => React.ReactNode; +}) => React.ReactElement; export interface SliderBaseProps { prefixCls?: string; @@ -35,7 +32,7 @@ export interface SliderBaseProps { reverse?: boolean; min?: number; max?: number; - step?: number | null; + step?: number; marks?: SliderMarks; dots?: boolean; included?: boolean; @@ -70,81 +67,98 @@ export interface SliderRangeProps extends SliderBaseProps { export type Visibles = { [index: number]: boolean }; -const Slider = React.forwardRef((props, ref) => { - const { getPrefixCls, direction, getPopupContainer } = React.useContext(ConfigContext); - const [visibles, setVisibles] = React.useState({}); +const Slider = React.forwardRef( + (props, ref: any) => { + const { getPrefixCls, direction, getPopupContainer } = React.useContext(ConfigContext); + const [visibles, setVisibles] = React.useState({}); - const toggleTooltipVisible = (index: number, visible: boolean) => { - setVisibles((prev: Visibles) => { - return { ...prev, [index]: visible }; - }); - }; + const toggleTooltipVisible = (index: number, visible: boolean) => { + setVisibles((prev: Visibles) => { + return { ...prev, [index]: visible }; + }); + }; - const getTooltipPlacement = (tooltipPlacement?: TooltipPlacement, vertical?: boolean) => { - if (tooltipPlacement) { - return tooltipPlacement; - } - if (!vertical) { - return 'top'; - } - return direction === 'rtl' ? 'left' : 'right'; - }; + const getTooltipPlacement = (tooltipPlacement?: TooltipPlacement, vertical?: boolean) => { + if (tooltipPlacement) { + return tooltipPlacement; + } + if (!vertical) { + return 'top'; + } + 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 ( + document.body)} + > + toggleTooltipVisible(index, true)} + onMouseLeave={() => toggleTooltipVisible(index, false)} + /> + + ); + }; - const handleWithTooltip: HandleGeneratorFn = ({ - tooltipPrefixCls, - prefixCls, - info: { value, dragging, index, ...restProps }, - }) => { const { - tipFormatter, - tooltipVisible, - tooltipPlacement, - getTooltipPopupContainer, - vertical, + prefixCls: customizePrefixCls, + tooltipPrefixCls: customizeTooltipPrefixCls, + range, + className, + ...restProps } = props; - const isTipFormatter = tipFormatter ? visibles[index] || dragging : false; - const visible = tooltipVisible || (tooltipVisible === undefined && isTipFormatter); - return ( - document.body)} - > - toggleTooltipVisible(index, true)} - onMouseLeave={() => toggleTooltipVisible(index, false)} + 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 ( + + handleWithTooltip({ + tooltipPrefixCls, + prefixCls, + info, + }) + } + prefixCls={prefixCls} /> - - ); - }; - - 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 ( - @@ -155,27 +169,10 @@ const Slider = React.forwardRef(( }) } prefixCls={prefixCls} - tooltipPrefixCls={tooltipPrefixCls} /> ); - } - return ( - - handleWithTooltip({ - tooltipPrefixCls, - prefixCls, - info, - }) - } - prefixCls={prefixCls} - tooltipPrefixCls={tooltipPrefixCls} - /> - ); -}); + }, +); Slider.displayName = 'Slider'; diff --git a/package.json b/package.json index 67bc9ec582..3c0362e505 100644 --- a/package.json +++ b/package.json @@ -137,7 +137,7 @@ "rc-rate": "~2.8.2", "rc-resize-observer": "^0.2.3", "rc-select": "~11.3.2", - "rc-slider": "~9.4.1", + "rc-slider": "~9.5.1", "rc-steps": "~4.1.0", "rc-switch": "~3.2.0", "rc-table": "~7.9.2", diff --git a/typings/custom-typings.d.ts b/typings/custom-typings.d.ts index 6a302f4c67..809a080bfd 100644 --- a/typings/custom-typings.d.ts +++ b/typings/custom-typings.d.ts @@ -46,14 +46,6 @@ declare module 'rc-rate'; 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-switch';