UNPKG

zp-bee

Version:

zp-bee,是一款基于 Dumi,由 React + TypeScript 开发的组件库 🎉。

114 lines (98 loc) 4 kB
import React from 'react'; import LocaleProvider, { ZP_MARK } from '../bee-locale'; import LocaleReceiver from '../bee-locale/LocaleReceiver'; import useMemo from 'rc-util/lib/hooks/useMemo'; import { ConfigConsumer, ConfigContext } from './context'; export { ConfigContext, ConfigConsumer }; export var defaultPrefixCls = 'bee'; var globalPrefixCls; var setGlobalConfig = function setGlobalConfig(params) { if (params.prefixCls !== undefined) { globalPrefixCls = params.prefixCls; } }; function getGlobalPrefixCls() { return globalPrefixCls || defaultPrefixCls; } export var globalConfig = function globalConfig() { return { getPrefixCls: function getPrefixCls(suffixCls, customizePrefixCls) { if (customizePrefixCls) return customizePrefixCls; return suffixCls ? "".concat(getGlobalPrefixCls(), "-").concat(suffixCls) : getGlobalPrefixCls(); }, getRootPrefixCls: function getRootPrefixCls(rootPrefixCls, customizePrefixCls) { // Customize rootPrefixCls is first priority if (rootPrefixCls) { return rootPrefixCls; } // If Global prefixCls provided, use this if (globalPrefixCls) { return globalPrefixCls; } // [Legacy] If customize prefixCls provided, we cut it to get the prefixCls if (customizePrefixCls && customizePrefixCls.includes('-')) { return customizePrefixCls.replace(/^(.*)-[^-]*$/, '$1'); } // Fallback to default prefixCls return getGlobalPrefixCls(); } }; }; var ProviderChildren = function ProviderChildren(props) { var children = props.children, autoInsertSpaceInButton = props.autoInsertSpaceInButton, locale = props.locale, virtual = props.virtual, dropdownMatchSelectWidth = props.dropdownMatchSelectWidth, legacyLocale = props.legacyLocale, parentContext = props.parentContext, iconPrefixCls = props.iconPrefixCls; var getPrefixCls = React.useCallback(function (suffixCls, customizePrefixCls) { var prefixCls = props.prefixCls; if (customizePrefixCls) return customizePrefixCls; var mergedPrefixCls = prefixCls || parentContext.getPrefixCls(''); return suffixCls ? "".concat(mergedPrefixCls, "-").concat(suffixCls) : mergedPrefixCls; }, [parentContext.getPrefixCls]); var config = Object.assign(Object.assign({}, parentContext), { autoInsertSpaceInButton: autoInsertSpaceInButton, locale: locale || legacyLocale, virtual: virtual, dropdownMatchSelectWidth: dropdownMatchSelectWidth, getPrefixCls: getPrefixCls }); // https://github.com/ant-design/ant-design/issues/27617 var memoedConfig = useMemo(function () { return config; }, config, function (prevConfig, currentConfig) { var prevKeys = Object.keys(prevConfig); var currentKeys = Object.keys(currentConfig); return prevKeys.length !== currentKeys.length || prevKeys.some(function (key) { return prevConfig[key] !== currentConfig[key]; }); }); var memoIconContextValue = React.useMemo(function () { return { prefixCls: iconPrefixCls }; }, [iconPrefixCls]); var childNode = children; if (locale) { childNode = /*#__PURE__*/React.createElement(LocaleProvider, { locale: locale, _ZP_MARK__: ZP_MARK }, childNode); } return /*#__PURE__*/React.createElement(ConfigContext.Provider, { value: memoedConfig }, childNode); }; var ConfigProvider = function ConfigProvider(props) { return /*#__PURE__*/React.createElement(LocaleReceiver, null, function (_, __, legacyLocale) { return /*#__PURE__*/React.createElement(ConfigConsumer, null, function (context) { return /*#__PURE__*/React.createElement(ProviderChildren, Object.assign({ parentContext: context, legacyLocale: legacyLocale }, props)); }); }); }; /** @private internal Usage. do not use in your production */ ConfigProvider.ConfigContext = ConfigContext; ConfigProvider.config = setGlobalConfig; export default ConfigProvider;