1
0

radioInner.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. constructor(props: RadioInnerProps) {
  41. super(props);
  42. this.state = {
  43. checked: false
  44. };
  45. this.foundation = new RadioInnerFoundation(this.adapter);
  46. this.onChange = this.onChange.bind(this);
  47. }
  48. get adapter(): RadioInnerAdapter {
  49. return {
  50. ...super.adapter,
  51. setNativeControlChecked: (checked: boolean) => {
  52. this.setState({ checked });
  53. },
  54. notifyChange: (e: RadioChangeEvent) => {
  55. this.props.onChange(e);
  56. }
  57. };
  58. }
  59. componentDidMount() {
  60. this.foundation.init();
  61. }
  62. componentDidUpdate(prevProps: RadioInnerProps) {
  63. if (prevProps.checked !== this.props.checked) {
  64. this.foundation.setChecked(this.props.checked);
  65. }
  66. }
  67. componentWillUnmount() {
  68. this.foundation.destroy();
  69. }
  70. blur() {
  71. this.inputEntity.blur();
  72. }
  73. focus() {
  74. this.inputEntity.focus();
  75. }
  76. onChange(e: React.ChangeEvent<HTMLInputElement>) {
  77. this.foundation.handleChange(e);
  78. }
  79. render() {
  80. const { disabled, mode, autoFocus, name, isButtonRadio, isPureCardRadioGroup } = this.props;
  81. const { checked } = this.state;
  82. const prefix = this.props.prefixCls || css.PREFIX;
  83. const wrapper = classnames({
  84. [`${prefix}-inner`]: true,
  85. [`${prefix}-inner-checked`]: Boolean(checked),
  86. [`${prefix}-inner-buttonRadio`]: isButtonRadio,
  87. [`${prefix}-inner-pureCardRadio`]: isPureCardRadioGroup,
  88. });
  89. const inner = classnames({
  90. [`${prefix}-inner-display`]: !isButtonRadio,
  91. });
  92. return (
  93. <span className={wrapper}>
  94. <input
  95. ref={ref => {
  96. this.inputEntity = ref;
  97. }}
  98. autoFocus={autoFocus}
  99. type={mode === 'advanced' ? 'checkbox' : 'radio'}
  100. checked={Boolean(checked)}
  101. disabled={disabled}
  102. onChange={this.onChange}
  103. name={name}
  104. />
  105. <span className={inner}>{checked ? <IconRadio /> : null}</span>
  106. </span>
  107. );
  108. }
  109. }
  110. export default RadioInner;