radioInner.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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';
  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. addonId?: string;
  23. extraId?: string;
  24. 'aria-label'?: React.AriaAttributes['aria-label'];
  25. }
  26. interface RadioInnerState {
  27. checked?: boolean;
  28. }
  29. class RadioInner extends BaseComponent<RadioInnerProps, RadioInnerState> {
  30. static contextType = Context;
  31. static propTypes = {
  32. checked: PropTypes.bool,
  33. disabled: PropTypes.bool,
  34. isButtonRadio: PropTypes.bool,
  35. onChange: PropTypes.func,
  36. mode: PropTypes.oneOf(['advanced', '']),
  37. 'aria-label': PropTypes.string,
  38. };
  39. static defaultProps = {
  40. onChange: noop,
  41. isButtonRadio: false
  42. };
  43. inputEntity!: HTMLInputElement;
  44. foundation: RadioInnerFoundation;
  45. constructor(props: RadioInnerProps) {
  46. super(props);
  47. this.state = {
  48. checked: false
  49. };
  50. this.foundation = new RadioInnerFoundation(this.adapter);
  51. this.onChange = this.onChange.bind(this);
  52. }
  53. get adapter(): RadioInnerAdapter {
  54. return {
  55. ...super.adapter,
  56. setNativeControlChecked: (checked: boolean) => {
  57. this.setState({ checked });
  58. },
  59. notifyChange: (e: RadioChangeEvent) => {
  60. this.props.onChange(e);
  61. }
  62. };
  63. }
  64. componentDidMount() {
  65. this.foundation.init();
  66. }
  67. componentDidUpdate(prevProps: RadioInnerProps) {
  68. if (prevProps.checked !== this.props.checked) {
  69. this.foundation.setChecked(this.props.checked);
  70. }
  71. }
  72. componentWillUnmount() {
  73. this.foundation.destroy();
  74. }
  75. blur() {
  76. this.inputEntity.blur();
  77. }
  78. focus() {
  79. this.inputEntity.focus();
  80. }
  81. onChange(e: React.ChangeEvent<HTMLInputElement>) {
  82. this.foundation.handleChange(e);
  83. }
  84. render() {
  85. const { disabled, mode, autoFocus, name, isButtonRadio, isPureCardRadioGroup, addonId, extraId, 'aria-label': ariaLabel } = this.props;
  86. const { checked } = this.state;
  87. const prefix = this.props.prefixCls || css.PREFIX;
  88. const wrapper = classnames({
  89. [`${prefix}-inner`]: true,
  90. [`${prefix}-inner-checked`]: Boolean(checked),
  91. [`${prefix}-inner-buttonRadio`]: isButtonRadio,
  92. [`${prefix}-inner-pureCardRadio`]: isPureCardRadioGroup,
  93. });
  94. const inner = classnames({
  95. [`${prefix}-inner-display`]: !isButtonRadio,
  96. });
  97. return (
  98. <span className={wrapper}>
  99. <input
  100. ref={ref => {
  101. this.inputEntity = ref;
  102. }}
  103. // eslint-disable-next-line jsx-a11y/no-autofocus
  104. autoFocus={autoFocus}
  105. type={mode === 'advanced' ? 'checkbox' : 'radio'}
  106. checked={Boolean(checked)}
  107. disabled={disabled}
  108. onChange={this.onChange}
  109. name={name}
  110. aria-label={ariaLabel}
  111. aria-labelledby={addonId}
  112. aria-describedby={extraId}
  113. />
  114. <span className={inner}>{checked ? <IconRadio /> : null}</span>
  115. </span>
  116. );
  117. }
  118. }
  119. export default RadioInner;