checkboxFoundation.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import BaseFoundation, { DefaultAdapter, noopFunction } from '../base/foundation';
  2. import isEnterPress from '../utils/isEnterPress';
  3. import warning from '../utils/warning';
  4. export interface BasicTargetObject {
  5. [x: string]: any;
  6. checked?: boolean
  7. }
  8. export interface BasicCheckboxEvent {
  9. target: BasicTargetObject;
  10. stopPropagation: () => void;
  11. preventDefault: () => void;
  12. [x: string]: any
  13. // nativeEvent: {
  14. // stopImmediatePropagation: () => void;
  15. // }
  16. }
  17. export interface CheckboxAdapter<P = Record<string, any>, S = Record<string, any>> extends DefaultAdapter<P, S> {
  18. getIsInGroup: () => boolean;
  19. getGroupValue: () => any[];
  20. notifyGroupChange: (e: any) => void;
  21. getGroupDisabled: () => boolean;
  22. setNativeControlChecked: (checked: boolean) => void;
  23. getState: noopFunction;
  24. notifyChange: (e: any) => void;
  25. setAddonId: () => void;
  26. setExtraId: () => void;
  27. setFocusVisible: (focusVisible: boolean) => void;
  28. focusCheckboxEntity: () => void;
  29. generateEvent: (checked: boolean, e: any) => any // 1.modify checked value 2.add nativeEvent on react adapter
  30. }
  31. class CheckboxFoundation<P = Record<string, any>, S = Record<string, any>> extends BaseFoundation<CheckboxAdapter<P, S>, P, S> {
  32. constructor(adapter: CheckboxAdapter<P, S>) {
  33. super({ ...adapter });
  34. }
  35. clickState = false;
  36. init() {
  37. const { children, extra, extraId, addonId } = this.getProps();
  38. if (children && !addonId) {
  39. this._adapter.setAddonId();
  40. }
  41. if (extra && !extraId) {
  42. this._adapter.setExtraId();
  43. }
  44. }
  45. notifyChange(checked: boolean, e: any) {
  46. const cbValue = this._adapter.generateEvent(checked, e);
  47. this._adapter.notifyChange(cbValue);
  48. }
  49. handleChange(e: any) {
  50. const disabled = this.getProp('disabled');
  51. if (disabled) {
  52. return;
  53. }
  54. if (e?.type === 'click') {
  55. this.clickState = true;
  56. }
  57. this._adapter.focusCheckboxEntity();
  58. const isInGroup = this._adapter.getIsInGroup();
  59. if (isInGroup) {
  60. const groupDisabled = this._adapter.getGroupDisabled();
  61. if (!groupDisabled) {
  62. this.handleChangeInGroup(e);
  63. }
  64. return;
  65. }
  66. const checked = this.getState('checked');
  67. const newChecked = !checked;
  68. if (this._isControlledComponent('checked')) {
  69. this.notifyChange(newChecked, e);
  70. } else {
  71. this.setChecked(newChecked);
  72. this.notifyChange(newChecked, e);
  73. }
  74. }
  75. handleChangeInGroup(e: any) {
  76. const { value } = this.getProps();
  77. const groupValue = this._adapter.getGroupValue();
  78. const checked = groupValue.includes(value);
  79. const newChecked = !checked;
  80. const event = this._adapter.generateEvent(newChecked, e);
  81. this._adapter.notifyChange(event);
  82. this._adapter.notifyGroupChange(event);
  83. }
  84. handleEnterPress(e: any) {
  85. if (isEnterPress(e)) {
  86. this.handleChange(e);
  87. }
  88. }
  89. setChecked(checked: boolean) {
  90. this._adapter.setNativeControlChecked(checked);
  91. }
  92. handleFocusVisible = (event: any) => {
  93. const { target } = event;
  94. try {
  95. if (this.clickState) {
  96. this.clickState = false;
  97. return;
  98. }
  99. if (target.matches(':focus-visible')) {
  100. this._adapter.setFocusVisible(true);
  101. }
  102. } catch (error) {
  103. warning(true, 'Warning: [Semi Checkbox] The current browser does not support the focus-visible');
  104. }
  105. }
  106. handleBlur = () => {
  107. this.clickState = false;
  108. this._adapter.setFocusVisible(false);
  109. }
  110. destroy() {}
  111. }
  112. export interface BaseCheckboxProps {
  113. id?: string;
  114. autoFocus?: boolean;
  115. checked?: boolean;
  116. defaultChecked?: boolean;
  117. disabled?: boolean;
  118. indeterminate?: boolean;
  119. onChange?: (e: any) => any;
  120. value?: any;
  121. style?: Record<string, any>;
  122. className?: string;
  123. prefixCls?: string;
  124. onMouseEnter?: (e: any) => void;
  125. onMouseLeave?: (e: any) => void;
  126. extra?: any;
  127. addonId?: string;
  128. extraId?: string;
  129. preventScroll?: boolean
  130. }
  131. export default CheckboxFoundation;