index.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  2. import _extends from "@babel/runtime/helpers/esm/extends";
  3. import { createVNode as _createVNode } from "vue";
  4. import { computed, reactive, provide, defineComponent, watch, watchEffect } from 'vue';
  5. import defaultRenderEmpty from './renderEmpty';
  6. import LocaleProvider, { ANT_MARK } from '../locale-provider';
  7. import LocaleReceiver from '../locale-provider/LocaleReceiver';
  8. import message from '../message';
  9. import notification from '../notification';
  10. import { registerTheme } from './cssVariables';
  11. import defaultLocale from '../locale/default';
  12. import { configProviderProps, useProvideGlobalForm } from './context';
  13. export var defaultPrefixCls = 'ant';
  14. function getGlobalPrefixCls() {
  15. return globalConfigForApi.prefixCls || defaultPrefixCls;
  16. }
  17. var globalConfigByCom = reactive({});
  18. var globalConfigBySet = reactive({}); // 权重最大
  19. export var globalConfigForApi = reactive({});
  20. watchEffect(function () {
  21. _extends(globalConfigForApi, globalConfigByCom, globalConfigBySet);
  22. globalConfigForApi.prefixCls = getGlobalPrefixCls();
  23. globalConfigForApi.getPrefixCls = function (suffixCls, customizePrefixCls) {
  24. if (customizePrefixCls) return customizePrefixCls;
  25. return suffixCls ? "".concat(globalConfigForApi.prefixCls, "-").concat(suffixCls) : globalConfigForApi.prefixCls;
  26. };
  27. globalConfigForApi.getRootPrefixCls = function (rootPrefixCls, customizePrefixCls) {
  28. // Customize rootPrefixCls is first priority
  29. if (rootPrefixCls) {
  30. return rootPrefixCls;
  31. }
  32. // If Global prefixCls provided, use this
  33. if (globalConfigForApi.prefixCls) {
  34. return globalConfigForApi.prefixCls;
  35. }
  36. // [Legacy] If customize prefixCls provided, we cut it to get the prefixCls
  37. if (customizePrefixCls && customizePrefixCls.includes('-')) {
  38. return customizePrefixCls.replace(/^(.*)-[^-]*$/, '$1');
  39. }
  40. // Fallback to default prefixCls
  41. return getGlobalPrefixCls();
  42. };
  43. });
  44. var stopWatchEffect;
  45. var setGlobalConfig = function setGlobalConfig(params) {
  46. if (stopWatchEffect) {
  47. stopWatchEffect();
  48. }
  49. stopWatchEffect = watchEffect(function () {
  50. _extends(globalConfigBySet, reactive(params));
  51. _extends(globalConfigForApi, reactive(params));
  52. });
  53. if (params.theme) {
  54. registerTheme(getGlobalPrefixCls(), params.theme);
  55. }
  56. };
  57. export var globalConfig = function globalConfig() {
  58. return {
  59. getPrefixCls: function getPrefixCls(suffixCls, customizePrefixCls) {
  60. if (customizePrefixCls) return customizePrefixCls;
  61. return suffixCls ? "".concat(getGlobalPrefixCls(), "-").concat(suffixCls) : getGlobalPrefixCls();
  62. },
  63. getRootPrefixCls: function getRootPrefixCls(rootPrefixCls, customizePrefixCls) {
  64. // Customize rootPrefixCls is first priority
  65. if (rootPrefixCls) {
  66. return rootPrefixCls;
  67. }
  68. // If Global prefixCls provided, use this
  69. if (globalConfigForApi.prefixCls) {
  70. return globalConfigForApi.prefixCls;
  71. }
  72. // [Legacy] If customize prefixCls provided, we cut it to get the prefixCls
  73. if (customizePrefixCls && customizePrefixCls.includes('-')) {
  74. return customizePrefixCls.replace(/^(.*)-[^-]*$/, '$1');
  75. }
  76. // Fallback to default prefixCls
  77. return getGlobalPrefixCls();
  78. }
  79. };
  80. };
  81. var ConfigProvider = defineComponent({
  82. compatConfig: {
  83. MODE: 3
  84. },
  85. name: 'AConfigProvider',
  86. inheritAttrs: false,
  87. props: configProviderProps(),
  88. setup: function setup(props, _ref) {
  89. var slots = _ref.slots;
  90. var getPrefixCls = function getPrefixCls(suffixCls, customizePrefixCls) {
  91. var _props$prefixCls = props.prefixCls,
  92. prefixCls = _props$prefixCls === void 0 ? 'ant' : _props$prefixCls;
  93. if (customizePrefixCls) return customizePrefixCls;
  94. return suffixCls ? "".concat(prefixCls, "-").concat(suffixCls) : prefixCls;
  95. };
  96. var renderEmptyComponent = function renderEmptyComponent(name) {
  97. var renderEmpty = props.renderEmpty || slots.renderEmpty || defaultRenderEmpty;
  98. return renderEmpty(name);
  99. };
  100. var getPrefixClsWrapper = function getPrefixClsWrapper(suffixCls, customizePrefixCls) {
  101. var prefixCls = props.prefixCls;
  102. if (customizePrefixCls) return customizePrefixCls;
  103. var mergedPrefixCls = prefixCls || getPrefixCls('');
  104. return suffixCls ? "".concat(mergedPrefixCls, "-").concat(suffixCls) : mergedPrefixCls;
  105. };
  106. var configProvider = reactive(_objectSpread(_objectSpread({}, props), {}, {
  107. getPrefixCls: getPrefixClsWrapper,
  108. renderEmpty: renderEmptyComponent
  109. }));
  110. Object.keys(props).forEach(function (key) {
  111. watch(function () {
  112. return props[key];
  113. }, function () {
  114. configProvider[key] = props[key];
  115. });
  116. });
  117. if (!props.notUpdateGlobalConfig) {
  118. _extends(globalConfigByCom, configProvider);
  119. watch(configProvider, function () {
  120. _extends(globalConfigByCom, configProvider);
  121. });
  122. }
  123. var validateMessagesRef = computed(function () {
  124. // Additional Form provider
  125. var validateMessages = {};
  126. if (props.locale) {
  127. var _props$locale$Form, _defaultLocale$Form;
  128. validateMessages = ((_props$locale$Form = props.locale.Form) === null || _props$locale$Form === void 0 ? void 0 : _props$locale$Form.defaultValidateMessages) || ((_defaultLocale$Form = defaultLocale.Form) === null || _defaultLocale$Form === void 0 ? void 0 : _defaultLocale$Form.defaultValidateMessages) || {};
  129. }
  130. if (props.form && props.form.validateMessages) {
  131. validateMessages = _objectSpread(_objectSpread({}, validateMessages), props.form.validateMessages);
  132. }
  133. return validateMessages;
  134. });
  135. useProvideGlobalForm({
  136. validateMessages: validateMessagesRef
  137. });
  138. provide('configProvider', configProvider);
  139. var renderProvider = function renderProvider(legacyLocale) {
  140. var _slots$default;
  141. return _createVNode(LocaleProvider, {
  142. "locale": props.locale || legacyLocale,
  143. "ANT_MARK__": ANT_MARK
  144. }, {
  145. default: function _default() {
  146. return [(_slots$default = slots.default) === null || _slots$default === void 0 ? void 0 : _slots$default.call(slots)];
  147. }
  148. });
  149. };
  150. watchEffect(function () {
  151. if (props.direction) {
  152. message.config({
  153. rtl: props.direction === 'rtl'
  154. });
  155. notification.config({
  156. rtl: props.direction === 'rtl'
  157. });
  158. }
  159. });
  160. return function () {
  161. return _createVNode(LocaleReceiver, {
  162. "children": function children(_, __, legacyLocale) {
  163. return renderProvider(legacyLocale);
  164. }
  165. }, null);
  166. };
  167. }
  168. });
  169. export var defaultConfigProvider = reactive({
  170. getPrefixCls: function getPrefixCls(suffixCls, customizePrefixCls) {
  171. if (customizePrefixCls) return customizePrefixCls;
  172. return suffixCls ? "ant-".concat(suffixCls) : 'ant';
  173. },
  174. renderEmpty: defaultRenderEmpty,
  175. direction: 'ltr'
  176. });
  177. ConfigProvider.config = setGlobalConfig;
  178. ConfigProvider.install = function (app) {
  179. app.component(ConfigProvider.name, ConfigProvider);
  180. };
  181. export default ConfigProvider;