index.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* eslint-disable max-len */
  2. import React from 'react';
  3. import cls from 'classnames';
  4. import PropTypes from 'prop-types';
  5. import SwitchFoudation, { SwitchAdapter } from '@douyinfe/semi-foundation/switch/foundation';
  6. import { cssClasses, strings } from '@douyinfe/semi-foundation/switch/constants';
  7. import BaseComponent from '../_base/baseComponent';
  8. import '@douyinfe/semi-foundation/switch/switch.scss';
  9. import { noop } from 'lodash-es';
  10. import Spin from '../spin';
  11. export interface SwitchProps {
  12. defaultChecked?: boolean;
  13. checked?: boolean;
  14. disabled?: boolean;
  15. onChange?: (checked: boolean, e: React.ChangeEvent<HTMLInputElement>) => void;
  16. loading?: boolean;
  17. className?: string;
  18. style?: React.CSSProperties;
  19. onMouseEnter?: (e: React.MouseEvent) => any;
  20. onMouseLeave?: (e: React.MouseEvent) => any;
  21. size?: 'large' | 'default' | 'small';
  22. checkedText?: React.ReactNode;
  23. uncheckedText?: React.ReactNode;
  24. }
  25. export interface SwitchState {
  26. nativeControlChecked: boolean;
  27. nativeControlDisabled: boolean;
  28. }
  29. class Switch extends BaseComponent<SwitchProps, SwitchState> {
  30. static propTypes = {
  31. className: PropTypes.string,
  32. checked: PropTypes.bool,
  33. checkedText: PropTypes.node,
  34. defaultChecked: PropTypes.bool,
  35. disabled: PropTypes.bool,
  36. loading: PropTypes.bool,
  37. onChange: PropTypes.func,
  38. onMouseEnter: PropTypes.func,
  39. onMouseLeave: PropTypes.func,
  40. style: PropTypes.object,
  41. size: PropTypes.oneOf<SwitchProps['size']>(strings.SIZE_MAP),
  42. uncheckedText: PropTypes.node,
  43. };
  44. static defaultProps: Partial<SwitchProps> = {
  45. disabled: false,
  46. className: '',
  47. onChange: noop,
  48. loading: false,
  49. onMouseEnter: noop,
  50. onMouseLeave: noop,
  51. size: 'default',
  52. };
  53. private switchRef: React.RefObject<HTMLInputElement>;
  54. constructor(props: SwitchProps) {
  55. super(props);
  56. this.state = {
  57. nativeControlChecked: false,
  58. nativeControlDisabled: false,
  59. };
  60. this.switchRef = React.createRef();
  61. this.foundation = new SwitchFoudation(this.adapter);
  62. }
  63. componentDidMount() {
  64. this.foundation.init();
  65. }
  66. componentDidUpdate(prevProps: SwitchProps) {
  67. if (this.props.checked !== prevProps.checked) {
  68. this.foundation.setChecked(this.props.checked);
  69. }
  70. if (this.props.disabled !== prevProps.disabled) {
  71. this.foundation.setDisabled(this.props.disabled);
  72. }
  73. }
  74. componentWillUnmount() {
  75. this.foundation.destroy();
  76. }
  77. get adapter(): SwitchAdapter<SwitchProps, SwitchState> {
  78. return {
  79. ...super.adapter,
  80. setNativeControlChecked: (nativeControlChecked: boolean): void => {
  81. this.setState({ nativeControlChecked });
  82. },
  83. setNativeControlDisabled: (nativeControlDisabled: boolean): void => {
  84. this.setState({ nativeControlDisabled });
  85. },
  86. notifyChange: (checked: boolean, e: React.ChangeEvent<HTMLInputElement>): void => {
  87. this.props.onChange(checked, e);
  88. },
  89. };
  90. }
  91. render() {
  92. const { nativeControlChecked, nativeControlDisabled } = this.state;
  93. const { className, style, onMouseEnter, onMouseLeave, size, checkedText, uncheckedText, loading } = this.props;
  94. const wrapperCls = cls(className, {
  95. [cssClasses.PREFIX]: true,
  96. [cssClasses.CHECKED]: nativeControlChecked,
  97. [cssClasses.DISABLED]: nativeControlDisabled,
  98. [cssClasses.LARGE]: size === 'large',
  99. [cssClasses.SMALL]: size === 'small',
  100. [cssClasses.LOADING]: loading,
  101. });
  102. const switchProps = {
  103. type: 'checkbox',
  104. role: 'switch',
  105. className: cssClasses.NATIVE_CONTROL,
  106. disabled: nativeControlDisabled || loading,
  107. checked: nativeControlChecked || false,
  108. };
  109. const showCheckedText = checkedText && nativeControlChecked && size !== 'small';
  110. const showUncheckedText = uncheckedText && !nativeControlChecked && size !== 'small';
  111. return (
  112. <div className={wrapperCls} style={style} onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave}>
  113. {
  114. loading
  115. ? (
  116. <Spin
  117. wrapperClassName={cssClasses.LOADING_SPIN}
  118. size={size === 'default' ? 'middle' : size}
  119. />
  120. )
  121. : <div className={cssClasses.KNOB} />
  122. }
  123. {showCheckedText ? (
  124. <div className={cssClasses.CHECKED_TEXT}>
  125. {checkedText}
  126. </div>
  127. ) : null}
  128. {showUncheckedText ? (
  129. <div className={cssClasses.UNCHECKED_TEXT}>
  130. {uncheckedText}
  131. </div>
  132. ) : null}
  133. <input
  134. {...switchProps}
  135. ref={this.switchRef}
  136. onChange={e => this.foundation.handleChange(e.target.checked, e)}
  137. />
  138. </div>
  139. );
  140. }
  141. }
  142. export default Switch;