1
0

foundation.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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';
  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. /* istanbul ignore next */
  26. static get cssClasses() {
  27. // Classes extending Foundation should implement this method to return an object which exports every
  28. // CSS class the foundation class needs as a property. e.g. {ACTIVE: 'component--active'}
  29. return {};
  30. }
  31. /** @return enum{strings} */
  32. /* istanbul ignore next */
  33. static get strings() {
  34. // Classes extending Foundation should implement this method to return an object which exports all
  35. // semantic strings as constants. e.g. {ARIA_ROLE: 'tablist'}
  36. return {};
  37. }
  38. /** @return enum{numbers} */
  39. /* istanbul ignore next */
  40. static get numbers() {
  41. // Classes extending Foundation should implement this method to return an object which exports all
  42. // of its semantic numbers as constants. e.g. {ANIMATION_DELAY_MS: 350}
  43. return {};
  44. }
  45. static get defaultAdapter() {
  46. return {
  47. getProp: noop,
  48. getProps: noop,
  49. getState: noop,
  50. getStates: noop,
  51. setState: noop,
  52. getContext: noop,
  53. getContexts: noop,
  54. getCache: noop,
  55. setCache: noop,
  56. getCaches: noop,
  57. stopPropagation: noop,
  58. };
  59. }
  60. _adapter!: T;
  61. constructor(adapter: T) {
  62. this._adapter = { ...BaseFoundation.defaultAdapter, ...adapter };
  63. }
  64. getProp(key: string) {
  65. return this._adapter.getProp(key);
  66. }
  67. getProps(): any {
  68. return this._adapter.getProps() as any;
  69. }
  70. getState(key: string) {
  71. return this._adapter.getState(key);
  72. }
  73. getStates(): any {
  74. return this._adapter.getStates();
  75. }
  76. setState(states: S, cb?: (...args: any) => void) {
  77. return this._adapter.setState({ ...states }, cb);
  78. }
  79. getContext(key: string) {
  80. return this._adapter.getContext(key);
  81. }
  82. /* istanbul ignore next */
  83. getContexts() {
  84. return this._adapter.getContexts();
  85. }
  86. /* istanbul ignore next */
  87. getCaches() {
  88. return this._adapter.getCaches();
  89. }
  90. getCache(key: string) {
  91. return this._adapter.getCache(key);
  92. }
  93. setCache(key: string, value: any) {
  94. return key && this._adapter.setCache(key, value);
  95. }
  96. stopPropagation(e: any) {
  97. this._adapter.stopPropagation(e);
  98. }
  99. // Determine whether a controlled component
  100. _isControlledComponent(key: string = 'value') { // eslint-disable-line
  101. const props = this.getProps();
  102. const isControlComponent = key in (props as any);
  103. return isControlComponent;
  104. }
  105. // Does the user have incoming props, eg: _isInProps (value)
  106. _isInProps(key: string) { // eslint-disable-line
  107. const props = this.getProps();
  108. return key in (props as any);
  109. }
  110. init(lifecycle?: any) {
  111. // Subclasses should override this method to perform initialization routines (registering events, etc.)
  112. }
  113. destroy() {
  114. // Subclasses should override this method to perform de-initialization routines (de-registering events, etc.)
  115. }
  116. /* istanbul ignore next */
  117. log(text: string, ...rest: any) {
  118. log(text, ...rest);
  119. }
  120. }
  121. export default BaseFoundation;