1
0

checkboxInner.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. }
  26. class CheckboxInner extends PureComponent<CheckboxInnerProps> {
  27. static contextType = Context;
  28. static propTypes = {
  29. 'aria-describedby': PropTypes.string,
  30. 'aria-errormessage': PropTypes.string,
  31. 'aria-invalid': PropTypes.bool,
  32. 'aria-labelledby': PropTypes.string,
  33. 'aria-required': PropTypes.bool,
  34. checked: PropTypes.bool,
  35. disabled: PropTypes.bool,
  36. onChange: PropTypes.func,
  37. children: PropTypes.node,
  38. grouped: PropTypes.bool,
  39. value: PropTypes.any,
  40. isPureCardType: PropTypes.bool,
  41. addonId: PropTypes.string,
  42. extraId: PropTypes.string,
  43. };
  44. static defaultProps = {
  45. onChange: noop,
  46. };
  47. inputEntity: HTMLInputElement;
  48. blur() {
  49. this.inputEntity.blur();
  50. }
  51. focus() {
  52. this.inputEntity.focus();
  53. }
  54. render() {
  55. const { indeterminate, checked, disabled, prefixCls, name, isPureCardType, addonId, extraId } = this.props;
  56. const prefix = prefixCls || css.PREFIX;
  57. const wrapper = classnames(
  58. {
  59. [`${prefix}-inner`]: true,
  60. [`${prefix}-inner-checked`]: Boolean(checked),
  61. [`${prefix}-inner-pureCardType`]: isPureCardType,
  62. },
  63. css.WRAPPER
  64. );
  65. const inner = classnames({
  66. [`${prefix}-inner-display`]: true,
  67. });
  68. const icon = checked ? (
  69. <IconCheckboxTick />
  70. ) : indeterminate ? (
  71. <IconCheckboxIndeterminate />
  72. ) : null;
  73. return (
  74. <span className={wrapper}>
  75. <input
  76. type="checkbox"
  77. aria-label={this.props['aria-label']}
  78. aria-disabled={disabled}
  79. aria-checked={checked}
  80. aria-labelledby={addonId}
  81. aria-describedby={extraId || this.props['aria-describedby']}
  82. aria-invalid={this.props['aria-invalid']}
  83. aria-errormessage={this.props['aria-errormessage']}
  84. aria-required={this.props['aria-required']}
  85. ref={ref => {
  86. this.inputEntity = ref;
  87. }}
  88. className={css.INPUT}
  89. onChange={noop}
  90. checked={checked}
  91. disabled={disabled}
  92. name={name}
  93. />
  94. <span className={inner}>{icon}</span>
  95. </span>
  96. );
  97. }
  98. }
  99. export default CheckboxInner;