1
0

index.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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';
  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. focusVisible: boolean;
  35. }
  36. class Switch extends BaseComponent<SwitchProps, SwitchState> {
  37. static propTypes = {
  38. 'aria-label': PropTypes.string,
  39. 'aria-labelledby': PropTypes.string,
  40. 'aria-invalid': PropTypes.bool,
  41. 'aria-errormessage': PropTypes.string,
  42. 'aria-describedby': PropTypes.string,
  43. className: PropTypes.string,
  44. checked: PropTypes.bool,
  45. checkedText: PropTypes.node,
  46. defaultChecked: PropTypes.bool,
  47. disabled: PropTypes.bool,
  48. loading: PropTypes.bool,
  49. onChange: PropTypes.func,
  50. onMouseEnter: PropTypes.func,
  51. onMouseLeave: PropTypes.func,
  52. style: PropTypes.object,
  53. size: PropTypes.oneOf<SwitchProps['size']>(strings.SIZE_MAP),
  54. uncheckedText: PropTypes.node,
  55. id: PropTypes.string,
  56. };
  57. static defaultProps: Partial<SwitchProps> = {
  58. disabled: false,
  59. className: '',
  60. onChange: noop,
  61. loading: false,
  62. onMouseEnter: noop,
  63. onMouseLeave: noop,
  64. size: 'default',
  65. };
  66. private switchRef: React.RefObject<HTMLInputElement>;
  67. constructor(props: SwitchProps) {
  68. super(props);
  69. this.state = {
  70. nativeControlChecked: false,
  71. nativeControlDisabled: false,
  72. focusVisible: false
  73. };
  74. this.switchRef = React.createRef();
  75. this.foundation = new SwitchFoudation(this.adapter);
  76. }
  77. componentDidMount() {
  78. this.foundation.init();
  79. }
  80. componentDidUpdate(prevProps: SwitchProps) {
  81. if (this.props.checked !== prevProps.checked) {
  82. this.foundation.setChecked(this.props.checked);
  83. }
  84. if (this.props.disabled !== prevProps.disabled) {
  85. this.foundation.setDisabled(this.props.disabled);
  86. }
  87. }
  88. componentWillUnmount() {
  89. this.foundation.destroy();
  90. }
  91. get adapter(): SwitchAdapter<SwitchProps, SwitchState> {
  92. return {
  93. ...super.adapter,
  94. setNativeControlChecked: (nativeControlChecked: boolean): void => {
  95. this.setState({ nativeControlChecked });
  96. },
  97. setNativeControlDisabled: (nativeControlDisabled: boolean): void => {
  98. this.setState({ nativeControlDisabled });
  99. },
  100. setFocusVisible: (focusVisible: boolean): void => {
  101. this.setState({ focusVisible });
  102. },
  103. notifyChange: (checked: boolean, e: React.ChangeEvent<HTMLInputElement>): void => {
  104. this.props.onChange(checked, e);
  105. },
  106. };
  107. }
  108. handleFocusVisible = (event: React.FocusEvent) => {
  109. this.foundation.handleFocusVisible(event);
  110. }
  111. handleBlur = (event: React.FocusEvent) => {
  112. this.foundation.handleBlur();
  113. }
  114. render() {
  115. const { nativeControlChecked, nativeControlDisabled, focusVisible } = this.state;
  116. const { className, style, onMouseEnter, onMouseLeave, size, checkedText, uncheckedText, loading, id } = this.props;
  117. const wrapperCls = cls(className, {
  118. [cssClasses.PREFIX]: true,
  119. [cssClasses.CHECKED]: nativeControlChecked,
  120. [cssClasses.DISABLED]: nativeControlDisabled,
  121. [cssClasses.LARGE]: size === 'large',
  122. [cssClasses.SMALL]: size === 'small',
  123. [cssClasses.LOADING]: loading,
  124. [cssClasses.FOCUS]: focusVisible,
  125. });
  126. const switchProps = {
  127. type: 'checkbox',
  128. className: cssClasses.NATIVE_CONTROL,
  129. disabled: nativeControlDisabled || loading,
  130. checked: nativeControlChecked || false,
  131. };
  132. const showCheckedText = checkedText && nativeControlChecked && size !== 'small';
  133. const showUncheckedText = uncheckedText && !nativeControlChecked && size !== 'small';
  134. return (
  135. <div className={wrapperCls} style={style} onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave}>
  136. {loading ? (
  137. <Spin wrapperClassName={cssClasses.LOADING_SPIN} size={size === 'default' ? 'middle' : size} />
  138. ) : (
  139. <div className={cssClasses.KNOB} aria-hidden={true} />
  140. )}
  141. {showCheckedText ? (
  142. <div className={cssClasses.CHECKED_TEXT} x-semi-prop="checkedText">
  143. {checkedText}
  144. </div>
  145. ) : null}
  146. {showUncheckedText ? (
  147. <div className={cssClasses.UNCHECKED_TEXT} x-semi-prop="uncheckedText">
  148. {uncheckedText}
  149. </div>
  150. ) : null}
  151. <input
  152. {...switchProps}
  153. ref={this.switchRef}
  154. id={id}
  155. role="switch"
  156. aria-checked={nativeControlChecked}
  157. aria-invalid={this.props['aria-invalid']}
  158. aria-errormessage={this.props['aria-errormessage']}
  159. aria-label={this.props['aria-label']}
  160. aria-labelledby={this.props['aria-labelledby']}
  161. aria-describedby={this.props['aria-describedby']}
  162. aria-disabled={this.props['disabled']}
  163. onChange={e => this.foundation.handleChange(e.target.checked, e)}
  164. onFocus={e => this.handleFocusVisible(e)}
  165. onBlur={e => this.handleBlur(e)}
  166. />
  167. </div>
  168. );
  169. }
  170. }
  171. export default Switch;