checkboxInner.tsx 2.4 KB

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