context.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. "use strict";
  2. var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.defaultPrefixCls = exports.defaultIconPrefixCls = exports.Variants = exports.ConfigContext = exports.ConfigConsumer = void 0;
  7. exports.useComponentConfig = useComponentConfig;
  8. var React = _interopRequireWildcard(require("react"));
  9. const defaultPrefixCls = exports.defaultPrefixCls = 'ant';
  10. const defaultIconPrefixCls = exports.defaultIconPrefixCls = 'anticon';
  11. const Variants = exports.Variants = ['outlined', 'borderless', 'filled', 'underlined'];
  12. const defaultGetPrefixCls = (suffixCls, customizePrefixCls) => {
  13. if (customizePrefixCls) {
  14. return customizePrefixCls;
  15. }
  16. return suffixCls ? `${defaultPrefixCls}-${suffixCls}` : defaultPrefixCls;
  17. };
  18. // zombieJ: 🚨 Do not pass `defaultRenderEmpty` here since it will cause circular dependency.
  19. const ConfigContext = exports.ConfigContext = /*#__PURE__*/React.createContext({
  20. // We provide a default function for Context without provider
  21. getPrefixCls: defaultGetPrefixCls,
  22. iconPrefixCls: defaultIconPrefixCls
  23. });
  24. const {
  25. Consumer: ConfigConsumer
  26. } = ConfigContext;
  27. exports.ConfigConsumer = ConfigConsumer;
  28. const EMPTY_OBJECT = {};
  29. /**
  30. * Get ConfigProvider configured component props.
  31. * This help to reduce bundle size for saving `?.` operator.
  32. * Do not use as `useMemo` deps since we do not cache the object here.
  33. *
  34. * NOTE: not refactor this with `useMemo` since memo will cost another memory space,
  35. * which will waste both compare calculation & memory.
  36. */
  37. function useComponentConfig(propName) {
  38. const context = React.useContext(ConfigContext);
  39. const {
  40. getPrefixCls,
  41. direction,
  42. getPopupContainer
  43. } = context;
  44. const propValue = context[propName];
  45. return Object.assign(Object.assign({
  46. classNames: EMPTY_OBJECT,
  47. styles: EMPTY_OBJECT
  48. }, propValue), {
  49. getPrefixCls,
  50. direction,
  51. getPopupContainer
  52. });
  53. }