checkbox.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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';
  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. id,
  113. } = this.props;
  114. const { checked } = this.state;
  115. const props: Record<string, any> = {
  116. checked,
  117. disabled,
  118. };
  119. if (this.isInGroup()) {
  120. if (this.context.checkboxGroup.value) {
  121. const realChecked = (this.context.checkboxGroup.value || []).includes(value);
  122. props.checked = realChecked;
  123. }
  124. if (this.context.checkboxGroup.disabled) {
  125. props.disabled = this.context.checkboxGroup.disabled || this.props.disabled;
  126. }
  127. const { isCardType, isPureCardType } = this.context.checkboxGroup;
  128. props.isCardType = isCardType;
  129. props.isPureCardType = isPureCardType;
  130. }
  131. const prefix = prefixCls || css.PREFIX;
  132. const wrapper = classnames(prefix, {
  133. [`${prefix}-disabled`]: props.disabled,
  134. [`${prefix}-indeterminate`]: indeterminate,
  135. [`${prefix}-checked`]: props.checked,
  136. [`${prefix}-unChecked`]: !props.checked,
  137. [`${prefix}-cardType`]: props.isCardType,
  138. [`${prefix}-cardType_disabled`]: props.disabled && props.isCardType,
  139. [`${prefix}-cardType_unDisabled`]: !(props.disabled && props.isCardType),
  140. [`${prefix}-cardType_checked`]: props.isCardType && props.checked && !props.disabled,
  141. [`${prefix}-cardType_checked_disabled`]: props.isCardType && props.checked && props.disabled,
  142. [className]: Boolean(className),
  143. });
  144. const extraCls = classnames(`${prefix}-extra`, {
  145. [`${prefix}-cardType_extra_noChildren`]: props.isCardType && !children,
  146. });
  147. const name = this.isInGroup() && this.context.checkboxGroup.name;
  148. const renderContent = () => (
  149. <>
  150. {children ? <span className={`${prefix}-addon`}>{children}</span> : null}
  151. {extra ? <div className={extraCls}>{extra}</div> : null}
  152. </>
  153. );
  154. return (
  155. <span
  156. style={style}
  157. className={wrapper}
  158. id={id}
  159. onMouseEnter={onMouseEnter}
  160. onMouseLeave={onMouseLeave}
  161. onClick={this.handleChange}
  162. >
  163. <CheckboxInner
  164. {...this.props}
  165. {...props}
  166. name={name}
  167. isPureCardType={props.isPureCardType}
  168. ref={ref => {
  169. this.checkboxEntity = ref;
  170. }}
  171. />
  172. {
  173. props.isCardType ?
  174. <div>{renderContent()}</div> :
  175. renderContent()
  176. }
  177. </span>
  178. );
  179. }
  180. }
  181. export default Checkbox;