checkbox.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* eslint-disable max-len */
  2. import React from 'react';
  3. import PropTypes from 'prop-types';
  4. import classnames from 'classnames';
  5. import { checkboxClasses as css } from '@douyinfe/semi-foundation/checkbox/constants';
  6. import CheckboxFoundation, { CheckboxAdapter, BasicCheckboxEvent, BasicTargetObject, BaseCheckboxProps } from '@douyinfe/semi-foundation/checkbox/checkboxFoundation';
  7. import CheckboxInner from './checkboxInner';
  8. import BaseComponent from '../_base/baseComponent';
  9. import '@douyinfe/semi-foundation/checkbox/checkbox.scss';
  10. import { Context } from './context';
  11. import { isUndefined, isBoolean, noop } from 'lodash-es';
  12. export type CheckboxEvent = BasicCheckboxEvent;
  13. export type TargetObject = BasicTargetObject;
  14. export interface CheckboxProps extends BaseCheckboxProps {
  15. onChange?: (e: CheckboxEvent) => any;
  16. // TODO, docs
  17. style?: React.CSSProperties;
  18. onMouseEnter?: React.MouseEventHandler<HTMLSpanElement>;
  19. onMouseLeave?: React.MouseEventHandler<HTMLSpanElement>;
  20. extra?: React.ReactNode;
  21. }
  22. interface CheckboxState {
  23. checked: boolean;
  24. }
  25. class Checkbox extends BaseComponent<CheckboxProps, CheckboxState> {
  26. static contextType = Context;
  27. static propTypes = {
  28. // Specifies whether it is currently selected
  29. checked: PropTypes.bool,
  30. // Initial check
  31. defaultChecked: PropTypes.bool,
  32. // Failure state
  33. disabled: PropTypes.bool,
  34. // Set indeterminate state, only responsible for style control
  35. indeterminate: PropTypes.bool,
  36. // Callback function when changing
  37. onChange: PropTypes.func,
  38. value: PropTypes.any,
  39. style: PropTypes.object,
  40. className: PropTypes.string,
  41. prefixCls: PropTypes.string,
  42. onMouseEnter: PropTypes.func,
  43. onMouseLeave: PropTypes.func,
  44. extra: PropTypes.node,
  45. };
  46. static defaultProps = {
  47. defaultChecked: false,
  48. indeterminate: false,
  49. onChange: noop,
  50. onMouseEnter: noop,
  51. onMouseLeave: noop,
  52. };
  53. checkboxEntity: CheckboxInner;
  54. get adapter(): CheckboxAdapter<CheckboxProps, CheckboxState> {
  55. return {
  56. ...super.adapter,
  57. setNativeControlChecked: checked => {
  58. this.setState({ checked });
  59. },
  60. notifyChange: cbContent => {
  61. const { onChange } = this.props;
  62. onChange && onChange(cbContent);
  63. },
  64. getIsInGroup: () => this.isInGroup(),
  65. getGroupValue: () => (this.context && this.context.checkboxGroup.value) || [],
  66. notifyGroupChange: cbContent => {
  67. this.context.checkboxGroup.onChange(cbContent);
  68. },
  69. getGroupDisabled: () => (this.context && this.context.checkboxGroup.disabled)
  70. };
  71. }
  72. constructor(props: CheckboxProps) {
  73. super(props);
  74. const checked = false;
  75. this.state = {
  76. checked: props.checked || props.defaultChecked || checked,
  77. };
  78. this.checkboxEntity = null;
  79. this.foundation = new CheckboxFoundation(this.adapter);
  80. }
  81. componentDidUpdate(prevProps: CheckboxProps) {
  82. if (this.props.checked !== prevProps.checked) {
  83. if (isUndefined(this.props.checked)) {
  84. this.foundation.setChecked(false);
  85. } else if (isBoolean(this.props.checked)) {
  86. this.foundation.setChecked(this.props.checked);
  87. }
  88. }
  89. }
  90. isInGroup() {
  91. return this.context && this.context.checkboxGroup;
  92. }
  93. focus() {
  94. this.checkboxEntity && this.checkboxEntity.focus();
  95. }
  96. blur() {
  97. this.checkboxEntity && this.checkboxEntity.blur();
  98. }
  99. handleChange: React.MouseEventHandler<HTMLSpanElement> = e => this.foundation.handleChange(e);
  100. render() {
  101. const {
  102. disabled,
  103. style,
  104. prefixCls,
  105. className,
  106. indeterminate,
  107. children,
  108. onMouseEnter,
  109. onMouseLeave,
  110. extra,
  111. value,
  112. } = this.props;
  113. const { checked } = this.state;
  114. const props: Record<string, any> = {
  115. checked,
  116. disabled,
  117. };
  118. if (this.isInGroup()) {
  119. if (this.context.checkboxGroup.value) {
  120. const realChecked = (this.context.checkboxGroup.value || []).includes(value);
  121. props.checked = realChecked;
  122. }
  123. if (this.context.checkboxGroup.disabled) {
  124. props.disabled = this.context.checkboxGroup.disabled || this.props.disabled;
  125. }
  126. const { isCardType, isPureCardType } = this.context.checkboxGroup;
  127. props.isCardType = isCardType;
  128. props.isPureCardType = isPureCardType;
  129. }
  130. const prefix = prefixCls || css.PREFIX;
  131. const wrapper = classnames(prefix, {
  132. [`${prefix}-disabled`]: props.disabled,
  133. [`${prefix}-indeterminate`]: indeterminate,
  134. [`${prefix}-checked`]: props.checked,
  135. [`${prefix}-unChecked`]: !props.checked,
  136. [`${prefix}-cardType`]: props.isCardType,
  137. [`${prefix}-cardType_disabled`]: props.disabled && props.isCardType,
  138. [`${prefix}-cardType_unDisabled`]: !(props.disabled && props.isCardType),
  139. [`${prefix}-cardType_checked`]: props.isCardType && props.checked && !props.disabled,
  140. [className]: Boolean(className),
  141. });
  142. const extraCls = classnames(`${prefix}-extra`, {
  143. [`${prefix}-cardType_extra_noChildren`]: props.isCardType && !children,
  144. });
  145. const name = this.isInGroup() && this.context.checkboxGroup.name;
  146. const renderContent = () => (
  147. <>
  148. {children ? <span className={`${prefix}-addon`}>{children}</span> : null}
  149. {extra ? <div className={extraCls}>{extra}</div> : null}
  150. </>
  151. );
  152. return (
  153. <span
  154. style={style}
  155. className={wrapper}
  156. onMouseEnter={onMouseEnter}
  157. onMouseLeave={onMouseLeave}
  158. onClick={this.handleChange}
  159. >
  160. <CheckboxInner
  161. {...this.props}
  162. {...props}
  163. name={name}
  164. isPureCardType={props.isPureCardType}
  165. ref={ref => {
  166. this.checkboxEntity = ref;
  167. }}
  168. />
  169. {
  170. props.isCardType ?
  171. <div>{renderContent()}</div> :
  172. renderContent()
  173. }
  174. </span>
  175. );
  176. }
  177. }
  178. export default Checkbox;