radioGroup.tsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import classnames from 'classnames';
  4. import { noop } from 'lodash';
  5. import { radioGroupClasses as css, strings } from '@douyinfe/semi-foundation/radio/constants';
  6. import RadioGroupFoundation, { RadioGroupAdapter } from '@douyinfe/semi-foundation/radio/radioGroupFoundation';
  7. import { RadioChangeEvent } from '@douyinfe/semi-foundation/radio/radioInnerFoundation';
  8. import BaseComponent from '../_base/baseComponent';
  9. import { ArrayElement } from '../_base/base';
  10. import Radio, { RadioType } from './radio';
  11. import Context, { RadioGroupButtonSize, RadioMode } from './context';
  12. export interface OptionItem {
  13. label?: React.ReactNode;
  14. value?: string | number;
  15. disabled?: boolean;
  16. extra?: React.ReactNode;
  17. style?: React.CSSProperties;
  18. className?: string
  19. }
  20. export type Options = string[] | Array<OptionItem>;
  21. export type RadioGroupProps = {
  22. defaultValue?: string | number;
  23. disabled?: boolean;
  24. name?: string;
  25. options?: Options;
  26. value?: string | number;
  27. onChange?: (event: RadioChangeEvent) => void;
  28. className?: string;
  29. children?: React.ReactNode;
  30. style?: React.CSSProperties;
  31. direction?: ArrayElement<typeof strings.DIRECTION_SET>;
  32. mode?: RadioMode;
  33. type?: RadioType;
  34. buttonSize?: RadioGroupButtonSize;
  35. prefixCls?: string;
  36. 'aria-label'?: React.AriaAttributes['aria-label'];
  37. 'aria-describedby'?: React.AriaAttributes['aria-describedby'];
  38. 'aria-errormessage'?: React.AriaAttributes['aria-errormessage'];
  39. 'aria-invalid'?: React.AriaAttributes['aria-invalid'];
  40. 'aria-labelledby'?: React.AriaAttributes['aria-labelledby'];
  41. 'aria-required'?: React.AriaAttributes['aria-required'];
  42. id?: string
  43. };
  44. export interface RadioGroupState {
  45. value?: any
  46. }
  47. class RadioGroup extends BaseComponent<RadioGroupProps, RadioGroupState> {
  48. static propTypes = {
  49. defaultValue: PropTypes.any,
  50. disabled: PropTypes.bool,
  51. name: PropTypes.string,
  52. options: PropTypes.array,
  53. buttonSize: PropTypes.oneOf(strings.BUTTON_SIZE),
  54. type: PropTypes.oneOf([strings.TYPE_DEFAULT, strings.TYPE_BUTTON, strings.TYPE_CARD, strings.TYPE_PURECARD]),
  55. value: PropTypes.any,
  56. onChange: PropTypes.func,
  57. children: PropTypes.node,
  58. prefixCls: PropTypes.string,
  59. className: PropTypes.string,
  60. style: PropTypes.object,
  61. direction: PropTypes.oneOf(strings.DIRECTION_SET),
  62. mode: PropTypes.oneOf(strings.MODE),
  63. 'aria-label': PropTypes.string,
  64. 'aria-describedby': PropTypes.string,
  65. 'aria-errormessage': PropTypes.string,
  66. 'aria-invalid': PropTypes.bool,
  67. 'aria-labelledby': PropTypes.string,
  68. 'aria-required': PropTypes.bool,
  69. id: PropTypes.string,
  70. };
  71. static defaultProps: Partial<RadioGroupProps> = {
  72. disabled: false,
  73. onChange: noop,
  74. direction: strings.DEFAULT_DIRECTION,
  75. mode: '',
  76. type: strings.TYPE_DEFAULT,
  77. buttonSize: 'middle'
  78. };
  79. foundation: RadioGroupFoundation;
  80. constructor(props: RadioGroupProps) {
  81. super(props);
  82. this.state = {
  83. value: props.value || props.defaultValue,
  84. };
  85. this.foundation = new RadioGroupFoundation(this.adapter);
  86. }
  87. componentDidMount() {
  88. this.foundation.init();
  89. }
  90. componentDidUpdate(prevProps: RadioGroupProps) {
  91. if (typeof prevProps.value === 'number'
  92. && isNaN(prevProps.value)
  93. && typeof this.props.value === 'number'
  94. && isNaN(this.props.value)
  95. ) {
  96. // `NaN === NaN` returns false, and this will fail the next if check
  97. // therefore triggering an infinite loop
  98. return;
  99. }
  100. if (prevProps.value !== this.props.value) {
  101. this.foundation.handlePropValueChange(this.props.value);
  102. }
  103. }
  104. componentWillUnmount() {
  105. this.foundation.destroy();
  106. }
  107. get adapter(): RadioGroupAdapter {
  108. return {
  109. ...super.adapter,
  110. setValue: (value: any) => {
  111. this.setState({ value });
  112. },
  113. getProps: () => this.props,
  114. isInProps: (name: string) => Boolean(name in this.props),
  115. notifyChange: (evt: RadioChangeEvent) => {
  116. this.props.onChange && this.props.onChange(evt);
  117. },
  118. };
  119. }
  120. onChange = (evt: RadioChangeEvent) => {
  121. this.foundation.handleChange(evt);
  122. };
  123. getFormatName = () => this.props.name || 'default';
  124. render() {
  125. const {
  126. children,
  127. options,
  128. mode,
  129. prefixCls,
  130. className,
  131. style,
  132. direction,
  133. type,
  134. buttonSize,
  135. id,
  136. } = this.props;
  137. const isButtonRadio = type === strings.TYPE_BUTTON;
  138. const isPureCardRadio = type === strings.TYPE_PURECARD;
  139. const isCardRadio = type === strings.TYPE_CARD || isPureCardRadio;
  140. const isDefaultRadio = type === strings.TYPE_DEFAULT;
  141. const prefix = prefixCls || css.PREFIX;
  142. const prefixClsDisplay = classnames(className, {
  143. [prefix]: true,
  144. [`${prefix}-wrapper`]: true,
  145. [`${prefix}-${direction}`]: direction && !isButtonRadio,
  146. [`${prefix}-${direction}-default`]: direction && isDefaultRadio,
  147. [`${prefix}-${direction}-card`]: direction && isCardRadio,
  148. [`${prefix}-buttonRadio`]: isButtonRadio,
  149. });
  150. const realValue = this.state.value;
  151. let inner;
  152. if (options) {
  153. inner = (options || []).map((option, index) => {
  154. if (typeof option === 'string') {
  155. return (
  156. <Radio
  157. key={index}
  158. disabled={this.props.disabled}
  159. value={option}
  160. >
  161. {option}
  162. </Radio>
  163. );
  164. } else {
  165. return (
  166. <Radio
  167. key={index}
  168. disabled={option.disabled || this.props.disabled}
  169. value={option.value}
  170. extra={option.extra}
  171. className={option.className}
  172. style={option.style}
  173. >
  174. {option.label}
  175. </Radio>
  176. );
  177. }
  178. });
  179. } else if (children) {
  180. inner = React.Children.map(children, (itm, index) => (React.isValidElement(itm) ?
  181. React.cloneElement(itm, { key: index }) :
  182. null));
  183. }
  184. return (
  185. <div
  186. className={prefixClsDisplay}
  187. style={style}
  188. id={id}
  189. aria-label={this.props['aria-label']}
  190. aria-invalid={this.props['aria-invalid']}
  191. aria-errormessage={this.props['aria-errormessage']}
  192. aria-labelledby={this.props['aria-labelledby']}
  193. aria-describedby={this.props['aria-describedby']}
  194. aria-required={this.props['aria-required']}
  195. >
  196. <Context.Provider
  197. value={{
  198. radioGroup: {
  199. onChange: this.onChange,
  200. value: realValue,
  201. disabled: this.props.disabled,
  202. name: this.getFormatName(),
  203. isButtonRadio,
  204. isCardRadio,
  205. isPureCardRadio,
  206. buttonSize,
  207. prefixCls
  208. },
  209. mode
  210. }}
  211. >
  212. {inner}
  213. </Context.Provider>
  214. </div>
  215. );
  216. }
  217. }
  218. export default RadioGroup;