checkboxFoundation.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. nativeEvent: {
  13. stopImmediatePropagation: () => void;
  14. }
  15. }
  16. export interface CheckboxAdapter<P = Record<string, any>, S = Record<string, any>> extends DefaultAdapter<P, S> {
  17. getIsInGroup: () => boolean;
  18. getGroupValue: () => any[];
  19. notifyGroupChange: (event: BasicCheckboxEvent) => void;
  20. getGroupDisabled: () => boolean;
  21. setNativeControlChecked: (checked: boolean) => void;
  22. getState: noopFunction;
  23. notifyChange: (event: BasicCheckboxEvent) => void;
  24. setAddonId: () => void;
  25. setExtraId: () => void;
  26. setFocusVisible: (focusVisible: boolean) => void;
  27. focusCheckboxEntity: () => void;
  28. }
  29. class CheckboxFoundation<P = Record<string, any>, S = Record<string, any>> extends BaseFoundation<CheckboxAdapter<P, S>, P, S> {
  30. constructor(adapter: CheckboxAdapter<P, S>) {
  31. super({ ...adapter });
  32. }
  33. clickState = false;
  34. init() {
  35. const { children, extra, extraId, addonId } = this.getProps();
  36. if (children && !addonId) {
  37. this._adapter.setAddonId();
  38. }
  39. if (extra && !extraId) {
  40. this._adapter.setExtraId();
  41. }
  42. }
  43. getEvent(checked: boolean, e: any) {
  44. const props = this.getProps();
  45. const cbValue = {
  46. target: {
  47. ...props,
  48. checked,
  49. },
  50. stopPropagation: () => {
  51. e.stopPropagation();
  52. },
  53. preventDefault: () => {
  54. e.preventDefault();
  55. },
  56. nativeEvent: {
  57. stopImmediatePropagation: () => {
  58. if (e.nativeEvent && typeof e.nativeEvent.stopImmediatePropagation === 'function') {
  59. e.nativeEvent.stopImmediatePropagation();
  60. }
  61. }
  62. },
  63. };
  64. return cbValue;
  65. }
  66. notifyChange(checked: boolean, e: any) {
  67. const cbValue = this.getEvent(checked, e);
  68. this._adapter.notifyChange(cbValue);
  69. }
  70. handleChange(e: any) {
  71. const disabled = this.getProp('disabled');
  72. if (disabled) {
  73. return;
  74. }
  75. if (e?.type === 'click') {
  76. this.clickState = true;
  77. }
  78. this._adapter.focusCheckboxEntity();
  79. const isInGroup = this._adapter.getIsInGroup();
  80. if (isInGroup) {
  81. const groupDisabled = this._adapter.getGroupDisabled();
  82. if (!groupDisabled) {
  83. this.handleChangeInGroup(e);
  84. }
  85. return;
  86. }
  87. const checked = this.getState('checked');
  88. const newChecked = !checked;
  89. if (this._isControlledComponent('checked')) {
  90. this.notifyChange(newChecked, e);
  91. } else {
  92. this.setChecked(newChecked);
  93. this.notifyChange(newChecked, e);
  94. }
  95. }
  96. handleChangeInGroup(e: any) {
  97. const { value } = this.getProps();
  98. const groupValue = this._adapter.getGroupValue();
  99. const checked = groupValue.includes(value);
  100. const newChecked = !checked;
  101. const event = this.getEvent(newChecked, e);
  102. this._adapter.notifyChange(event);
  103. this._adapter.notifyGroupChange(event);
  104. }
  105. handleEnterPress(e: any) {
  106. if (isEnterPress(e)) {
  107. this.handleChange(e);
  108. }
  109. }
  110. setChecked(checked: boolean) {
  111. this._adapter.setNativeControlChecked(checked);
  112. }
  113. handleFocusVisible = (event: any) => {
  114. const { target } = event;
  115. try {
  116. if (this.clickState) {
  117. this.clickState = false;
  118. return;
  119. }
  120. if (target.matches(':focus-visible')) {
  121. this._adapter.setFocusVisible(true);
  122. }
  123. } catch (error){
  124. warning(true, 'Warning: [Semi Checkbox] The current browser does not support the focus-visible');
  125. }
  126. }
  127. handleBlur = () => {
  128. this.clickState = false;
  129. this._adapter.setFocusVisible(false);
  130. }
  131. // eslint-disable-next-line @typescript-eslint/no-empty-function
  132. destroy() {}
  133. }
  134. export interface BaseCheckboxProps {
  135. id?: string;
  136. autoFocus?: boolean;
  137. checked?: boolean;
  138. defaultChecked?: boolean;
  139. disabled?: boolean;
  140. indeterminate?: boolean;
  141. onChange?: (e: BasicCheckboxEvent) => any;
  142. value?: any;
  143. style?: Record<string, any>;
  144. className?: string;
  145. prefixCls?: string;
  146. onMouseEnter?: (e: any) => void;
  147. onMouseLeave?: (e: any) => void;
  148. extra?: any;
  149. addonId?: string;
  150. extraId?: string;
  151. preventScroll?: boolean;
  152. }
  153. export default CheckboxFoundation;