Skip to content

Commit 90e9ec3

Browse files
committed
Add copy of CellFormat using Vue Setup syntax
1 parent 05ca49d commit 90e9ec3

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<template>
2+
<span v-if="labelValue">
3+
<span class="flex" :title="`${refInfo?.model} ${rawValue}`">
4+
<Icon v-if="refImage" :image="refImage" class="w-5 h-5 mr-1" />
5+
{{ labelValue }}
6+
</span>
7+
</span>
8+
<span v-else-if="isArray">
9+
<span class="mr-2">{{ value?.length }}</span>
10+
<span v-html="fmt"></span>
11+
</span>
12+
<span v-else v-html="fmt"></span>
13+
</template>
14+
15+
<script lang="ts">
16+
export default { inheritAttrs: false }
17+
</script>
18+
19+
<script setup lang="ts">
20+
// Copy of CellFormat.vue using setup
21+
import type { MetadataPropertyType, MetadataType, FormatInfo, RefInfo } from '@/types'
22+
import { mapGet } from '@servicestack/client'
23+
import { formatValue } from '@/use/formatters'
24+
import { computed, useAttrs } from 'vue'
25+
import { typeProperties, typeOf } from '@/use/metadata'
26+
import Icon from './Icon.vue'
27+
28+
const props = defineProps<{
29+
type: MetadataType|null,
30+
propType?: MetadataPropertyType|null,
31+
modelValue: any
32+
}>()
33+
34+
const attrs = useAttrs()
35+
36+
const rawValue = computed(() => props.propType ? mapGet(props.modelValue, props.propType.name!) : null)
37+
const value = computed(() => rawValue.value)
38+
const isArray = computed(() => Array.isArray(value.value))
39+
40+
const formatInfo = computed<FormatInfo|null>(() => {
41+
const prop = props.propType
42+
if (prop?.format) return prop.format
43+
if (prop?.type === 'TimeSpan' || prop?.type === 'TimeOnly') return { method: 'time' }
44+
return null
45+
})
46+
47+
const scopeAttrs = computed(() => Object.assign({ modelValue: props.modelValue }, attrs))
48+
const fmt = computed(() => formatValue(value.value, formatInfo.value || undefined, scopeAttrs.value))
49+
50+
const refInfo = computed<RefInfo|undefined>(() => props.propType?.ref || undefined)
51+
const modelProps = computed(() => props.type ? typeProperties(props.type) : [])
52+
const complexRefProp = computed(() => {
53+
const ref = refInfo.value
54+
return ref ? modelProps.value.find(x => x.type === ref.model) : null
55+
})
56+
const complexRefValue = computed(() => complexRefProp.value ? mapGet(props.modelValue, complexRefProp.value.name) : null)
57+
const labelValue = computed(() => {
58+
const ref = refInfo.value
59+
return complexRefValue.value && ref?.refLabel ? mapGet(complexRefValue.value, ref.refLabel) : null
60+
})
61+
const refImage = computed(() => {
62+
const ref = refInfo.value
63+
if (!ref) return null
64+
const t = typeOf(ref.model)
65+
return t?.icon || null
66+
})
67+
</script>

0 commit comments

Comments
 (0)