checkboxInner.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. }
  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. };
  50. static defaultProps = {
  51. onChange: noop,
  52. };
  53. inputEntity: HTMLInputElement;
  54. blur() {
  55. this.inputEntity.blur();
  56. }
  57. focus() {
  58. this.inputEntity.focus();
  59. }
  60. render() {
  61. const { indeterminate, checked, disabled, prefixCls, name, isPureCardType, addonId, extraId, focusInner, onInputFocus, onInputBlur } = this.props;
  62. const prefix = prefixCls || css.PREFIX;
  63. const wrapper = classnames(
  64. {
  65. [`${prefix}-inner`]: true,
  66. [`${prefix}-inner-checked`]: Boolean(checked),
  67. [`${prefix}-inner-pureCardType`]: isPureCardType,
  68. },
  69. css.WRAPPER
  70. );
  71. const inner = classnames({
  72. [`${prefix}-inner-display`]: true,
  73. [`${prefix}-focus`]: focusInner,
  74. [`${prefix}-focus-border`]: focusInner && !checked,
  75. });
  76. const icon = checked ? (
  77. <IconCheckboxTick />
  78. ) : indeterminate ? (
  79. <IconCheckboxIndeterminate />
  80. ) : null;
  81. const inputProps: React.InputHTMLAttributes<HTMLInputElement> = {
  82. type: "checkbox",
  83. 'aria-label': this.props['aria-label'],
  84. 'aria-disabled': disabled,
  85. 'aria-checked': checked,
  86. 'aria-labelledby': addonId,
  87. 'aria-describedby':extraId || this.props['aria-describedby'],
  88. 'aria-invalid': this.props['aria-invalid'],
  89. 'aria-errormessage': this.props['aria-errormessage'],
  90. 'aria-required': this.props['aria-required'],
  91. className: css.INPUT,
  92. onChange: noop,
  93. checked: checked,
  94. disabled: disabled,
  95. onFocus: onInputFocus,
  96. onBlur: onInputBlur,
  97. };
  98. name && (inputProps['name'] = name);
  99. return (
  100. <span className={wrapper}>
  101. <input
  102. {...inputProps}
  103. ref={ref => {
  104. this.inputEntity = ref;
  105. }}
  106. />
  107. <span className={inner}>{icon}</span>
  108. </span>
  109. );
  110. }
  111. }
  112. export default CheckboxInner;