checkboxFoundation.ts 4.8 KB

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