FormItemContext.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { watch, computed, inject, provide, ref, onBeforeUnmount, getCurrentInstance, defineComponent } from 'vue';
  2. import devWarning from '../vc-util/devWarning';
  3. import createContext from '../_util/createContext';
  4. const ContextKey = Symbol('ContextProps');
  5. const InternalContextKey = Symbol('InternalContextProps');
  6. export const useProvideFormItemContext = function (props) {
  7. let useValidation = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : computed(() => true);
  8. const formItemFields = ref(new Map());
  9. const addFormItemField = (key, type) => {
  10. formItemFields.value.set(key, type);
  11. formItemFields.value = new Map(formItemFields.value);
  12. };
  13. const removeFormItemField = key => {
  14. formItemFields.value.delete(key);
  15. formItemFields.value = new Map(formItemFields.value);
  16. };
  17. const instance = getCurrentInstance();
  18. watch([useValidation, formItemFields], () => {
  19. if (process.env.NODE_ENV !== 'production') {
  20. if (useValidation.value && formItemFields.value.size > 1) {
  21. devWarning(false, 'Form.Item', `FormItem can only collect one field item, you haved set ${[...formItemFields.value.values()].map(v => `\`${v.name}\``).join(', ')} ${formItemFields.value.size} field items.
  22. You can set not need to be collected fields into \`a-form-item-rest\``);
  23. let cur = instance;
  24. while (cur.parent) {
  25. console.warn('at', cur.type);
  26. cur = cur.parent;
  27. }
  28. }
  29. }
  30. });
  31. provide(ContextKey, props);
  32. provide(InternalContextKey, {
  33. addFormItemField,
  34. removeFormItemField
  35. });
  36. };
  37. const defaultContext = {
  38. id: computed(() => undefined),
  39. onFieldBlur: () => {},
  40. onFieldChange: () => {},
  41. clearValidate: () => {}
  42. };
  43. const defaultInternalContext = {
  44. addFormItemField: () => {},
  45. removeFormItemField: () => {}
  46. };
  47. export const useInjectFormItemContext = () => {
  48. const internalContext = inject(InternalContextKey, defaultInternalContext);
  49. const formItemFieldKey = Symbol('FormItemFieldKey');
  50. const instance = getCurrentInstance();
  51. internalContext.addFormItemField(formItemFieldKey, instance.type);
  52. onBeforeUnmount(() => {
  53. internalContext.removeFormItemField(formItemFieldKey);
  54. });
  55. // We should prevent the passing of context for children
  56. provide(InternalContextKey, defaultInternalContext);
  57. provide(ContextKey, defaultContext);
  58. return inject(ContextKey, defaultContext);
  59. };
  60. export default defineComponent({
  61. compatConfig: {
  62. MODE: 3
  63. },
  64. name: 'AFormItemRest',
  65. setup(_, _ref) {
  66. let {
  67. slots
  68. } = _ref;
  69. provide(InternalContextKey, defaultInternalContext);
  70. provide(ContextKey, defaultContext);
  71. return () => {
  72. var _a;
  73. return (_a = slots.default) === null || _a === void 0 ? void 0 : _a.call(slots);
  74. };
  75. }
  76. });
  77. export const FormItemInputContext = createContext({});
  78. export const NoFormStatus = defineComponent({
  79. name: 'NoFormStatus',
  80. setup(_, _ref2) {
  81. let {
  82. slots
  83. } = _ref2;
  84. FormItemInputContext.useProvide({});
  85. return () => {
  86. var _a;
  87. return (_a = slots.default) === null || _a === void 0 ? void 0 : _a.call(slots);
  88. };
  89. }
  90. });