Skip to content

Commit 974a9a2

Browse files
committed
Add support for type=lookup
1 parent 12e6833 commit 974a9a2

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

src/components/AutoFormFields.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
: 'col-span-12 xl:col-span-6' + (f.type == 'checkbox' ? ' flex items-center' : '')),
1010
f.type == 'hidden' ? 'hidden' : '']">
1111

12-
<LookupInput v-if="f.prop?.ref != null && f.type != 'file' && !f.prop.isPrimaryKey" :metadataType="dataModelType"
12+
<LookupInput v-if="f.type === 'lookup' || (f.prop?.ref != null && f.type != 'file' && !f.prop.isPrimaryKey)" :metadataType="dataModelType"
1313
:input="f" :modelValue="modelValue" @update:modelValue="updateField(f,$event)" :status="api?.error" />
1414
<DynamicInput v-else :input="f" :modelValue="modelValue" @update:modelValue="$emit('update:modelValue',$event)" :api="api" />
1515
</div>

src/components/LookupInput.vue

+25-7
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
</div>
1313
</div>
1414

15-
<div v-if="property?.ref" class="mt-1 relative">
15+
<div v-if="useRef" class="mt-1 relative">
1616
<button type="button" class="lookup flex relative w-full bg-white dark:bg-black border border-gray-300 dark:border-gray-700 rounded-md shadow-sm pl-3 pr-10 py-2 text-left focus:outline-none focus:ring-1 focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"
17-
@click="lookup(property!.ref!)"
17+
@click="lookup(useRef!)"
1818
aria-haspopup="listbox" aria-expanded="true" aria-labelledby="listbox-label">
1919
<span class="w-full inline-flex truncate">
2020
<span class="text-blue-700 dark:text-blue-300 flex cursor-pointer">
@@ -37,9 +37,9 @@
3737

3838
<script setup lang="ts">
3939
import type { ApiState, InputInfo, MetadataType, RefInfo, ResponseStatus, ModalProvider, InputProp } from '@/types'
40-
import { useConfig } from '@/use/config'
41-
import { LookupValues, typeOf, typeProperties, useMetadata } from '@/use/metadata'
42-
import { isComplexType } from '@/use/utils'
40+
import { Sole, useConfig } from '@/use/config'
41+
import { getPrimaryKey, LookupValues, typeOf, typeProperties, useMetadata } from '@/use/metadata'
42+
import { isComplexType, scopedExpr } from '@/use/utils'
4343
import { errorResponse, humanize, JsonServiceClient, mapGet, toPascalCase } from '@servicestack/client'
4444
import { computed, inject, onMounted, ref, unref } from 'vue'
4545
@@ -73,6 +73,24 @@ const value = computed(() => mapGet(props.modelValue, id.value))
7373
const property = computed(() => typeProperties(props.metadataType).find(x => x.name.toLowerCase() == id.value.toLowerCase()))
7474
const icon = computed(() => typeOf(property.value?.ref?.model)?.icon || config.value.tableIcon)
7575
76+
function withOptions(refInfo:RefInfo|null) {
77+
return !refInfo
78+
? null
79+
: props.input.options
80+
? Object.assign({}, refInfo, scopedExpr(props.input.options, {
81+
input: props.input,
82+
$typeFields: typeProperties(props.metadataType).map(x => x.name),
83+
...Sole.config.scopeWhitelist
84+
}))
85+
: refInfo
86+
}
87+
const useRef = computed(() => withOptions(property.value?.ref
88+
?? (props.input.type == 'lookup' ? {
89+
model: props.metadataType.name,
90+
refId: getPrimaryKey(props.metadataType)?.name ?? 'id',
91+
refLabel: props.metadataType.properties?.find(x => x.type == 'String' && !x.isPrimaryKey)?.name,
92+
} as RefInfo : null)))
93+
7694
let ModalProvider:ModalProvider|undefined
7795
7896
function lookup(ref:RefInfo) {
@@ -112,8 +130,8 @@ onMounted(async () => {
112130
}
113131
114132
const prop = property.value
115-
const refInfo = prop?.ref
116-
if (!refInfo) {
133+
const refInfo = useRef.value
134+
if (!prop || !refInfo) {
117135
console.warn(`No RefInfo for property '${id.value}'`)
118136
return
119137
}

0 commit comments

Comments
 (0)