index.tsx 6.3 KB

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