checkboxFoundation.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import BaseFoundation, { DefaultAdapter, noopFunction } from '../base/foundation';
  2. export interface BasicTargetObject {
  3. [x: string]: any;
  4. checked?: boolean;
  5. }
  6. export interface BasicCheckboxEvent {
  7. target: BasicTargetObject;
  8. stopPropagation: () => void;
  9. preventDefault: () => void;
  10. nativeEvent: {
  11. stopImmediatePropagation: () => void;
  12. }
  13. }
  14. export interface CheckboxAdapter<P = Record<string, any>, S = Record<string, any>> extends DefaultAdapter<P, S> {
  15. getIsInGroup: () => boolean;
  16. getGroupValue: () => any[];
  17. notifyGroupChange: (event: BasicCheckboxEvent) => void;
  18. getGroupDisabled: () => boolean;
  19. setNativeControlChecked: (checked: boolean) => void;
  20. getState: noopFunction;
  21. notifyChange: (event: BasicCheckboxEvent) => void;
  22. }
  23. class CheckboxFoundation<P = Record<string, any>, S = Record<string, any>> extends BaseFoundation<CheckboxAdapter<P, S>, P, S> {
  24. constructor(adapter: CheckboxAdapter<P, S>) {
  25. super({ ...adapter });
  26. }
  27. // eslint-disable-next-line @typescript-eslint/no-empty-function
  28. init() {}
  29. getEvent(checked: boolean, e: any) {
  30. const props = this.getProps();
  31. const cbValue = {
  32. target: {
  33. ...props,
  34. checked,
  35. },
  36. stopPropagation: () => {
  37. e.stopPropagation();
  38. },
  39. preventDefault: () => {
  40. e.preventDefault();
  41. },
  42. nativeEvent: {
  43. stopImmediatePropagation: () => {
  44. if (e.nativeEvent && typeof e.nativeEvent.stopImmediatePropagation === 'function') {
  45. e.nativeEvent.stopImmediatePropagation();
  46. }
  47. }
  48. },
  49. };
  50. return cbValue;
  51. }
  52. notifyChange(checked: boolean, e: any) {
  53. const cbValue = this.getEvent(checked, e);
  54. this._adapter.notifyChange(cbValue);
  55. }
  56. handleChange(e: any) {
  57. const disabled = this.getProp('disabled');
  58. if (disabled) {
  59. return;
  60. }
  61. const isInGroup = this._adapter.getIsInGroup();
  62. if (isInGroup) {
  63. const groupDisabled = this._adapter.getGroupDisabled();
  64. if (!groupDisabled) {
  65. this.handleChangeInGroup(e);
  66. }
  67. return;
  68. }
  69. const checked = this.getState('checked');
  70. const newChecked = !checked;
  71. if (this._isControlledComponent('checked')) {
  72. this.notifyChange(newChecked, e);
  73. } else {
  74. this.setChecked(newChecked);
  75. this.notifyChange(newChecked, e);
  76. }
  77. }
  78. handleChangeInGroup(e: any) {
  79. const { value } = this.getProps();
  80. const groupValue = this._adapter.getGroupValue();
  81. const checked = groupValue.includes(value);
  82. const newChecked = !checked;
  83. const event = this.getEvent(newChecked, e);
  84. this._adapter.notifyChange(event);
  85. this._adapter.notifyGroupChange(event);
  86. }
  87. setChecked(checked: boolean) {
  88. this._adapter.setNativeControlChecked(checked);
  89. }
  90. // eslint-disable-next-line @typescript-eslint/no-empty-function
  91. destroy() {}
  92. }
  93. export interface BaseCheckboxProps {
  94. autoFocus?: boolean;
  95. checked?: boolean;
  96. defaultChecked?: boolean;
  97. disabled?: boolean;
  98. indeterminate?: boolean;
  99. onChange?: (e: BasicCheckboxEvent) => any;
  100. value?: any;
  101. style?: Record<string, any>;
  102. className?: string;
  103. prefixCls?: string;
  104. onMouseEnter?: (e: any) => void;
  105. onMouseLeave?: (e: any) => void;
  106. extra?: any;
  107. }
  108. export default CheckboxFoundation;