baseComponent.tsx 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. import React, { Component, ReactNode } from 'react';
  6. import log from '@douyinfe/semi-foundation/utils/log';
  7. import { DefaultAdapter } from '@douyinfe/semi-foundation/base/foundation';
  8. import { VALIDATE_STATUS } from '@douyinfe/semi-foundation/base/constants';
  9. import { ArrayElement } from './base';
  10. const { hasOwnProperty } = Object.prototype;
  11. export type ValidateStatus = ArrayElement<typeof VALIDATE_STATUS>;
  12. export interface BaseProps {
  13. style?: React.CSSProperties;
  14. className?: string;
  15. children?: ReactNode | undefined | any;
  16. }
  17. // eslint-disable-next-line
  18. export default class BaseComponent<P extends BaseProps = {}, S = {}> extends Component<P, S> {
  19. static propTypes = {};
  20. static defaultProps = {};
  21. cache: any;
  22. foundation: any;
  23. constructor(props: P) {
  24. super(props);
  25. this.cache = {};
  26. this.foundation = null;
  27. }
  28. componentDidMount(): void {
  29. this.foundation && typeof this.foundation.init === 'function' && this.foundation.init();
  30. }
  31. componentWillUnmount(): void {
  32. this.foundation && typeof this.foundation.destroy === 'function' && this.foundation.destroy();
  33. this.cache = {};
  34. }
  35. get adapter(): DefaultAdapter<P, S> { // eslint-disable-line
  36. return {
  37. getContext: key => { // eslint-disable-line
  38. if (this.context && key) {
  39. return this.context[key];
  40. }
  41. },
  42. getContexts: () => this.context, // eslint-disable-line
  43. getProp: key => this.props[key], // eslint-disable-line
  44. // return all props
  45. getProps: () => this.props, // eslint-disable-line
  46. getState: key => this.state[key], // eslint-disable-line
  47. getStates: () => this.state, // eslint-disable-line
  48. setState: (states, cb) => this.setState({ ...states }, cb), // eslint-disable-line
  49. getCache: key => key && this.cache[key], // eslint-disable-line
  50. getCaches: () => this.cache, // eslint-disable-line
  51. setCache: (key, value) => key && (this.cache[key] = value), // eslint-disable-line
  52. stopPropagation: e => { // eslint-disable-line
  53. try {
  54. e.stopPropagation();
  55. e.nativeEvent && e.nativeEvent.stopImmediatePropagation();
  56. } catch (error) {
  57. }
  58. }
  59. };
  60. }
  61. // eslint-disable-next-line
  62. isControlled = (key: any) => Boolean(key && this.props && typeof this.props === 'object' && hasOwnProperty.call(this.props, key));
  63. log(text: string, ...rest: any): any {
  64. return log(text, ...rest);
  65. }
  66. }