resizableHandler.tsx 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. },
  54. unregisterEvent: () => {
  55. this.resizeHandlerRef.current.removeEventListener('mousedown', this.foundation.onMouseDown);
  56. },
  57. };
  58. }
  59. resizeHandlerRef: React.RefObject<HTMLDivElement>
  60. render() {
  61. const { children, style, className } = this.props;
  62. return (
  63. <div
  64. className={classNames(className, prefixCls + '-resizableHandler', prefixCls + '-resizableHandler-' + this.props.direction)}
  65. style={{
  66. ...style
  67. }}
  68. ref={this.resizeHandlerRef}
  69. >
  70. { children }
  71. </div>
  72. );
  73. }
  74. }
  75. export default ResizableHandler;