unis-oa/frontend/src/components/oa/design/PathPicker.vue

86 lines
3.1 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<template>
<div class="w-full" :data-test="testId">
<label v-if="label" class="input-label mb-1.5 block">
{{ label }}
<span v-if="required" class="text-red-500">*</span>
</label>
<!--
有得挑就换成下拉,没得挑还是原来那只输入框:候选是从「样例响应」里摊出来的,
样例还没贴的时候这里本来就只能手敲,摆一只空下拉只是看着像坏了。
-->
<Select
v-if="options.length"
:model-value="modelValue"
:options="options"
:placeholder="placeholderText"
:disabled="disabled"
:compact="compact"
searchable
creatable
clearable
@update:model-value="emit('update:modelValue', String($event ?? ''))"
/>
<input
v-else
:value="modelValue"
type="text"
autocomplete="off"
:placeholder="placeholderText"
:disabled="disabled"
:class="[
'input w-full transition-all duration-200',
compact ? 'cell-control px-2' : ''
]"
@input="emit('update:modelValue', ($event.target as HTMLInputElement).value)"
/>
</div>
</template>
<script setup lang="ts">
/**
* 「值在对方响应里的哪一栏」那一格:能挑就不让人敲。
*
* 这一格要写的 ``data.print_url`` 长在**对方**的响应树里,配的人背不下来;而这一页本来就有
* 一份「样例响应」,于是把那份 JSON 摊平(``utils/jsonPaths.ts``)当候选,选项后面跟着示例值,
* 让人看着值认位置。摊不出东西(没贴样例、样例不是 JSON)时退回输入框,写法与以前一样。
*
* ``creatable`` 是留着的那扇门:清单只来自样例,真实响应里多出来的那一栏照样敲得进去——
* 挑不到不等于不能填,这一栏最终判它的是后端 ``service/fetch.py:pick_path``,不是这份清单。
*/
import { computed } from 'vue'
import { useI18n } from 'vue-i18n'
import Select, { type SelectOption } from '@/components/common/Select.vue'
import { linksFirst, pathOptionOf, type JsonValuePath } from '@/utils/jsonPaths'
const props = withDefaults(
defineProps<{
modelValue?: string
/** 从样例响应里摊出来的那些位置,由调用方算好传进来(一屏只摊一次) */
paths?: JsonValuePath[]
/** 没有 label 的那一格用在表格行里 */
label?: string
required?: boolean
placeholder?: string
/** 挑链接那一档置真:看着像地址的那几项挪到第一屏 */
preferLinks?: boolean
compact?: boolean
disabled?: boolean
testId?: string
}>(),
{ modelValue: '', paths: () => [], label: '', placeholder: '', testId: '' }
)
const emit = defineEmits<{ (e: 'update:modelValue', value: string): void }>()
const { t } = useI18n()
/** 空着的那格谈不上"输入一个位置",占位语交给 i18n,界面不把示例当已填的值 */
const placeholderText = computed(() => props.placeholder || t('ds.embedPathPlaceholder'))
const options = computed<SelectOption[]>(() => {
const hits = props.paths ?? []
if (!hits.length) return []
return (props.preferLinks ? linksFirst(hits) : hits).map(pathOptionOf)
})
</script>