index.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import React from 'react';
  2. import cls from 'classnames';
  3. import PropTypes from 'prop-types';
  4. import SwitchFoudation, { SwitchAdapter } from '@douyinfe/semi-foundation/switch/foundation';
  5. import { cssClasses, strings } from '@douyinfe/semi-foundation/switch/constants';
  6. import BaseComponent from '../_base/baseComponent';
  7. import '@douyinfe/semi-foundation/switch/switch.scss';
  8. import { noop } from 'lodash';
  9. import Spin from '../spin';
  10. export interface SwitchProps {
  11. 'aria-label'?: React.AriaAttributes['aria-label'];
  12. 'aria-describedby'?: React.AriaAttributes['aria-describedby'];
  13. 'aria-errormessage'?: React.AriaAttributes['aria-errormessage'];
  14. 'aria-invalid'?: React.AriaAttributes['aria-invalid'];
  15. 'aria-labelledby'?: React.AriaAttributes['aria-labelledby'];
  16. defaultChecked?: boolean;
  17. checked?: boolean;
  18. disabled?: boolean;
  19. onChange?: (checked: boolean, e: React.ChangeEvent<HTMLInputElement>) => void;
  20. loading?: boolean;
  21. className?: string;
  22. style?: React.CSSProperties;
  23. onMouseEnter?: (e: React.MouseEvent) => any;
  24. onMouseLeave?: (e: React.MouseEvent) => any;
  25. size?: 'large' | 'default' | 'small';
  26. checkedText?: React.ReactNode;
  27. uncheckedText?: React.ReactNode;
  28. id?: string
  29. }
  30. export interface SwitchState {
  31. nativeControlChecked: boolean;
  32. nativeControlDisabled: boolean;
  33. focusVisible: 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: props.defaultChecked || props.checked,
  70. nativeControlDisabled: false,
  71. focusVisible: false
  72. };
  73. this.switchRef = React.createRef();
  74. this.foundation = new SwitchFoudation(this.adapter);
  75. }
  76. componentDidMount() {
  77. this.foundation.init();
  78. }
  79. componentDidUpdate(prevProps: SwitchProps) {
  80. if (this.props.checked !== prevProps.checked) {
  81. this.foundation.setChecked(this.props.checked);
  82. }
  83. if (this.props.disabled !== prevProps.disabled) {
  84. this.foundation.setDisabled(this.props.disabled);
  85. }
  86. }
  87. componentWillUnmount() {
  88. this.foundation.destroy();
  89. }
  90. get adapter(): SwitchAdapter<SwitchProps, SwitchState> {
  91. return {
  92. ...super.adapter,
  93. setNativeControlChecked: (nativeControlChecked: boolean): void => {
  94. this.setState({ nativeControlChecked });
  95. },
  96. setNativeControlDisabled: (nativeControlDisabled: boolean): void => {
  97. this.setState({ nativeControlDisabled });
  98. },
  99. setFocusVisible: (focusVisible: boolean): void => {
  100. this.setState({ focusVisible });
  101. },
  102. notifyChange: (checked: boolean, e: React.ChangeEvent<HTMLInputElement>): void => {
  103. this.props.onChange(checked, e);
  104. },
  105. };
  106. }
  107. handleFocusVisible = (event: React.FocusEvent) => {
  108. this.foundation.handleFocusVisible(event);
  109. }
  110. handleBlur = (event: React.FocusEvent) => {
  111. this.foundation.handleBlur();
  112. }
  113. render() {
  114. const { nativeControlChecked, nativeControlDisabled, focusVisible } = this.state;
  115. const { className, style, onMouseEnter, onMouseLeave, size, checkedText, uncheckedText, loading, id, ...rest } = this.props;
  116. const wrapperCls = cls(className, {
  117. [cssClasses.PREFIX]: true,
  118. [cssClasses.CHECKED]: nativeControlChecked,
  119. [cssClasses.DISABLED]: nativeControlDisabled,
  120. [cssClasses.LARGE]: size === 'large',
  121. [cssClasses.SMALL]: size === 'small',
  122. [cssClasses.LOADING]: loading,
  123. [cssClasses.FOCUS]: focusVisible,
  124. });
  125. const switchProps = {
  126. type: 'checkbox',
  127. className: cssClasses.NATIVE_CONTROL,
  128. disabled: nativeControlDisabled || loading,
  129. checked: nativeControlChecked || false,
  130. };
  131. const showCheckedText = checkedText && nativeControlChecked && size !== 'small';
  132. const showUncheckedText = uncheckedText && !nativeControlChecked && size !== 'small';
  133. return (
  134. <div className={wrapperCls} style={style} onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave} {...this.getDataAttr(rest)}>
  135. {loading ? (
  136. <Spin wrapperClassName={cssClasses.LOADING_SPIN} size={size === 'default' ? 'middle' : size} />
  137. ) : (
  138. <div className={cssClasses.KNOB} aria-hidden={true} />
  139. )}
  140. {showCheckedText ? (
  141. <div className={cssClasses.CHECKED_TEXT} x-semi-prop="checkedText">
  142. {checkedText}
  143. </div>
  144. ) : null}
  145. {showUncheckedText ? (
  146. <div className={cssClasses.UNCHECKED_TEXT} x-semi-prop="uncheckedText">
  147. {uncheckedText}
  148. </div>
  149. ) : null}
  150. <input
  151. {...switchProps}
  152. ref={this.switchRef}
  153. id={id}
  154. role="switch"
  155. aria-checked={nativeControlChecked}
  156. aria-invalid={this.props['aria-invalid']}
  157. aria-errormessage={this.props['aria-errormessage']}
  158. aria-label={this.props['aria-label']}
  159. aria-labelledby={this.props['aria-labelledby']}
  160. aria-describedby={this.props['aria-describedby']}
  161. aria-disabled={this.props['disabled']}
  162. onChange={e => this.foundation.handleChange(e.target.checked, e)}
  163. onFocus={e => this.handleFocusVisible(e)}
  164. onBlur={e => this.handleBlur(e)}
  165. />
  166. </div>
  167. );
  168. }
  169. }
  170. export default Switch;