useVariants.js 1.1 KB

123456789101112131415161718192021222324252627
  1. import * as React from 'react';
  2. import { VariantContext } from '../context';
  3. import { ConfigContext, Variants } from '../../config-provider';
  4. /**
  5. * Compatible for legacy `bordered` prop.
  6. */
  7. const useVariant = (component, variant, legacyBordered = undefined) => {
  8. var _a, _b;
  9. const {
  10. variant: configVariant,
  11. [component]: componentConfig
  12. } = React.useContext(ConfigContext);
  13. const ctxVariant = React.useContext(VariantContext);
  14. const configComponentVariant = componentConfig === null || componentConfig === void 0 ? void 0 : componentConfig.variant;
  15. let mergedVariant;
  16. if (typeof variant !== 'undefined') {
  17. mergedVariant = variant;
  18. } else if (legacyBordered === false) {
  19. mergedVariant = 'borderless';
  20. } else {
  21. // form variant > component global variant > global variant
  22. mergedVariant = (_b = (_a = ctxVariant !== null && ctxVariant !== void 0 ? ctxVariant : configComponentVariant) !== null && _a !== void 0 ? _a : configVariant) !== null && _b !== void 0 ? _b : 'outlined';
  23. }
  24. const enableVariantCls = Variants.includes(mergedVariant);
  25. return [mergedVariant, enableVariantCls];
  26. };
  27. export default useVariant;