foundation.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * The Semi Foundation / Adapter architecture split was inspired by Material Component For Web. (https://github.com/material-components/material-components-web)
  3. * We re-implemented our own code based on the principle and added more functions we need according to actual needs.
  4. *
  5. */
  6. import log from '../utils/log';
  7. import { noop } from 'lodash-es';
  8. export type noopFunction = (...args: any) => any;
  9. // eslint-disable-next-line
  10. export interface DefaultAdapter<P = Record<string, any>, S = Record<string, any>> {
  11. getContext(key: string): any;
  12. getContexts(): any;
  13. getProp(key: string): any;
  14. getProps(): P;
  15. getState(key: string): any;
  16. getStates(): S;
  17. setState(s: Pick<S, keyof S>, callback?: any): void;
  18. getCache(c: string): any;
  19. getCaches(): any;
  20. setCache(key: any, value: any): void;
  21. stopPropagation(e: any): void;
  22. }
  23. class BaseFoundation<T extends Partial<DefaultAdapter<P, S>>, P = Record<string, any>, S = Record<string, any>> {
  24. /** @return enum{css className} */
  25. static get cssClasses() {
  26. // Classes extending Foundation should implement this method to return an object which exports every
  27. // CSS class the foundation class needs as a property. e.g. {ACTIVE: 'component--active'}
  28. return {};
  29. }
  30. /** @return enum{strings} */
  31. static get strings() {
  32. // Classes extending Foundation should implement this method to return an object which exports all
  33. // semantic strings as constants. e.g. {ARIA_ROLE: 'tablist'}
  34. return {};
  35. }
  36. /** @return enum{numbers} */
  37. static get numbers() {
  38. // Classes extending Foundation should implement this method to return an object which exports all
  39. // of its semantic numbers as constants. e.g. {ANIMATION_DELAY_MS: 350}
  40. return {};
  41. }
  42. static get defaultAdapter() {
  43. return {
  44. getProp: noop,
  45. getProps: noop,
  46. getState: noop,
  47. getStates: noop,
  48. setState: noop,
  49. getContext: noop,
  50. getContexts: noop,
  51. getCache: noop,
  52. setCache: noop,
  53. getCaches: noop,
  54. stopPropagation: noop,
  55. };
  56. }
  57. _adapter!: T;
  58. constructor(adapter: T) {
  59. this._adapter = { ...BaseFoundation.defaultAdapter, ...adapter };
  60. }
  61. getProp(key: string) {
  62. return this._adapter.getProp(key);
  63. }
  64. getProps(): any {
  65. return this._adapter.getProps() as any;
  66. }
  67. getState(key: string) {
  68. return this._adapter.getState(key);
  69. }
  70. getStates(): any {
  71. return this._adapter.getStates();
  72. }
  73. setState(states: S, cb?: (...args: any) => void) {
  74. return this._adapter.setState({ ...states }, cb);
  75. }
  76. getContext(key: string) {
  77. return this._adapter.getContext(key);
  78. }
  79. getContexts() {
  80. return this._adapter.getContexts();
  81. }
  82. getCaches() {
  83. return this._adapter.getCaches();
  84. }
  85. getCache(key: string) {
  86. return this._adapter.getCache(key);
  87. }
  88. setCache(key: string, value: any) {
  89. return key && this._adapter.setCache(key, value);
  90. }
  91. stopPropagation(e: any) {
  92. this._adapter.stopPropagation(e);
  93. }
  94. // Determine whether a controlled component
  95. _isControlledComponent(key: string = 'value') { // eslint-disable-line
  96. const props = this.getProps();
  97. const isControlComponent = key in (props as any);
  98. return isControlComponent;
  99. }
  100. // Does the user have incoming props, eg: _isInProps (value)
  101. _isInProps(key: string) { // eslint-disable-line
  102. const props = this.getProps();
  103. return key in (props as any);
  104. }
  105. init(lifecycle?: any) {
  106. // Subclasses should override this method to perform initialization routines (registering events, etc.)
  107. }
  108. destroy() {
  109. // Subclasses should override this method to perform de-initialization routines (de-registering events, etc.)
  110. }
  111. log(text: string, ...rest: any) {
  112. log(text, ...rest);
  113. }
  114. }
  115. export default BaseFoundation;