checkbox.tsx 7.4 KB

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