checkboxFoundation.ts 3.4 KB

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