resizableHandler.tsx 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import React, { createRef, ReactNode } from 'react';
  2. import classNames from 'classnames';
  3. import PropTypes from 'prop-types';
  4. import { ResizableHandlerFoundation, ResizableHandlerAdapter } from '@douyinfe/semi-foundation/resizable/foundation';
  5. import { cssClasses } from '@douyinfe/semi-foundation/resizable/constants';
  6. import { Direction, HandlerCallback } from '@douyinfe/semi-foundation/resizable/types';
  7. import BaseComponent from '../../_base/baseComponent';
  8. const prefixCls = cssClasses.PREFIX;
  9. export interface ResizableHandlerProps {
  10. children?: ReactNode;
  11. direction?: Direction;
  12. onResizeStart?: HandlerCallback;
  13. className?: string;
  14. disabled?: boolean;
  15. style?: React.CSSProperties
  16. }
  17. export interface ResizableHandlerState {
  18. direction: Direction
  19. }
  20. class ResizableHandler extends BaseComponent<ResizableHandlerProps, ResizableHandlerState> {
  21. static propTypes = {
  22. children: PropTypes.node,
  23. direction: PropTypes.string,
  24. onResizeStart: PropTypes.func,
  25. className: PropTypes.string,
  26. disabled: PropTypes.bool,
  27. style: PropTypes.object,
  28. };
  29. static defaultProps: Partial<ResizableHandlerProps> = {
  30. };
  31. constructor(props: ResizableHandlerProps) {
  32. super(props);
  33. this.state = {
  34. direction: this.props.direction
  35. };
  36. this.resizeHandlerRef = createRef();
  37. this.foundation = new ResizableHandlerFoundation(this.adapter);
  38. }
  39. componentDidMount() {
  40. this.foundation.init();
  41. }
  42. componentDidUpdate(_prevProps: ResizableHandlerProps) {
  43. }
  44. componentWillUnmount() {
  45. this.foundation.destroy();
  46. }
  47. foundation: ResizableHandlerFoundation;
  48. get adapter(): ResizableHandlerAdapter<ResizableHandlerProps, ResizableHandlerState> {
  49. return {
  50. ...super.adapter,
  51. registerEvent: () => {
  52. this.resizeHandlerRef.current.addEventListener('mousedown', this.foundation.onMouseDown);
  53. this.resizeHandlerRef.current.addEventListener('touchstart', this.foundation.onTouchStart);
  54. },
  55. unregisterEvent: () => {
  56. this.resizeHandlerRef.current.removeEventListener('mousedown', this.foundation.onMouseDown);
  57. this.resizeHandlerRef.current.removeEventListener('touchstart', this.foundation.onTouchStart);
  58. },
  59. };
  60. }
  61. resizeHandlerRef: React.RefObject<HTMLDivElement>
  62. render() {
  63. const { children, style, className } = this.props;
  64. return (
  65. <div
  66. className={classNames(className, prefixCls + '-resizableHandler', prefixCls + '-resizableHandler-' + this.props.direction)}
  67. style={{
  68. ...style
  69. }}
  70. ref={this.resizeHandlerRef}
  71. >
  72. { children }
  73. </div>
  74. );
  75. }
  76. }
  77. export default ResizableHandler;