1
0

checkboxInner.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* eslint-disable no-nested-ternary */
  2. import React, { PureComponent } from 'react';
  3. import classnames from 'classnames';
  4. import PropTypes from 'prop-types';
  5. import { noop } from 'lodash';
  6. import { checkboxClasses as css } from '@douyinfe/semi-foundation/checkbox/constants';
  7. import { Context } from './context';
  8. import { IconCheckboxTick, IconCheckboxIndeterminate } from '@douyinfe/semi-icons';
  9. export interface CheckboxInnerProps {
  10. 'aria-describedby'?: React.AriaAttributes['aria-describedby'];
  11. 'aria-errormessage'?: React.AriaAttributes['aria-errormessage'];
  12. 'aria-invalid'?: React.AriaAttributes['aria-invalid'];
  13. 'aria-labelledby'?: React.AriaAttributes['aria-labelledby'];
  14. 'aria-required'?: React.AriaAttributes['aria-required'];
  15. indeterminate?: boolean;
  16. checked?: boolean;
  17. disabled?: boolean;
  18. prefixCls?: string;
  19. name?: string;
  20. isPureCardType?: boolean;
  21. ref?: React.MutableRefObject<CheckboxInner> | ((ref: CheckboxInner) => void);
  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. class CheckboxInner extends PureComponent<CheckboxInnerProps> {
  31. static contextType = Context;
  32. static propTypes = {
  33. 'aria-describedby': PropTypes.string,
  34. 'aria-errormessage': PropTypes.string,
  35. 'aria-invalid': PropTypes.bool,
  36. 'aria-labelledby': PropTypes.string,
  37. 'aria-required': PropTypes.bool,
  38. checked: PropTypes.bool,
  39. disabled: PropTypes.bool,
  40. onChange: PropTypes.func,
  41. children: PropTypes.node,
  42. grouped: PropTypes.bool,
  43. value: PropTypes.any,
  44. isPureCardType: PropTypes.bool,
  45. addonId: PropTypes.string,
  46. extraId: PropTypes.string,
  47. focusInner: PropTypes.bool,
  48. onInputFocus: PropTypes.func,
  49. onInputBlur: PropTypes.func,
  50. preventScroll: PropTypes.bool,
  51. };
  52. static defaultProps = {
  53. onChange: noop,
  54. };
  55. inputEntity: HTMLInputElement;
  56. blur() {
  57. this.inputEntity.blur();
  58. }
  59. focus() {
  60. const { preventScroll } = this.props;
  61. this.inputEntity.focus({ preventScroll });
  62. }
  63. render() {
  64. const { indeterminate, checked, disabled, prefixCls, name, isPureCardType, addonId, extraId, focusInner, onInputFocus, onInputBlur } = this.props;
  65. const prefix = prefixCls || css.PREFIX;
  66. const wrapper = classnames(
  67. {
  68. [`${prefix}-inner`]: true,
  69. [`${prefix}-inner-checked`]: Boolean(checked),
  70. [`${prefix}-inner-pureCardType`]: isPureCardType,
  71. },
  72. css.WRAPPER
  73. );
  74. const inner = classnames({
  75. [`${prefix}-inner-display`]: true,
  76. [`${prefix}-focus`]: focusInner,
  77. [`${prefix}-focus-border`]: focusInner && !checked,
  78. });
  79. const icon = checked ? (
  80. <IconCheckboxTick />
  81. ) : indeterminate ? (
  82. <IconCheckboxIndeterminate />
  83. ) : null;
  84. const inputProps: React.InputHTMLAttributes<HTMLInputElement> = {
  85. type: "checkbox",
  86. 'aria-label': this.props['aria-label'],
  87. 'aria-disabled': disabled,
  88. 'aria-checked': checked,
  89. 'aria-labelledby': addonId,
  90. 'aria-describedby':extraId || this.props['aria-describedby'],
  91. 'aria-invalid': this.props['aria-invalid'],
  92. 'aria-errormessage': this.props['aria-errormessage'],
  93. 'aria-required': this.props['aria-required'],
  94. className: css.INPUT,
  95. onChange: noop,
  96. checked: checked,
  97. disabled: disabled,
  98. onFocus: onInputFocus,
  99. onBlur: onInputBlur,
  100. };
  101. name && (inputProps['name'] = name);
  102. return (
  103. <span className={wrapper}>
  104. <input
  105. {...inputProps}
  106. ref={ref => {
  107. this.inputEntity = ref;
  108. }}
  109. />
  110. <span className={inner}>{icon}</span>
  111. </span>
  112. );
  113. }
  114. }
  115. export default CheckboxInner;