radio.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /* eslint-disable prefer-destructuring */
  2. import React from 'react';
  3. import PropTypes from 'prop-types';
  4. import cls from 'classnames';
  5. import { noop } from 'lodash';
  6. import RadioFoundation, { RadioAdapter } from '@douyinfe/semi-foundation/radio/radioFoundation';
  7. import { RadioChangeEvent } from '@douyinfe/semi-foundation/radio/radioInnerFoundation';
  8. import { strings, radioClasses as css } from '@douyinfe/semi-foundation/radio/constants';
  9. import { getUuidShort } from '@douyinfe/semi-foundation/utils/uuid';
  10. import '@douyinfe/semi-foundation/radio/radio.scss';
  11. import BaseComponent from '../_base/baseComponent';
  12. import RadioInner from './radioInner';
  13. import Context, { RadioContextValue, RadioMode } from './context';
  14. export type RadioDisplayMode = 'vertical' | '';
  15. export type RadioType =
  16. typeof strings.TYPE_DEFAULT |
  17. typeof strings.TYPE_BUTTON |
  18. typeof strings.TYPE_CARD |
  19. typeof strings.TYPE_PURECARD;
  20. export type RadioProps = {
  21. autoFocus?: boolean;
  22. checked?: boolean;
  23. children?: React.ReactNode | undefined;
  24. defaultChecked?: boolean;
  25. value?: string | number;
  26. disabled?: boolean;
  27. prefixCls?: string;
  28. displayMode?: RadioDisplayMode;
  29. onChange?: (e: RadioChangeEvent) => void;
  30. onMouseEnter?: (e: React.MouseEvent<HTMLLabelElement>) => void;
  31. onMouseLeave?: (e: React.MouseEvent<HTMLLabelElement>) => void;
  32. mode?: RadioMode;
  33. extra?: React.ReactNode;
  34. style?: React.CSSProperties;
  35. className?: string;
  36. addonStyle?: React.CSSProperties;
  37. addonClassName?: string;
  38. type?: RadioType;
  39. 'aria-label'?: React.AriaAttributes['aria-label'];
  40. addonId?: string;
  41. extraId?: string;
  42. name?: string;
  43. preventScroll?: boolean;
  44. };
  45. export interface RadioState {
  46. hover?: boolean;
  47. addonId?: string;
  48. extraId?: string;
  49. focusVisible?: boolean;
  50. }
  51. export { RadioChangeEvent };
  52. class Radio extends BaseComponent<RadioProps, RadioState> {
  53. static contextType = Context;
  54. static propTypes = {
  55. autoFocus: PropTypes.bool,
  56. checked: PropTypes.bool,
  57. defaultChecked: PropTypes.bool,
  58. value: PropTypes.any, // Compare according to value to determine whether to select
  59. style: PropTypes.object,
  60. className: PropTypes.string,
  61. disabled: PropTypes.bool,
  62. prefixCls: PropTypes.string,
  63. displayMode: PropTypes.oneOf<RadioDisplayMode>(['vertical', '']),
  64. onChange: PropTypes.func,
  65. onMouseEnter: PropTypes.func,
  66. onMouseLeave: PropTypes.func,
  67. mode: PropTypes.oneOf(strings.MODE),
  68. extra: PropTypes.node, // extra info
  69. addonStyle: PropTypes.object,
  70. addonClassName: PropTypes.string,
  71. type: PropTypes.oneOf([strings.TYPE_DEFAULT, strings.TYPE_BUTTON, strings.TYPE_CARD, strings.TYPE_PURECARD]), // Button style type
  72. 'aria-label': PropTypes.string,
  73. preventScroll: PropTypes.bool,
  74. };
  75. static defaultProps: Partial<RadioProps> = {
  76. autoFocus: false,
  77. defaultChecked: false,
  78. value: undefined as undefined,
  79. style: undefined as undefined,
  80. onMouseEnter: noop,
  81. onMouseLeave: noop,
  82. mode: '',
  83. type: 'default'
  84. };
  85. radioEntity: RadioInner;
  86. context!: RadioContextValue;
  87. foundation: RadioFoundation;
  88. addonId: string;
  89. extraId: string;
  90. constructor(props: RadioProps) {
  91. super(props);
  92. this.state = {
  93. hover: false,
  94. addonId: props.addonId,
  95. extraId: props.extraId,
  96. };
  97. this.foundation = new RadioFoundation(this.adapter);
  98. this.radioEntity = null;
  99. }
  100. get adapter(): RadioAdapter {
  101. return {
  102. ...super.adapter,
  103. setHover: (hover: boolean) => {
  104. this.setState({ hover });
  105. },
  106. setAddonId: () => {
  107. this.setState({ addonId: getUuidShort({ prefix: 'addon' }) });
  108. },
  109. setExtraId: () => {
  110. this.setState({ extraId: getUuidShort({ prefix: 'extra' }) });
  111. },
  112. setFocusVisible: (focusVisible: boolean): void => {
  113. this.setState({ focusVisible });
  114. },
  115. };
  116. }
  117. isInGroup() {
  118. // eslint-disable-next-line react/destructuring-assignment
  119. return this.context && this.context.radioGroup;
  120. }
  121. focus() {
  122. this.radioEntity.focus();
  123. }
  124. blur() {
  125. this.radioEntity.blur();
  126. }
  127. onChange = (e: RadioChangeEvent) => {
  128. const { onChange } = this.props;
  129. if (this.isInGroup()) {
  130. const { radioGroup } = this.context;
  131. radioGroup.onChange && radioGroup.onChange(e);
  132. }
  133. onChange && onChange(e);
  134. };
  135. handleMouseEnter = (e: React.MouseEvent<HTMLLabelElement>) => {
  136. this.props.onMouseEnter(e);
  137. this.foundation.setHover(true);
  138. };
  139. handleMouseLeave = (e: React.MouseEvent<HTMLLabelElement>) => {
  140. this.props.onMouseLeave(e);
  141. this.foundation.setHover(false);
  142. };
  143. handleFocusVisible = (event: React.FocusEvent) => {
  144. this.foundation.handleFocusVisible(event);
  145. }
  146. handleBlur = (event: React.FocusEvent) => {
  147. this.foundation.handleBlur();
  148. }
  149. render() {
  150. const {
  151. addonClassName,
  152. addonStyle,
  153. checked,
  154. disabled,
  155. style,
  156. className,
  157. prefixCls,
  158. displayMode,
  159. children,
  160. extra,
  161. mode,
  162. type,
  163. value: propValue,
  164. name
  165. } = this.props;
  166. let realChecked,
  167. isDisabled,
  168. realMode,
  169. isButtonRadioGroup,
  170. isCardRadioGroup,
  171. isPureCardRadioGroup,
  172. isButtonRadioComponent,
  173. buttonSize,
  174. realPrefixCls;
  175. const { hover: isHover, addonId, extraId, focusVisible } = this.state;
  176. let props = {};
  177. if (this.isInGroup()) {
  178. realChecked = this.context.radioGroup.value === propValue;
  179. isDisabled = disabled || this.context.radioGroup.disabled;
  180. realMode = this.context.mode;
  181. isButtonRadioGroup = this.context.radioGroup.isButtonRadio;
  182. isCardRadioGroup = this.context.radioGroup.isCardRadio;
  183. isPureCardRadioGroup = this.context.radioGroup.isPureCardRadio;
  184. buttonSize = this.context.radioGroup.buttonSize;
  185. realPrefixCls = prefixCls || this.context.radioGroup.prefixCls;
  186. props = { checked: realChecked, disabled: isDisabled };
  187. } else {
  188. realChecked = checked;
  189. isDisabled = disabled;
  190. realMode = mode;
  191. isButtonRadioComponent = type === 'button';
  192. realPrefixCls = prefixCls;
  193. }
  194. const isButtonRadio = typeof isButtonRadioGroup === 'undefined' ? isButtonRadioComponent : isButtonRadioGroup;
  195. const prefix = realPrefixCls || css.PREFIX;
  196. const focusOuter = isCardRadioGroup || isPureCardRadioGroup || isButtonRadio;
  197. const wrapper = cls(prefix, {
  198. [`${prefix}-disabled`]: isDisabled,
  199. [`${prefix}-checked`]: realChecked,
  200. [`${prefix}-${displayMode}`]: Boolean(displayMode),
  201. [`${prefix}-buttonRadioComponent`]: isButtonRadioComponent,
  202. [`${prefix}-buttonRadioGroup`]: isButtonRadioGroup,
  203. [`${prefix}-buttonRadioGroup-${buttonSize}`]: isButtonRadioGroup && buttonSize,
  204. [`${prefix}-cardRadioGroup`]: isCardRadioGroup,
  205. [`${prefix}-cardRadioGroup_disabled`]: isDisabled && isCardRadioGroup,
  206. [`${prefix}-cardRadioGroup_checked`]: isCardRadioGroup && realChecked && !isDisabled,
  207. [`${prefix}-cardRadioGroup_checked_disabled`]: isCardRadioGroup && realChecked && isDisabled,
  208. [`${prefix}-cardRadioGroup_hover`]: isCardRadioGroup && !realChecked && isHover && !isDisabled,
  209. [className]: Boolean(className),
  210. [`${prefix}-focus`]: focusVisible && (isCardRadioGroup || isPureCardRadioGroup),
  211. });
  212. const groupName = this.isInGroup() && this.context.radioGroup.name;
  213. const addonCls = cls({
  214. [`${prefix}-addon`]: !isButtonRadio,
  215. [`${prefix}-addon-buttonRadio`]: isButtonRadio,
  216. [`${prefix}-addon-buttonRadio-checked`]: isButtonRadio && realChecked,
  217. [`${prefix}-addon-buttonRadio-disabled`]: isButtonRadio && isDisabled,
  218. [`${prefix}-addon-buttonRadio-hover`]: isButtonRadio && !realChecked && !isDisabled && isHover,
  219. [`${prefix}-addon-buttonRadio-${buttonSize}`]: isButtonRadio && buttonSize,
  220. [`${prefix}-focus`]: focusVisible && isButtonRadio,
  221. }, addonClassName);
  222. const renderContent = () => (
  223. <>
  224. {children ? (
  225. <span className={addonCls} style={addonStyle} id={addonId} x-semi-prop="children">
  226. {children}
  227. </span>
  228. ) : null}
  229. {extra && !isButtonRadio ? (
  230. <div className={`${prefix}-extra`} id={extraId} x-semi-prop="extra">
  231. {extra}
  232. </div>
  233. ) : null}
  234. </>
  235. );
  236. return (
  237. <label
  238. style={style}
  239. className={wrapper}
  240. onMouseEnter={this.handleMouseEnter}
  241. onMouseLeave={this.handleMouseLeave}
  242. >
  243. <RadioInner
  244. {...this.props}
  245. {...props}
  246. mode={realMode}
  247. name={name ?? groupName}
  248. isButtonRadio={isButtonRadio}
  249. isPureCardRadioGroup={isPureCardRadioGroup}
  250. onChange={this.onChange}
  251. ref={(ref: RadioInner) => {
  252. this.radioEntity = ref;
  253. }}
  254. addonId={children && addonId}
  255. extraId={extra && extraId}
  256. focusInner={focusVisible && !focusOuter}
  257. onInputFocus={this.handleFocusVisible}
  258. onInputBlur={this.handleBlur}
  259. />
  260. {
  261. isCardRadioGroup ?
  262. <div className={`${prefix}-isCardRadioGroup_content`}>{renderContent()}</div> :
  263. renderContent()
  264. }
  265. </label>
  266. );
  267. }
  268. }
  269. export default Radio;