acud
Version:
acg react 组件库
159 lines • 5.91 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
/**
* @file
* @author zhousheng
*/
import * as React from 'react';
import useMemo from "rc-util/es/hooks/useMemo";
import IconContext from "acud-icon/es/components/IconContext";
import LocaleProvider from '../locale-provider';
import LocaleReceiver from '../locale-provider/LocaleReceiver';
import { ConfigConsumer, ConfigContext } from './context';
import SizeContext, { SizeContextProvider } from './SizeContext';
import { FormProvider } from '../form/context';
import { unstableSetRender } from './UnstableContext';
export { ConfigContext, ConfigConsumer };
export var configConsumerProps = ['getTargetContainer', 'getPopupContainer', 'rootPrefixCls', 'getPrefixCls', 'renderEmpty', 'csp', 'autoInsertSpaceInButton', 'locale', 'pageHeader'];
// These props is used by `useContext` directly in sub component
var PASSED_PROPS = ['getTargetContainer', 'getPopupContainer', 'renderEmpty', 'pageHeader', 'input'];
export var defaultPrefixCls = 'acud';
export var defaultIconPrefixCls = "".concat(defaultPrefixCls, "icon");
var globalPrefixCls = defaultPrefixCls;
var globalIconPrefixCls;
function getGlobalIconPrefixCls(prefixCls) {
return globalIconPrefixCls || (prefixCls ? "".concat(prefixCls, "icon") : defaultIconPrefixCls);
}
var setGlobalConfig = function setGlobalConfig(_ref) {
var prefixCls = _ref.prefixCls,
iconPrefixCls = _ref.iconPrefixCls,
unstableRender = _ref.unstableRender;
if (prefixCls !== undefined) {
globalPrefixCls = prefixCls;
}
if (iconPrefixCls !== undefined) {
globalIconPrefixCls = iconPrefixCls;
}
if (unstableRender !== undefined) {
unstableSetRender(unstableRender);
}
};
export var globalConfig = function globalConfig() {
return {
getPrefixCls: function getPrefixCls(suffixCls, customizePrefixCls) {
if (customizePrefixCls) {
return customizePrefixCls;
}
return suffixCls ? "".concat(globalPrefixCls, "-").concat(suffixCls) : globalPrefixCls;
},
getIconPrefixCls: getGlobalIconPrefixCls
};
};
var ProviderChildren = function ProviderChildren(props) {
var children = props.children,
csp = props.csp,
autoInsertSpaceInButton = props.autoInsertSpaceInButton,
locale = props.locale,
componentSize = props.componentSize,
direction = props.direction,
space = props.space,
virtual = props.virtual,
dropdownMatchSelectWidth = props.dropdownMatchSelectWidth,
legacyLocale = props.legacyLocale,
parentContext = props.parentContext,
customIconPrefixCls = props.iconPrefixCls,
form = props.form;
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;
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[parentContext.getPrefixCls]);
var iconPrefixCls = customIconPrefixCls || parentContext.iconPrefixCls || getGlobalIconPrefixCls(getPrefixCls(''));
var config = _extends(_extends({}, parentContext), {
csp: csp,
autoInsertSpaceInButton: autoInsertSpaceInButton,
locale: locale || legacyLocale,
direction: direction,
space: space,
virtual: virtual,
dropdownMatchSelectWidth: dropdownMatchSelectWidth,
form: form,
getPrefixCls: getPrefixCls
});
// Pass the props used by `useContext` directly with child component.
// These props should merged into `config`.
PASSED_PROPS.forEach(function (propName) {
var propValue = props[propName];
if (propValue) {
config[propName] = propValue;
}
});
var memoedConfig = useMemo(function () {
return config;
},
// eslint-disable-next-line react-hooks/exhaustive-deps
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;
// Additional Form provider
if (locale) {
childNode = /*#__PURE__*/React.createElement(LocaleProvider, {
locale: locale
}, childNode);
}
if (iconPrefixCls) {
childNode = /*#__PURE__*/React.createElement(IconContext.Provider, {
value: memoIconContextValue
}, childNode);
}
if (componentSize) {
childNode = /*#__PURE__*/React.createElement(SizeContextProvider, {
size: componentSize
}, childNode);
}
if (form) {
childNode = /*#__PURE__*/React.createElement(FormProvider, form, childNode);
}
return /*#__PURE__*/React.createElement(ConfigContext.Provider, {
value: memoedConfig
}, childNode);
};
var ConfigProvider = function ConfigProvider(props) {
React.useEffect(function () {
if (props.direction) {
// message.config({
// rtl: props.direction === 'rtl',
// });
// notification.config({
// rtl: props.direction === 'rtl',
// });
}
}, [props.direction]);
return /*#__PURE__*/React.createElement(LocaleReceiver, null, function (_, __, legacyLocale) {
return /*#__PURE__*/React.createElement(ConfigConsumer, null, function (context) {
return /*#__PURE__*/React.createElement(ProviderChildren, _extends({
parentContext: context,
legacyLocale: legacyLocale
}, props));
});
});
};
/** @private internal Usage. do not use in your production */
ConfigProvider.ConfigContext = ConfigContext;
ConfigProvider.SizeContext = SizeContext;
ConfigProvider.config = setGlobalConfig;
export default ConfigProvider;