You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

0 lines
6.7 KiB

3 months ago
  1. {"ast":null,"code":"import { inject, ref, computed, onMounted, watch, toRef, onUnmounted } from 'vue';\nimport { formContextKey, formItemContextKey } from '../constants.mjs';\nimport { useId } from '../../../../hooks/use-id/index.mjs';\nconst useFormItem = () => {\n const form = inject(formContextKey, void 0);\n const formItem = inject(formItemContextKey, void 0);\n return {\n form,\n formItem\n };\n};\nconst useFormItemInputId = (props, {\n formItemContext,\n disableIdGeneration,\n disableIdManagement\n}) => {\n if (!disableIdGeneration) {\n disableIdGeneration = ref(false);\n }\n if (!disableIdManagement) {\n disableIdManagement = ref(false);\n }\n const inputId = ref();\n let idUnwatch = void 0;\n const isLabeledByFormItem = computed(() => {\n var _a;\n return !!(!(props.label || props.ariaLabel) && formItemContext && formItemContext.inputIds && ((_a = formItemContext.inputIds) == null ? void 0 : _a.length) <= 1);\n });\n onMounted(() => {\n idUnwatch = watch([toRef(props, \"id\"), disableIdGeneration], ([id, disableIdGeneration2]) => {\n const newId = id != null ? id : !disableIdGeneration2 ? useId().value : void 0;\n if (newId !== inputId.value) {\n if (formItemContext == null ? void 0 : formItemContext.removeInputId) {\n inputId.value && formItemContext.removeInputId(inputId.value);\n if (!(disableIdManagement == null ? void 0 : disableIdManagement.value) && !disableIdGeneration2 && newId) {\n formItemContext.addInputId(newId);\n }\n }\n inputId.value = newId;\n }\n }, {\n immediate: true\n });\n });\n onUnmounted(() => {\n idUnwatch && idUnwatch();\n if (formItemContext == null ? void 0 : formItemContext.removeInputId) {\n inputId.value && formItemContext.removeInputId(inputId.value);\n }\n });\n return {\n isLabeledByFormItem,\n inputId\n };\n};\nexport { useFormItem, useFormItemInputId };","map":{"version":3,"names":["useFormItem","form","inject","formContextKey","formItem","formItemContextKey","useFormItemInputId","props","formItemContext","disableIdGeneration","disableIdManagement","ref","inputId","idUnwatch","isLabeledByFormItem","computed","_a","label","ariaLabel","inputIds","length","onMounted","watch","toRef","id","disableIdGeneration2","newId","useId","value","removeInputId","addInputId","immediate","onUnmounted"],"sources":["../../../../../../../packages/components/form/src/hooks/use-form-item.ts"],"sourcesContent":["import {\n computed,\n inject,\n onMounted,\n onUnmounted,\n ref,\n toRef,\n watch,\n} from 'vue'\nimport { useId } from '@element-plus/hooks'\nimport { formContextKey, formItemContextKey } from '../constants'\n\nimport type { ComputedRef, Ref, WatchStopHandle } from 'vue'\nimport type { FormItemContext } from '../types'\n\nexport const useFormItem = () => {\n const form = inject(formContextKey, undefined)\n const formItem = inject(formItemContextKey, undefined)\n return {\n form,\n formItem,\n }\n}\n\nexport type IUseFormItemInputCommonProps = {\n id?: string\n label?: string | number | boolean | Record<string, any>\n ariaLabel?: string | number | boolean | Record<string, any>\n}\n\nexport const useFormItemInputId = (\n props: Partial<IUseFormItemInputCommonProps>,\n {\n formItemContext,\n disableIdGeneration,\n disableIdManagement,\n }: {\n formItemContext?: FormItemContext\n disableIdGeneration?: ComputedRef<boolean> | Ref<boolean>\n disableIdManagement?: ComputedRef<boolean> | Ref<boolean>\n }\n) => {\n if (!disableIdGeneration) {\n disableIdGeneration = ref<boolean>(false)\n }\n if (!disableIdManagement) {\n disableIdManagement = ref<boolean>(false)\n }\n\n const inputId = ref<string>()\n let idUnwatch: WatchStopHandle | undefined = undefined\n\n const isLabeledByFormItem = computed<boolean>(() => {\n return !!(\n !(props.label || props.ariaLabel) &&\n formItemContext &&\n formItemContext.inputIds &&\n formItemContext.inputIds?.length <= 1\n )\n })\n\n // Gene