checkboxFoundation.ts 3.3 KB

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