index.tsx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /* eslint-disable max-lines-per-function */
  2. /* eslint-disable react/no-find-dom-node */
  3. import React, { CSSProperties } from 'react';
  4. import ReactDOM from 'react-dom';
  5. import PropTypes from 'prop-types';
  6. import cls from 'classnames';
  7. import { cssClasses } from '@douyinfe/semi-foundation/slider/constants';
  8. import BaseComponent from '../_base/baseComponent';
  9. import SliderFoundation, { SliderAdapter, SliderProps as BasicSliceProps, SliderState, tipFormatterBasicType } from '@douyinfe/semi-foundation/slider/foundation';
  10. import Tooltip from '../tooltip/index';
  11. import '@douyinfe/semi-foundation/slider/slider.scss';
  12. import { isEqual, noop } from 'lodash';
  13. const prefixCls = cssClasses.PREFIX;
  14. export interface SliderProps extends BasicSliceProps {
  15. style?: CSSProperties;
  16. railStyle?: CSSProperties
  17. }
  18. export type {
  19. SliderState
  20. };
  21. function domIsInRenderTree(e: HTMLElement) {
  22. if (!e) {
  23. return false;
  24. }
  25. return Boolean(e.offsetWidth || e.offsetHeight || e.getClientRects().length);
  26. }
  27. export default class Slider extends BaseComponent<SliderProps, SliderState> {
  28. static propTypes = {
  29. // allowClear: PropTypes.bool,
  30. defaultValue: PropTypes.oneOfType([PropTypes.number, PropTypes.array]),
  31. disabled: PropTypes.bool,
  32. included: PropTypes.bool, // Whether to juxtapose. Allow dragging
  33. marks: PropTypes.object, // Scale
  34. max: PropTypes.number,
  35. min: PropTypes.number,
  36. range: PropTypes.bool, // Whether both sides
  37. step: PropTypes.number,
  38. tipFormatter: PropTypes.func,
  39. value: PropTypes.oneOfType([PropTypes.number, PropTypes.array]),
  40. vertical: PropTypes.bool,
  41. onAfterChange: PropTypes.func, // OnmouseUp and triggered when clicked
  42. onChange: PropTypes.func,
  43. tooltipVisible: PropTypes.bool,
  44. style: PropTypes.object,
  45. className: PropTypes.string,
  46. showBoundary: PropTypes.bool,
  47. railStyle: PropTypes.object,
  48. verticalReverse: PropTypes.bool,
  49. getAriaValueText: PropTypes.func,
  50. } as any;
  51. static defaultProps: Partial<SliderProps> = {
  52. // allowClear: false,
  53. disabled: false,
  54. included: true, // No is juxtaposition. Allow dragging
  55. max: 100,
  56. min: 0,
  57. range: false, // Whether both sides
  58. step: 1,
  59. tipFormatter: (value: tipFormatterBasicType | tipFormatterBasicType[]) => value,
  60. vertical: false,
  61. showBoundary: false,
  62. onAfterChange: (value: number | number[]) => {
  63. // console.log(value);
  64. },
  65. onChange: (value: number | number[]) => {
  66. // console.log(value);
  67. },
  68. verticalReverse: false
  69. };
  70. private sliderEl: React.RefObject<HTMLDivElement>;
  71. private minHanleEl: React.RefObject<HTMLSpanElement>;
  72. private maxHanleEl: React.RefObject<HTMLSpanElement>;
  73. private dragging: boolean[];
  74. private eventListenerSet: Set<() => void>;
  75. foundation: SliderFoundation;
  76. constructor(props: SliderProps) {
  77. super(props);
  78. let { value } = this.props;
  79. if (!value) {
  80. value = this.props.defaultValue;
  81. }
  82. this.state = {
  83. // eslint-disable-next-line no-nested-ternary
  84. currentValue: value ? value : this.props.range ? [0, 0] : 0,
  85. min: this.props.min || 0,
  86. max: this.props.max || 0,
  87. focusPos: '',
  88. onChange: this.props.onChange,
  89. disabled: this.props.disabled || false,
  90. chooseMovePos: '',
  91. isDrag: false,
  92. clickValue: 0,
  93. showBoundary: false,
  94. isInRenderTree: true,
  95. firstDotFocusVisible: false,
  96. secondDotFocusVisible: false,
  97. };
  98. this.sliderEl = React.createRef();
  99. this.minHanleEl = React.createRef();
  100. this.maxHanleEl = React.createRef();
  101. this.dragging = [false, false];
  102. this.foundation = new SliderFoundation(this.adapter);
  103. this.eventListenerSet = new Set();
  104. }
  105. get adapter(): SliderAdapter {
  106. return {
  107. ...super.adapter,
  108. getSliderLengths: () => {
  109. if (this.sliderEl && this.sliderEl.current) {
  110. const rect = this.sliderEl.current.getBoundingClientRect();
  111. const offset = {
  112. x: this.sliderEl.current.offsetLeft,
  113. y: this.sliderEl.current.offsetTop,
  114. };
  115. return {
  116. sliderX: offset.x,
  117. sliderY: offset.y,
  118. sliderWidth: rect.width,
  119. sliderHeight: rect.height,
  120. };
  121. }
  122. return {
  123. sliderX: 0,
  124. sliderY: 0,
  125. sliderWidth: 0,
  126. sliderHeight: 0,
  127. };
  128. },
  129. getParentRect: (): DOMRect | undefined => {
  130. const parentObj = this.sliderEl && this.sliderEl.current && this.sliderEl.current.offsetParent;
  131. if (!parentObj) {
  132. return undefined;
  133. }
  134. return parentObj.getBoundingClientRect();
  135. },
  136. getScrollParentVal: () => {
  137. const scrollParent = this.foundation.getScrollParent(this.sliderEl.current);
  138. return {
  139. scrollTop: scrollParent.scrollTop,
  140. scrollLeft: scrollParent.scrollLeft,
  141. };
  142. },
  143. isEventFromHandle: (e: React.MouseEvent) => {
  144. const handles = [this.minHanleEl, this.maxHanleEl];
  145. let flag = false;
  146. handles.forEach(handle => {
  147. if (!handle) {
  148. return;
  149. }
  150. const handleInstance = handle && handle.current;
  151. const handleDom = ReactDOM.findDOMNode(handleInstance);
  152. if (handleDom && handleDom.contains(e.target as Node)) {
  153. flag = true;
  154. }
  155. });
  156. return flag;
  157. },
  158. getOverallVars: () => ({
  159. dragging: this.dragging,
  160. }),
  161. updateDisabled: (disabled: boolean) => {
  162. this.setState({ disabled });
  163. },
  164. transNewPropsToState<K extends keyof SliderState>(stateObj: Pick<SliderState, K>, callback = noop) {
  165. this.setState(stateObj, callback);
  166. },
  167. notifyChange: (cbValue: number | number[]) => this.props.onChange(cbValue),
  168. setDragging: (value: boolean[]) => {
  169. this.dragging = value;
  170. },
  171. updateCurrentValue: (value: number | number[]) => {
  172. const { currentValue } = this.state;
  173. if (value !== currentValue) {
  174. this.setState({ currentValue: value });
  175. }
  176. },
  177. setOverallVars: (key: string, value: any) => {
  178. this[key] = value;
  179. },
  180. getMinHandleEl: () => this.minHanleEl.current,
  181. getMaxHandleEl: () => this.maxHanleEl.current,
  182. onHandleDown: (e: React.MouseEvent) => {
  183. this._addEventListener(document.body, 'mousemove', this.foundation.onHandleMove, false);
  184. this._addEventListener(document.body, 'mouseup', this.foundation.onHandleUp, false);
  185. this._addEventListener(document.body, 'touchmove', this.foundation.onHandleTouchMove, false);
  186. },
  187. onHandleMove: (mousePos: number, isMin: boolean, stateChangeCallback = noop, clickTrack = false, outPutValue): boolean | void => {
  188. const sliderDOMIsInRenderTree = this.foundation.checkAndUpdateIsInRenderTreeState();
  189. if (!sliderDOMIsInRenderTree) {
  190. return;
  191. }
  192. const { value } = this.props;
  193. let finalOutPutValue = outPutValue;
  194. if (finalOutPutValue === undefined) {
  195. const moveValue = this.foundation.transPosToValue(mousePos, isMin);
  196. if (moveValue === false) {
  197. return;
  198. }
  199. finalOutPutValue = this.foundation.outPutValue(moveValue);
  200. }
  201. const { currentValue } = this.state;
  202. if (!isEqual(this.foundation.outPutValue(currentValue), finalOutPutValue)) {
  203. if (!clickTrack && this.foundation.valueFormatIsCorrect(value)) {
  204. // still require afterChangeCallback when click on the track directly, need skip here
  205. return false;
  206. }
  207. this.setState({
  208. currentValue: finalOutPutValue,
  209. }, stateChangeCallback);
  210. }
  211. },
  212. setEventDefault: (e: React.MouseEvent) => {
  213. e.stopPropagation();
  214. e.preventDefault();
  215. },
  216. setStateVal: <K extends keyof SliderState>(name: K, val: SliderState[K]) => {
  217. this.setState({ [name]: val } as Pick<SliderState, K>);
  218. },
  219. checkAndUpdateIsInRenderTreeState: () => {
  220. const sliderDOMIsInRenderTree = domIsInRenderTree(this.sliderEl.current);
  221. if (sliderDOMIsInRenderTree !== this.state.isInRenderTree) {
  222. this.setState({ isInRenderTree: sliderDOMIsInRenderTree });
  223. }
  224. return sliderDOMIsInRenderTree;
  225. },
  226. onHandleEnter: (pos: SliderState['focusPos']) => {
  227. this.setState({ focusPos: pos });
  228. },
  229. onHandleLeave: () => {
  230. this.setState({ focusPos: '' });
  231. },
  232. onHandleUpBefore: (e: React.MouseEvent) => {
  233. e.stopPropagation();
  234. e.preventDefault();
  235. document.body.removeEventListener('mousemove', this.foundation.onHandleMove, false);
  236. document.body.removeEventListener('mouseup', this.foundation.onHandleUp, false);
  237. },
  238. onHandleUpAfter: () => {
  239. const { currentValue } = this.state;
  240. const value = this.foundation.outPutValue(currentValue);
  241. this.props.onAfterChange(value);
  242. },
  243. unSubscribeEventListener: () => {
  244. Array.from(this.eventListenerSet).forEach(clear => clear());
  245. },
  246. };
  247. }
  248. componentDidMount() {
  249. this.foundation.init();
  250. }
  251. componentDidUpdate(prevProps: SliderProps, prevState: SliderState) {
  252. const hasPropValueChange = !isEqual(this.props.value, prevProps.value);
  253. const hasPropDisabledChange = this.props.disabled !== prevProps.disabled;
  254. if (hasPropDisabledChange) {
  255. this.foundation.handleDisabledChange(this.props.disabled);
  256. }
  257. if (hasPropValueChange) {
  258. const nextValue = this.props.value;
  259. const prevValue = this.state.currentValue;
  260. this.foundation.handleValueChange(prevValue, nextValue);
  261. // trigger onAfterChange when value is controlled and changed
  262. this.props.onAfterChange(this.props.value);
  263. }
  264. }
  265. componentWillUnmount() {
  266. this.foundation.destroy();
  267. }
  268. renderHandle = () => {
  269. const { vertical, range, tooltipVisible, tipFormatter, 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledby, 'aria-valuetext': ariaValueText, getAriaValueText, disabled } = this.props;
  270. const { chooseMovePos, isDrag, isInRenderTree, firstDotFocusVisible, secondDotFocusVisible } = this.state;
  271. const stylePos = vertical ? 'top' : 'left';
  272. const percentInfo = this.foundation.getMinAndMaxPercent(this.state.currentValue);
  273. const minPercent = percentInfo.min;
  274. const maxPercent = percentInfo.max;
  275. const { tipVisible, tipChildren } = this.foundation.computeHandleVisibleVal(
  276. tooltipVisible && isInRenderTree,
  277. tipFormatter,
  278. range
  279. );
  280. const minClass = cls(cssClasses.HANDLE, {
  281. [`${cssClasses.HANDLE}-clicked`]: chooseMovePos === 'min' && isDrag,
  282. });
  283. const maxClass = cls(cssClasses.HANDLE, {
  284. [`${cssClasses.HANDLE}-clicked`]: chooseMovePos === 'max' && isDrag,
  285. });
  286. const { min, max, currentValue } = this.state;
  287. const commonAria = {
  288. 'aria-label': ariaLabel ?? (disabled ? 'Disabled Slider' : undefined),
  289. 'aria-labelledby': ariaLabelledby,
  290. 'aria-disabled': disabled
  291. };
  292. vertical && Object.assign(commonAria, { 'aria-orientation': 'vertical' });
  293. const handleContents = !range ? (
  294. <Tooltip
  295. content={tipChildren.min}
  296. position="top"
  297. trigger="custom"
  298. rePosKey={minPercent}
  299. visible={isInRenderTree && (tipVisible.min || firstDotFocusVisible)}
  300. className={`${cssClasses.HANDLE}-tooltip`}
  301. >
  302. <span
  303. onMouseOver={this.foundation.checkAndUpdateIsInRenderTreeState}
  304. ref={this.minHanleEl}
  305. className={minClass}
  306. style={{
  307. [stylePos]: `${minPercent * 100}%`,
  308. zIndex: chooseMovePos === 'min' && isDrag ? 2 : 1,
  309. }}
  310. onMouseDown={e => {
  311. this.foundation.onHandleDown(e, 'min');
  312. }}
  313. onMouseEnter={() => {
  314. this.foundation.onHandleEnter('min');
  315. }}
  316. onTouchStart={e => {
  317. this.foundation.onHandleTouchStart(e, 'min');
  318. }}
  319. onMouseLeave={() => {
  320. this.foundation.onHandleLeave();
  321. }}
  322. onMouseUp={e => {
  323. this.foundation.onHandleUp(e);
  324. }}
  325. onKeyUp={e => {
  326. this.foundation.onHandleUp(e);
  327. }}
  328. onTouchEnd={e => {
  329. this.foundation.onHandleUp(e);
  330. }}
  331. onKeyDown={(e)=>{
  332. this.foundation.handleKeyDown(e, 'min');
  333. }}
  334. onFocus={e => {
  335. this.foundation.onFocus(e, 'min');
  336. }}
  337. onBlur={(e) => {
  338. this.foundation.onBlur(e, 'min');
  339. }}
  340. role="slider"
  341. aria-valuetext={getAriaValueText ? getAriaValueText(currentValue as number, 0) : ariaValueText}
  342. tabIndex={disabled ? -1 : 0}
  343. {...commonAria}
  344. aria-valuenow={currentValue as number}
  345. aria-valuemax={max}
  346. aria-valuemin={min}
  347. />
  348. </Tooltip>
  349. ) : (
  350. <React.Fragment>
  351. <Tooltip
  352. content={tipChildren.min}
  353. position="top"
  354. trigger="custom"
  355. rePosKey={minPercent}
  356. visible={isInRenderTree && (tipVisible.min || firstDotFocusVisible)}
  357. className={`${cssClasses.HANDLE}-tooltip`}
  358. >
  359. <span
  360. ref={this.minHanleEl}
  361. className={minClass}
  362. style={{
  363. [stylePos]: `${minPercent * 100}%`,
  364. zIndex: chooseMovePos === 'min' ? 2 : 1,
  365. }}
  366. onMouseDown={e => {
  367. this.foundation.onHandleDown(e, 'min');
  368. }}
  369. onMouseEnter={() => {
  370. this.foundation.onHandleEnter('min');
  371. }}
  372. onTouchStart={e => {
  373. this.foundation.onHandleTouchStart(e, 'min');
  374. }}
  375. onMouseLeave={() => {
  376. this.foundation.onHandleLeave();
  377. }}
  378. onMouseUp={e => {
  379. this.foundation.onHandleUp(e);
  380. }}
  381. onKeyUp={e => {
  382. this.foundation.onHandleUp(e);
  383. }}
  384. onTouchEnd={e => {
  385. this.foundation.onHandleUp(e);
  386. }}
  387. onKeyDown={(e)=>{
  388. this.foundation.handleKeyDown(e, 'min');
  389. }}
  390. onFocus={e => {
  391. this.foundation.onFocus(e, 'min');
  392. }}
  393. onBlur={(e) => {
  394. this.foundation.onBlur(e, 'min');
  395. }}
  396. role="slider"
  397. tabIndex={disabled ? -1 : 0}
  398. {...commonAria}
  399. aria-valuetext={getAriaValueText ? getAriaValueText(currentValue[0], 0) : ariaValueText}
  400. aria-valuenow={currentValue[0]}
  401. aria-valuemax={currentValue[1]}
  402. aria-valuemin={min}
  403. />
  404. </Tooltip>
  405. <Tooltip
  406. content={tipChildren.max}
  407. position="top"
  408. trigger="custom"
  409. rePosKey={maxPercent}
  410. visible={isInRenderTree && (tipVisible.max || secondDotFocusVisible)}
  411. className={`${cssClasses.HANDLE}-tooltip`}
  412. >
  413. <span
  414. ref={this.maxHanleEl}
  415. className={maxClass}
  416. style={{
  417. [stylePos]: `${maxPercent * 100}%`,
  418. zIndex: chooseMovePos === 'max' ? 2 : 1,
  419. }}
  420. onMouseDown={e => {
  421. this.foundation.onHandleDown(e, 'max');
  422. }}
  423. onMouseEnter={() => {
  424. this.foundation.onHandleEnter('max');
  425. }}
  426. onMouseLeave={() => {
  427. this.foundation.onHandleLeave();
  428. }}
  429. onMouseUp={e => {
  430. this.foundation.onHandleUp(e);
  431. }}
  432. onKeyUp={e => {
  433. this.foundation.onHandleUp(e);
  434. }}
  435. onTouchStart={e => {
  436. this.foundation.onHandleTouchStart(e, 'max');
  437. }}
  438. onTouchEnd={e => {
  439. this.foundation.onHandleUp(e);
  440. }}
  441. onKeyDown={e =>{
  442. this.foundation.handleKeyDown(e, 'max');
  443. }}
  444. onFocus={e => {
  445. this.foundation.onFocus(e, 'max');
  446. }}
  447. onBlur={(e) => {
  448. this.foundation.onBlur(e, 'max');
  449. }}
  450. role="slider"
  451. tabIndex={disabled ? -1 : 0}
  452. {...commonAria}
  453. aria-valuetext={getAriaValueText ? getAriaValueText(currentValue[1], 1) : ariaValueText}
  454. aria-valuenow={currentValue[1]}
  455. aria-valuemax={max}
  456. aria-valuemin={currentValue[0]}
  457. />
  458. </Tooltip>
  459. </React.Fragment>
  460. );
  461. return handleContents;
  462. };
  463. renderTrack = () => {
  464. const { range, included, vertical } = this.props;
  465. const percentInfo = this.foundation.getMinAndMaxPercent(this.state.currentValue);
  466. const minPercent = percentInfo.min;
  467. const maxPercent = percentInfo.max;
  468. let trackStyle: CSSProperties = !vertical ?
  469. {
  470. width: range ? `${(maxPercent - minPercent) * 100}%` : `${minPercent * 100}%`,
  471. left: range ? `${minPercent * 100}%` : 0,
  472. } :
  473. {
  474. height: range ? `${(maxPercent - minPercent) * 100}%` : `${minPercent * 100}%`,
  475. top: range ? `${minPercent * 100}%` : 0,
  476. };
  477. trackStyle = included ? trackStyle : {};
  478. return (// eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions
  479. <div className={cssClasses.TRACK} style={trackStyle} onClick={this.foundation.handleWrapClick}>
  480. {/* {this.renderTrack} */}
  481. </div>
  482. );
  483. };
  484. renderStepDot = () => {
  485. const { min, max, vertical, marks } = this.props;
  486. const stylePos = vertical ? 'top' : 'left';
  487. const labelContent =
  488. marks && Object.keys(marks).length > 0 ? (
  489. <div className={cssClasses.DOTS}>
  490. {Object.keys(marks).map(mark => {
  491. const activeResult = this.foundation.isMarkActive(Number(mark));
  492. const markClass = cls(`${prefixCls}-dot`, {
  493. [`${prefixCls}-dot-active`]: this.foundation.isMarkActive(Number(mark)) === 'active',
  494. });
  495. const markPercent = (Number(mark) - min) / (max - min);
  496. return activeResult ? (
  497. // eslint-disable-next-line jsx-a11y/no-static-element-interactions
  498. <span
  499. key={mark}
  500. onClick={this.foundation.handleWrapClick}
  501. className={markClass}
  502. style={{ [stylePos]: `calc(${markPercent * 100}% - 2px)` }}
  503. />
  504. ) : null;
  505. })}
  506. </div>
  507. ) : null;
  508. return labelContent;
  509. };
  510. renderLabel = () => {
  511. const { min, max, vertical, marks, verticalReverse } = this.props;
  512. const stylePos = vertical ? 'top' : 'left';
  513. const labelContent =
  514. marks && Object.keys(marks).length > 0 ? (
  515. <div className={cssClasses.MARKS + ((vertical && verticalReverse) ? '-reverse' : '')}>
  516. {Object.keys(marks).map(mark => {
  517. const activeResult = this.foundation.isMarkActive(Number(mark));
  518. const markPercent = (Number(mark) - min) / (max - min);
  519. return activeResult ? (
  520. // eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions
  521. <span
  522. key={mark}
  523. className={cls(`${prefixCls}-mark${(vertical && verticalReverse) ? '-reverse' : ''}`)}
  524. style={{ [stylePos]: `${markPercent * 100}%` }}
  525. onClick={this.foundation.handleWrapClick}
  526. >
  527. {marks[mark]}
  528. </span>
  529. ) : null;
  530. })}
  531. </div>
  532. ) : null;
  533. return labelContent;
  534. };
  535. _getAriaValueText = (value: number, index?: number) => {
  536. const { getAriaValueText } = this.props;
  537. return getAriaValueText ? getAriaValueText(value, index) : value;
  538. }
  539. render() {
  540. const { disabled, currentValue, min, max } = this.state;
  541. const { vertical, verticalReverse, style, railStyle, range, className } = this.props;
  542. const wrapperClass = cls(
  543. `${prefixCls}-wrapper`,
  544. {
  545. [`${prefixCls}-disabled`]: disabled,
  546. [`${cssClasses.VERTICAL}-wrapper`]: vertical,
  547. [`${prefixCls}-reverse`]: vertical && verticalReverse
  548. },
  549. className
  550. );
  551. const boundaryClass = cls(`${prefixCls}-boundary`, {
  552. [`${prefixCls}-boundary-show`]: this.props.showBoundary && this.state.showBoundary,
  553. });
  554. const sliderCls = cls({
  555. [`${prefixCls}`]: !vertical,
  556. [cssClasses.VERTICAL]: vertical,
  557. });
  558. const ariaLabel = range ? `Range: ${this._getAriaValueText(currentValue[0], 0)} to ${this._getAriaValueText(currentValue[1], 1)}` : undefined;
  559. const slider = (
  560. <div
  561. className={wrapperClass}
  562. style={style}
  563. ref={this.sliderEl}
  564. aria-label={ariaLabel}
  565. onMouseEnter={() => this.foundation.handleWrapperEnter()}
  566. onMouseLeave={() => this.foundation.handleWrapperLeave()}
  567. >
  568. {// eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions
  569. <div
  570. className={`${prefixCls}-rail`}
  571. onClick={this.foundation.handleWrapClick}
  572. style={railStyle}
  573. />
  574. }
  575. {this.renderTrack()}
  576. {this.renderStepDot()}
  577. <div>{this.renderHandle()}</div>
  578. {this.renderLabel()}
  579. <div className={boundaryClass}>
  580. <span className={`${prefixCls}-boundary-min`}>{min}</span>
  581. <span className={`${prefixCls}-boundary-max`}>{max}</span>
  582. </div>
  583. </div>
  584. );
  585. if (!vertical) {
  586. return <div className={sliderCls}>{slider}</div>;
  587. }
  588. return slider;
  589. }
  590. private _addEventListener<T extends keyof HTMLElementEventMap>(target: HTMLElement, eventName: T, callback: (e: HTMLElementEventMap[T]) => void, ...rests: any) {
  591. if (target.addEventListener) {
  592. target.addEventListener(eventName, callback, ...rests);
  593. const clearSelf = () => {
  594. target?.removeEventListener(eventName, callback);
  595. Promise.resolve().then(() => {
  596. this.eventListenerSet.delete(clearSelf);
  597. });
  598. };
  599. this.eventListenerSet.add(clearSelf);
  600. return clearSelf;
  601. } else {
  602. return noop;
  603. }
  604. }
  605. }