index.tsx 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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 {
  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<HTMLDivElement>;
  72. private maxHanleEl: React.RefObject<HTMLDivElement>;
  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,
  181. getMaxHandleEl: () => this.maxHanleEl,
  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. }
  262. }
  263. componentWillUnmount() {
  264. this.foundation.destroy();
  265. }
  266. renderHandle = () => {
  267. const { vertical, range, tooltipVisible, tipFormatter, 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledby, 'aria-valuetext': ariaValueText, getAriaValueText, disabled } = this.props;
  268. const { chooseMovePos, isDrag, isInRenderTree, firstDotFocusVisible, secondDotFocusVisible } = this.state;
  269. const stylePos = vertical ? 'top' : 'left';
  270. const percentInfo = this.foundation.getMinAndMaxPercent(this.state.currentValue);
  271. const minPercent = percentInfo.min;
  272. const maxPercent = percentInfo.max;
  273. const { tipVisible, tipChildren } = this.foundation.computeHandleVisibleVal(
  274. tooltipVisible && isInRenderTree,
  275. tipFormatter,
  276. range
  277. );
  278. const transform = { top: 'translateY(-50%)', left: 'translateX(-50%)' };
  279. const minClass = cls(cssClasses.HANDLE, {
  280. [`${cssClasses.HANDLE}-clicked`]: chooseMovePos === 'min' && isDrag,
  281. });
  282. const maxClass = cls(cssClasses.HANDLE, {
  283. [`${cssClasses.HANDLE}-clicked`]: chooseMovePos === 'max' && isDrag,
  284. });
  285. const { min, max, currentValue } = this.state;
  286. const commonAria = {
  287. 'aria-label': ariaLabel ?? (disabled ? 'Disabled Slider' : undefined),
  288. 'aria-labelledby': ariaLabelledby,
  289. 'aria-disabled': disabled
  290. };
  291. vertical && Object.assign(commonAria, { 'aria-orientation': 'vertical' });
  292. const handleContents = !range ? (
  293. <Tooltip
  294. content={tipChildren.min}
  295. position="top"
  296. trigger="custom"
  297. rePosKey={minPercent}
  298. visible={isInRenderTree && (tipVisible.min || firstDotFocusVisible)}
  299. className={`${cssClasses.HANDLE}-tooltip`}
  300. >
  301. <span
  302. onMouseOver={this.foundation.checkAndUpdateIsInRenderTreeState}
  303. ref={this.minHanleEl}
  304. className={minClass}
  305. style={{
  306. [stylePos]: `${minPercent * 100}%`,
  307. zIndex: chooseMovePos === 'min' && isDrag ? 2 : 1,
  308. transform: transform[stylePos],
  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. transform: transform[stylePos],
  366. }}
  367. onMouseDown={e => {
  368. this.foundation.onHandleDown(e, 'min');
  369. }}
  370. onMouseEnter={() => {
  371. this.foundation.onHandleEnter('min');
  372. }}
  373. onTouchStart={e => {
  374. this.foundation.onHandleTouchStart(e, 'min');
  375. }}
  376. onMouseLeave={() => {
  377. this.foundation.onHandleLeave();
  378. }}
  379. onMouseUp={e => {
  380. this.foundation.onHandleUp(e);
  381. }}
  382. onKeyUp={e => {
  383. this.foundation.onHandleUp(e);
  384. }}
  385. onTouchEnd={e => {
  386. this.foundation.onHandleUp(e);
  387. }}
  388. onKeyDown={(e)=>{
  389. this.foundation.handleKeyDown(e, 'min');
  390. }}
  391. onFocus={e => {
  392. this.foundation.onFocus(e, 'min');
  393. }}
  394. onBlur={(e) => {
  395. this.foundation.onBlur(e, 'min');
  396. }}
  397. role="slider"
  398. tabIndex={disabled ? -1 : 0}
  399. {...commonAria}
  400. aria-valuetext={getAriaValueText ? getAriaValueText(currentValue[0], 0) : ariaValueText}
  401. aria-valuenow={currentValue[0]}
  402. aria-valuemax={currentValue[1]}
  403. aria-valuemin={min}
  404. />
  405. </Tooltip>
  406. <Tooltip
  407. content={tipChildren.max}
  408. position="top"
  409. trigger="custom"
  410. rePosKey={maxPercent}
  411. visible={isInRenderTree && (tipVisible.max || secondDotFocusVisible)}
  412. className={`${cssClasses.HANDLE}-tooltip`}
  413. >
  414. <span
  415. ref={this.maxHanleEl}
  416. className={maxClass}
  417. style={{
  418. [stylePos]: `${maxPercent * 100}%`,
  419. zIndex: chooseMovePos === 'max' ? 2 : 1,
  420. transform: transform[stylePos],
  421. }}
  422. onMouseDown={e => {
  423. this.foundation.onHandleDown(e, 'max');
  424. }}
  425. onMouseEnter={() => {
  426. this.foundation.onHandleEnter('max');
  427. }}
  428. onMouseLeave={() => {
  429. this.foundation.onHandleLeave();
  430. }}
  431. onMouseUp={e => {
  432. this.foundation.onHandleUp(e);
  433. }}
  434. onKeyUp={e => {
  435. this.foundation.onHandleUp(e);
  436. }}
  437. onTouchStart={e => {
  438. this.foundation.onHandleTouchStart(e, 'max');
  439. }}
  440. onTouchEnd={e => {
  441. this.foundation.onHandleUp(e);
  442. }}
  443. onKeyDown={e =>{
  444. this.foundation.handleKeyDown(e, 'max');
  445. }}
  446. onFocus={e => {
  447. this.foundation.onFocus(e, 'max');
  448. }}
  449. onBlur={(e) => {
  450. this.foundation.onBlur(e, 'max');
  451. }}
  452. role="slider"
  453. tabIndex={disabled ? -1 : 0}
  454. {...commonAria}
  455. aria-valuetext={getAriaValueText ? getAriaValueText(currentValue[1], 1) : ariaValueText}
  456. aria-valuenow={currentValue[1]}
  457. aria-valuemax={max}
  458. aria-valuemin={currentValue[0]}
  459. />
  460. </Tooltip>
  461. </React.Fragment>
  462. );
  463. return handleContents;
  464. };
  465. renderTrack = () => {
  466. const { range, included, vertical } = this.props;
  467. const percentInfo = this.foundation.getMinAndMaxPercent(this.state.currentValue);
  468. const minPercent = percentInfo.min;
  469. const maxPercent = percentInfo.max;
  470. let trackStyle: CSSProperties = !vertical ?
  471. {
  472. width: range ? `${(maxPercent - minPercent) * 100}%` : `${minPercent * 100}%`,
  473. left: range ? `${minPercent * 100}%` : 0,
  474. } :
  475. {
  476. height: range ? `${(maxPercent - minPercent) * 100}%` : `${minPercent * 100}%`,
  477. top: range ? `${minPercent * 100}%` : 0,
  478. };
  479. trackStyle = included ? trackStyle : {};
  480. return (// eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions
  481. <div className={cssClasses.TRACK} style={trackStyle} onClick={this.foundation.handleWrapClick}>
  482. {/* {this.renderTrack} */}
  483. </div>
  484. );
  485. };
  486. renderStepDot = () => {
  487. const { min, max, vertical, marks } = this.props;
  488. const stylePos = vertical ? 'top' : 'left';
  489. const labelContent =
  490. marks && Object.keys(marks).length > 0 ? (
  491. <div className={cssClasses.DOTS}>
  492. {Object.keys(marks).map(mark => {
  493. const activeResult = this.foundation.isMarkActive(Number(mark));
  494. const markClass = cls(`${prefixCls}-dot`, {
  495. [`${prefixCls}-dot-active`]: this.foundation.isMarkActive(Number(mark)) === 'active',
  496. });
  497. const markPercent = (Number(mark) - min) / (max - min);
  498. return activeResult ? (
  499. // eslint-disable-next-line jsx-a11y/no-static-element-interactions
  500. <span
  501. key={mark}
  502. onClick={this.foundation.handleWrapClick}
  503. className={markClass}
  504. style={{ [stylePos]: `calc(${markPercent * 100}% - 2px)` }}
  505. />
  506. ) : null;
  507. })}
  508. </div>
  509. ) : null;
  510. return labelContent;
  511. };
  512. renderLabel = () => {
  513. const { min, max, vertical, marks, verticalReverse } = this.props;
  514. const stylePos = vertical ? 'top' : 'left';
  515. const labelContent =
  516. marks && Object.keys(marks).length > 0 ? (
  517. <div className={cssClasses.MARKS + ((vertical && verticalReverse) ? '-reverse' : '')}>
  518. {Object.keys(marks).map(mark => {
  519. const activeResult = this.foundation.isMarkActive(Number(mark));
  520. const markPercent = (Number(mark) - min) / (max - min);
  521. return activeResult ? (
  522. // eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions
  523. <span
  524. key={mark}
  525. className={cls(`${prefixCls}-mark${(vertical && verticalReverse) ? '-reverse' : ''}`)}
  526. style={{ [stylePos]: `${markPercent * 100}%` }}
  527. onClick={this.foundation.handleWrapClick}
  528. >
  529. {marks[mark]}
  530. </span>
  531. ) : null;
  532. })}
  533. </div>
  534. ) : null;
  535. return labelContent;
  536. };
  537. _getAriaValueText = (value: number, index?: number) => {
  538. const { getAriaValueText } = this.props;
  539. return getAriaValueText ? getAriaValueText(value, index) : value;
  540. }
  541. render() {
  542. const { disabled, currentValue, min, max } = this.state;
  543. const { vertical, verticalReverse, style, railStyle, range, className } = this.props;
  544. const wrapperClass = cls(
  545. `${prefixCls}-wrapper`,
  546. {
  547. [`${prefixCls}-disabled`]: disabled,
  548. [`${cssClasses.VERTICAL}-wrapper`]: vertical,
  549. [`${prefixCls}-reverse`]: vertical && verticalReverse
  550. },
  551. className
  552. );
  553. const boundaryClass = cls(`${prefixCls}-boundary`, {
  554. [`${prefixCls}-boundary-show`]: this.props.showBoundary && this.state.showBoundary,
  555. });
  556. const sliderCls = cls({
  557. [`${prefixCls}`]: !vertical,
  558. [cssClasses.VERTICAL]: vertical,
  559. });
  560. const ariaLabel = range ? `Range: ${this._getAriaValueText(currentValue[0], 0)} to ${this._getAriaValueText(currentValue[1], 1)}` : undefined;
  561. const slider = (
  562. <div
  563. className={wrapperClass}
  564. style={style}
  565. ref={this.sliderEl}
  566. aria-label={ariaLabel}
  567. onMouseEnter={() => this.foundation.handleWrapperEnter()}
  568. onMouseLeave={() => this.foundation.handleWrapperLeave()}
  569. >
  570. {// eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions
  571. <div
  572. className={`${prefixCls}-rail`}
  573. onClick={this.foundation.handleWrapClick}
  574. style={railStyle}
  575. />
  576. }
  577. {this.renderTrack()}
  578. {this.renderStepDot()}
  579. <div>{this.renderHandle()}</div>
  580. {this.renderLabel()}
  581. <div className={boundaryClass}>
  582. <span className={`${prefixCls}-boundary-min`}>{min}</span>
  583. <span className={`${prefixCls}-boundary-max`}>{max}</span>
  584. </div>
  585. </div>
  586. );
  587. if (!vertical) {
  588. return <div className={sliderCls}>{slider}</div>;
  589. }
  590. return slider;
  591. }
  592. private _addEventListener<T extends keyof HTMLElementEventMap>(target: HTMLElement, eventName: T, callback: (e: HTMLElementEventMap[T]) => void, ...rests: any) {
  593. if (target.addEventListener) {
  594. target.addEventListener(eventName, callback, ...rests);
  595. const clearSelf = () => {
  596. target?.removeEventListener(eventName, callback);
  597. Promise.resolve().then(() => {
  598. this.eventListenerSet.delete(clearSelf);
  599. });
  600. };
  601. this.eventListenerSet.add(clearSelf);
  602. return clearSelf;
  603. } else {
  604. return noop;
  605. }
  606. }
  607. }