radioInner.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import RadioInnerFoundation, { RadioChangeEvent, RadioInnerAdapter } from '@douyinfe/semi-foundation/radio/radioInnerFoundation';
  4. import BaseComponent, { BaseProps } from '../_base/baseComponent';
  5. import { radioClasses as css } from '@douyinfe/semi-foundation/radio/constants';
  6. import Context from './context';
  7. import classnames from 'classnames';
  8. import { IconRadio } from '@douyinfe/semi-icons';
  9. import { noop } from 'lodash-es';
  10. export type RadioInnerMode = 'advanced' | '';
  11. export interface RadioInnerProps extends BaseProps {
  12. checked?: boolean;
  13. disabled?: boolean;
  14. isButtonRadio?: boolean;
  15. onChange?: (e: RadioChangeEvent) => void;
  16. mode?: RadioInnerMode;
  17. autoFocus?: boolean;
  18. name?: string;
  19. prefixCls?: string;
  20. ref?: React.MutableRefObject<RadioInner> | ((instance: RadioInner) => void);
  21. isPureCardRadioGroup?: boolean;
  22. }
  23. interface RadioInnerState {
  24. checked?: boolean;
  25. }
  26. class RadioInner extends BaseComponent<RadioInnerProps, RadioInnerState> {
  27. static contextType = Context;
  28. static propTypes = {
  29. checked: PropTypes.bool,
  30. disabled: PropTypes.bool,
  31. isButtonRadio: PropTypes.bool,
  32. onChange: PropTypes.func,
  33. mode: PropTypes.oneOf(['advanced', '']),
  34. };
  35. static defaultProps = {
  36. onChange: noop,
  37. isButtonRadio: false
  38. };
  39. inputEntity!: HTMLInputElement;
  40. foundation: RadioInnerFoundation;
  41. constructor(props: RadioInnerProps) {
  42. super(props);
  43. this.state = {
  44. checked: false
  45. };
  46. this.foundation = new RadioInnerFoundation(this.adapter);
  47. this.onChange = this.onChange.bind(this);
  48. }
  49. get adapter(): RadioInnerAdapter {
  50. return {
  51. ...super.adapter,
  52. setNativeControlChecked: (checked: boolean) => {
  53. this.setState({ checked });
  54. },
  55. notifyChange: (e: RadioChangeEvent) => {
  56. this.props.onChange(e);
  57. }
  58. };
  59. }
  60. componentDidMount() {
  61. this.foundation.init();
  62. }
  63. componentDidUpdate(prevProps: RadioInnerProps) {
  64. if (prevProps.checked !== this.props.checked) {
  65. this.foundation.setChecked(this.props.checked);
  66. }
  67. }
  68. componentWillUnmount() {
  69. this.foundation.destroy();
  70. }
  71. blur() {
  72. this.inputEntity.blur();
  73. }
  74. focus() {
  75. this.inputEntity.focus();
  76. }
  77. onChange(e: React.ChangeEvent<HTMLInputElement>) {
  78. this.foundation.handleChange(e);
  79. }
  80. render() {
  81. const { disabled, mode, autoFocus, name, isButtonRadio, isPureCardRadioGroup } = this.props;
  82. const { checked } = this.state;
  83. const prefix = this.props.prefixCls || css.PREFIX;
  84. const wrapper = classnames({
  85. [`${prefix}-inner`]: true,
  86. [`${prefix}-inner-checked`]: Boolean(checked),
  87. [`${prefix}-inner-buttonRadio`]: isButtonRadio,
  88. [`${prefix}-inner-pureCardRadio`]: isPureCardRadioGroup,
  89. });
  90. const inner = classnames({
  91. [`${prefix}-inner-display`]: !isButtonRadio,
  92. });
  93. return (
  94. <span className={wrapper}>
  95. <input
  96. ref={ref => {
  97. this.inputEntity = ref;
  98. }}
  99. // eslint-disable-next-line jsx-a11y/no-autofocus
  100. autoFocus={autoFocus}
  101. type={mode === 'advanced' ? 'checkbox' : 'radio'}
  102. checked={Boolean(checked)}
  103. disabled={disabled}
  104. onChange={this.onChange}
  105. name={name}
  106. />
  107. <span className={inner}>{checked ? <IconRadio /> : null}</span>
  108. </span>
  109. );
  110. }
  111. }
  112. export default RadioInner;