checkboxInner.tsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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-es';
  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. indeterminate?: boolean;
  11. checked?: boolean;
  12. disabled?: boolean;
  13. prefixCls?: string;
  14. name?: string;
  15. isPureCardType?: boolean;
  16. ref?: React.MutableRefObject<CheckboxInner> | ((ref: CheckboxInner) => void);
  17. addonId?: string;
  18. extraId?: string;
  19. }
  20. class CheckboxInner extends PureComponent<CheckboxInnerProps> {
  21. static contextType = Context;
  22. static propTypes = {
  23. checked: PropTypes.bool,
  24. disabled: PropTypes.bool,
  25. onChange: PropTypes.func,
  26. children: PropTypes.node,
  27. grouped: PropTypes.bool,
  28. value: PropTypes.any,
  29. isPureCardType: PropTypes.bool,
  30. addonId: PropTypes.string,
  31. extraId: PropTypes.string,
  32. };
  33. static defaultProps = {
  34. onChange: noop,
  35. };
  36. inputEntity: HTMLInputElement;
  37. blur() {
  38. this.inputEntity.blur();
  39. }
  40. focus() {
  41. this.inputEntity.focus();
  42. }
  43. render() {
  44. const { indeterminate, checked, disabled, prefixCls, name, isPureCardType, addonId, extraId } = this.props;
  45. const prefix = prefixCls || css.PREFIX;
  46. const wrapper = classnames(
  47. {
  48. [`${prefix}-inner`]: true,
  49. [`${prefix}-inner-checked`]: Boolean(checked),
  50. [`${prefix}-inner-pureCardType`]: isPureCardType,
  51. },
  52. css.WRAPPER
  53. );
  54. const inner = classnames({
  55. [`${prefix}-inner-display`]: true,
  56. });
  57. const icon = checked ? (
  58. <IconCheckboxTick />
  59. ) : indeterminate ? (
  60. <IconCheckboxIndeterminate />
  61. ) : null;
  62. return (
  63. <span className={wrapper}>
  64. <input
  65. aria-hidden={true}
  66. tabIndex={-1}
  67. ref={ref => {
  68. this.inputEntity = ref;
  69. }}
  70. type="checkbox"
  71. className={css.INPUT}
  72. onChange={noop}
  73. checked={checked}
  74. disabled={disabled}
  75. name={name}
  76. />
  77. <span className={inner}>{icon}</span>
  78. </span>
  79. );
  80. }
  81. }
  82. export default CheckboxInner;