checkboxInner.tsx 4.2 KB

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