checkboxGroup.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import classnames from 'classnames';
  4. import { checkboxGroupClasses as css, strings } from '@douyinfe/semi-foundation/checkbox/constants';
  5. import CheckboxGroupFoundation, { CheckboxGroupAdapter } from '@douyinfe/semi-foundation/checkbox/checkboxGroupFoundation';
  6. import BaseComponent from '../_base/baseComponent';
  7. import { Context } from './context';
  8. import { isEqual } from 'lodash';
  9. import Checkbox, { CheckboxEvent } from './checkbox';
  10. export type CheckboxDirection = 'horizontal' | 'vertical';
  11. export type CheckboxType = 'default' | 'card' | 'pureCard';
  12. export type CheckboxGroupProps = {
  13. 'aria-describedby'?: React.AriaAttributes['aria-describedby'];
  14. 'aria-errormessage'?: React.AriaAttributes['aria-errormessage'];
  15. 'aria-invalid'?: React.AriaAttributes['aria-invalid'];
  16. 'aria-labelledby'?: React.AriaAttributes['aria-labelledby'];
  17. 'aria-required'?: React.AriaAttributes['aria-required'];
  18. defaultValue?: any[];
  19. disabled?: boolean;
  20. name?: string;
  21. options?: any[];
  22. value?: any[];
  23. onChange?: (value: any[]) => void;
  24. children?: React.ReactNode;
  25. prefixCls?: string;
  26. direction?: CheckboxDirection;
  27. style?: React.CSSProperties;
  28. className?: string;
  29. type?: CheckboxType;
  30. id?: string;
  31. 'aria-label'?: React.AriaAttributes['aria-label']
  32. };
  33. export type CheckboxGroupState = {
  34. value?: any[]
  35. };
  36. class CheckboxGroup extends BaseComponent<CheckboxGroupProps, CheckboxGroupState> {
  37. static propTypes = {
  38. 'aria-describedby': PropTypes.string,
  39. 'aria-errormessage': PropTypes.string,
  40. 'aria-invalid': PropTypes.bool,
  41. 'aria-labelledby': PropTypes.string,
  42. 'aria-required': PropTypes.bool,
  43. defaultValue: PropTypes.array,
  44. disabled: PropTypes.bool,
  45. name: PropTypes.string,
  46. options: PropTypes.array,
  47. value: PropTypes.array,
  48. onChange: PropTypes.func,
  49. children: PropTypes.node,
  50. prefixCls: PropTypes.string,
  51. direction: PropTypes.oneOf<CheckboxGroupProps['direction']>(strings.DIRECTION_SET),
  52. className: PropTypes.string,
  53. type: PropTypes.oneOf([strings.TYPE_DEFAULT, strings.TYPE_CARD, strings.TYPE_PURECARD]),
  54. style: PropTypes.object,
  55. };
  56. static defaultProps: Partial<CheckboxGroupProps> = {
  57. disabled: false,
  58. onChange: () => {},
  59. type: strings.TYPE_DEFAULT,
  60. defaultValue: [] as any,
  61. direction: strings.DEFAULT_DIRECTION,
  62. };
  63. get adapter(): CheckboxGroupAdapter {
  64. return {
  65. ...super.adapter,
  66. updateGroupValue: value => {
  67. this.setState({ value });
  68. },
  69. notifyChange: value => {
  70. this.props.onChange && this.props.onChange(value);
  71. },
  72. };
  73. }
  74. foundation: CheckboxGroupFoundation;
  75. constructor(props: CheckboxGroupProps) {
  76. super(props);
  77. this.state = {
  78. value: props.value || props.defaultValue,
  79. };
  80. this.foundation = new CheckboxGroupFoundation(this.adapter);
  81. this.onChange = this.onChange.bind(this);
  82. }
  83. componentDidMount() {
  84. this.foundation.init();
  85. }
  86. componentDidUpdate(prevProps: CheckboxGroupProps) {
  87. if (!isEqual(prevProps.value, this.props.value)) {
  88. this.foundation.handlePropValueChange(this.props.value);
  89. }
  90. }
  91. componentWillUnmount() {
  92. this.foundation.destroy();
  93. }
  94. onChange(evt: CheckboxEvent) {
  95. this.foundation.handleChange(evt);
  96. }
  97. render() {
  98. const { children, options, prefixCls, direction, className, id, style, type, disabled } = this.props;
  99. const isPureCardType = type === strings.TYPE_PURECARD;
  100. const isCardType = type === strings.TYPE_CARD || isPureCardType;
  101. const prefix = prefixCls || css.PREFIX;
  102. const prefixClsDisplay = classnames({
  103. [prefix as string]: true,
  104. [`${prefix }-wrapper`]: true,
  105. [`${prefix }-${ direction}`]: direction,
  106. [`${prefix}-${direction}-cardType`]: direction && isCardType,
  107. [`${prefix}-${direction}-pureCardType`]: direction && isPureCardType,
  108. }, className);
  109. const realValue = this.state.value.slice();
  110. let inner;
  111. if (options) {
  112. inner = (options || []).map((option, index) => {
  113. if (typeof option === 'string') {
  114. return (
  115. <Checkbox
  116. role="listitem"
  117. key={index}
  118. disabled={this.props.disabled}
  119. value={option}
  120. prefixCls={prefixCls}
  121. >
  122. {option}
  123. </Checkbox>
  124. );
  125. } else {
  126. return (
  127. <Checkbox
  128. role="listitem"
  129. key={index}
  130. disabled={option.disabled || this.props.disabled}
  131. value={option.value}
  132. prefixCls={prefixCls}
  133. extra={option.extra}
  134. className={option.className}
  135. style={option.style}
  136. onChange={option.onChange}
  137. >
  138. {option.label}
  139. </Checkbox>
  140. );
  141. }
  142. });
  143. } else if (children) {
  144. inner = (React.Children.toArray(children) as React.ReactElement[]).map((itm, index) => React.cloneElement(itm, { key: index, role: 'listitem' }));
  145. }
  146. return (
  147. <div
  148. id={id}
  149. role="list"
  150. aria-label={this.props['aria-label']}
  151. className={prefixClsDisplay}
  152. style={style}
  153. aria-labelledby={this.props['aria-labelledby']}
  154. aria-describedby={this.props['aria-describedby']}
  155. {...this.getDataAttr(this.props)}
  156. // aria-errormessage={this.props['aria-errormessage']}
  157. // aria-invalid={this.props['aria-invalid']}
  158. // aria-required={this.props['aria-required']}
  159. >
  160. <Context.Provider
  161. value={{
  162. checkboxGroup: {
  163. onChange: this.onChange,
  164. value: realValue,
  165. disabled: this.props.disabled,
  166. name: this.foundation.getFormatName(),
  167. isCardType,
  168. isPureCardType,
  169. },
  170. }}
  171. >
  172. {inner}
  173. </Context.Provider>
  174. </div>
  175. );
  176. }
  177. }
  178. export default CheckboxGroup;