1
0

checkboxFoundation.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. }
  24. class CheckboxFoundation<P = Record<string, any>, S = Record<string, any>> extends BaseFoundation<CheckboxAdapter<P, S>, P, S> {
  25. constructor(adapter: CheckboxAdapter<P, S>) {
  26. super({ ...adapter });
  27. }
  28. // eslint-disable-next-line @typescript-eslint/no-empty-function
  29. init() {}
  30. getEvent(checked: boolean, e: any) {
  31. const props = this.getProps();
  32. const cbValue = {
  33. target: {
  34. ...props,
  35. checked,
  36. },
  37. stopPropagation: () => {
  38. e.stopPropagation();
  39. },
  40. preventDefault: () => {
  41. e.preventDefault();
  42. },
  43. nativeEvent: {
  44. stopImmediatePropagation: () => {
  45. if (e.nativeEvent && typeof e.nativeEvent.stopImmediatePropagation === 'function') {
  46. e.nativeEvent.stopImmediatePropagation();
  47. }
  48. }
  49. },
  50. };
  51. return cbValue;
  52. }
  53. notifyChange(checked: boolean, e: any) {
  54. const cbValue = this.getEvent(checked, e);
  55. this._adapter.notifyChange(cbValue);
  56. }
  57. handleChange(e: any) {
  58. const disabled = this.getProp('disabled');
  59. if (disabled) {
  60. return;
  61. }
  62. const isInGroup = this._adapter.getIsInGroup();
  63. if (isInGroup) {
  64. const groupDisabled = this._adapter.getGroupDisabled();
  65. if (!groupDisabled) {
  66. this.handleChangeInGroup(e);
  67. }
  68. return;
  69. }
  70. const checked = this.getState('checked');
  71. const newChecked = !checked;
  72. if (this._isControlledComponent('checked')) {
  73. this.notifyChange(newChecked, e);
  74. } else {
  75. this.setChecked(newChecked);
  76. this.notifyChange(newChecked, e);
  77. }
  78. }
  79. handleChangeInGroup(e: any) {
  80. const { value } = this.getProps();
  81. const groupValue = this._adapter.getGroupValue();
  82. const checked = groupValue.includes(value);
  83. const newChecked = !checked;
  84. const event = this.getEvent(newChecked, e);
  85. this._adapter.notifyChange(event);
  86. this._adapter.notifyGroupChange(event);
  87. }
  88. handleEnterPress(e: any) {
  89. if (isEnterPress(e)) {
  90. this.handleChange(e);
  91. }
  92. }
  93. setChecked(checked: boolean) {
  94. this._adapter.setNativeControlChecked(checked);
  95. }
  96. // eslint-disable-next-line @typescript-eslint/no-empty-function
  97. destroy() {}
  98. }
  99. export interface BaseCheckboxProps {
  100. id?: string;
  101. autoFocus?: boolean;
  102. checked?: boolean;
  103. defaultChecked?: boolean;
  104. disabled?: boolean;
  105. indeterminate?: boolean;
  106. onChange?: (e: BasicCheckboxEvent) => any;
  107. value?: any;
  108. style?: Record<string, any>;
  109. className?: string;
  110. prefixCls?: string;
  111. onMouseEnter?: (e: any) => void;
  112. onMouseLeave?: (e: any) => void;
  113. extra?: any;
  114. addonId?: string;
  115. extraId?: string;
  116. }
  117. export default CheckboxFoundation;