radioInner.tsx 4.6 KB

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