checkbox.tsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. addonId: PropTypes.string, // A11y aria-labelledby
  46. extraId: PropTypes.string, // A11y aria-describedby
  47. index: PropTypes.number,
  48. };
  49. static defaultProps = {
  50. defaultChecked: false,
  51. indeterminate: false,
  52. onChange: noop,
  53. onMouseEnter: noop,
  54. onMouseLeave: noop,
  55. };
  56. checkboxEntity: CheckboxInner;
  57. get adapter(): CheckboxAdapter<CheckboxProps, CheckboxState> {
  58. return {
  59. ...super.adapter,
  60. setNativeControlChecked: checked => {
  61. this.setState({ checked });
  62. },
  63. notifyChange: cbContent => {
  64. const { onChange } = this.props;
  65. onChange && onChange(cbContent);
  66. },
  67. getIsInGroup: () => this.isInGroup(),
  68. getGroupValue: () => (this.context && this.context.checkboxGroup.value) || [],
  69. notifyGroupChange: cbContent => {
  70. this.context.checkboxGroup.onChange(cbContent);
  71. },
  72. getGroupDisabled: () => (this.context && this.context.checkboxGroup.disabled)
  73. };
  74. }
  75. foundation: CheckboxFoundation;
  76. constructor(props: CheckboxProps) {
  77. super(props);
  78. const checked = false;
  79. this.state = {
  80. checked: props.checked || props.defaultChecked || checked,
  81. };
  82. this.checkboxEntity = null;
  83. this.foundation = new CheckboxFoundation(this.adapter);
  84. }
  85. componentDidUpdate(prevProps: CheckboxProps) {
  86. if (this.props.checked !== prevProps.checked) {
  87. if (isUndefined(this.props.checked)) {
  88. this.foundation.setChecked(false);
  89. } else if (isBoolean(this.props.checked)) {
  90. this.foundation.setChecked(this.props.checked);
  91. }
  92. }
  93. }
  94. isInGroup() {
  95. return this.context && this.context.checkboxGroup;
  96. }
  97. focus() {
  98. this.checkboxEntity && this.checkboxEntity.focus();
  99. }
  100. blur() {
  101. this.checkboxEntity && this.checkboxEntity.blur();
  102. }
  103. handleChange: React.MouseEventHandler<HTMLSpanElement> = e => this.foundation.handleChange(e);
  104. handleEnterPress = (e: React.KeyboardEvent<HTMLSpanElement>) => this.foundation.handleEnterPress(e);
  105. render() {
  106. const {
  107. disabled,
  108. style,
  109. prefixCls,
  110. className,
  111. indeterminate,
  112. children,
  113. onMouseEnter,
  114. onMouseLeave,
  115. extra,
  116. value,
  117. addonId,
  118. extraId
  119. } = this.props;
  120. const { checked } = this.state;
  121. const props: Record<string, any> = {
  122. checked,
  123. disabled,
  124. };
  125. const inGroup = this.isInGroup();
  126. if (inGroup) {
  127. if (this.context.checkboxGroup.value) {
  128. const realChecked = (this.context.checkboxGroup.value || []).includes(value);
  129. props.checked = realChecked;
  130. }
  131. if (this.context.checkboxGroup.disabled) {
  132. props.disabled = this.context.checkboxGroup.disabled || this.props.disabled;
  133. }
  134. const { isCardType, isPureCardType } = this.context.checkboxGroup;
  135. props.isCardType = isCardType;
  136. props.isPureCardType = isPureCardType;
  137. }
  138. const prefix = prefixCls || css.PREFIX;
  139. const wrapper = classnames(prefix, {
  140. [`${prefix}-disabled`]: props.disabled,
  141. [`${prefix}-indeterminate`]: indeterminate,
  142. [`${prefix}-checked`]: props.checked,
  143. [`${prefix}-unChecked`]: !props.checked,
  144. [`${prefix}-cardType`]: props.isCardType,
  145. [`${prefix}-cardType_disabled`]: props.disabled && props.isCardType,
  146. [`${prefix}-cardType_unDisabled`]: !(props.disabled && props.isCardType),
  147. [`${prefix}-cardType_checked`]: props.isCardType && props.checked && !props.disabled,
  148. [className]: Boolean(className),
  149. });
  150. const extraCls = classnames(`${prefix}-extra`, {
  151. [`${prefix}-cardType_extra_noChildren`]: props.isCardType && !children,
  152. });
  153. const name = inGroup && this.context.checkboxGroup.name;
  154. const renderContent = () => (
  155. <>
  156. {children ? <span id={addonId} className={`${prefix}-addon`}>{children}</span> : null}
  157. {extra ? <div id={extraId} className={extraCls}>{extra}</div> : null}
  158. </>
  159. );
  160. return (
  161. <span
  162. role='checkbox'
  163. tabIndex={disabled ? -1 : 0}
  164. aria-disabled={props.checked}
  165. aria-checked={props.checked}
  166. aria-labelledby={addonId}
  167. aria-describedby={extraId}
  168. style={style}
  169. className={wrapper}
  170. onMouseEnter={onMouseEnter}
  171. onMouseLeave={onMouseLeave}
  172. onClick={this.handleChange}
  173. onKeyPress={this.handleEnterPress}
  174. >
  175. <CheckboxInner
  176. {...this.props}
  177. {...props}
  178. name={name}
  179. isPureCardType={props.isPureCardType}
  180. ref={ref => {
  181. this.checkboxEntity = ref;
  182. }}
  183. />
  184. {
  185. props.isCardType ?
  186. <div>{renderContent()}</div> :
  187. renderContent()
  188. }
  189. </span>
  190. );
  191. }
  192. }
  193. export default Checkbox;