Skip to content

Commit a09d3de

Browse files
committed
feat(PrimeOutputList): add converter functions for output list and update props to support conversion
1 parent cb627fc commit a09d3de

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

dev/pages/outputs/OutputList.vue

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@ const primeAttributes = ''
55
const customAttributes = 'iconPrefix, prefix, suffix, iconSuffix'
66
const { addElement } = useFormKitSchema()
77
8+
function convertValues(value: string[]): string[] {
9+
return value.map(item => item.toUpperCase())
10+
}
11+
12+
function convertValuesCharCount(value: string[]): string[] {
13+
return value.map(item => `${item} (${item.length})`)
14+
}
15+
16+
function convertValuesSortedReverse(value: string[]): string[] {
17+
return value.sort((a, b) => a.localeCompare(b)).reverse()
18+
}
19+
820
const schema
921
= [
1022
addElement('h3', ['Default (listStyle: span)']),
@@ -27,6 +39,28 @@ const schema
2739
iconPrefix: 'pi pi-list',
2840
divider: ' - ',
2941
},
42+
addElement('h3', ['Converter']),
43+
44+
{
45+
$formkit: 'primeOutputList',
46+
name: 'list1',
47+
label: 'Converter Function',
48+
convertValue: convertValues,
49+
divider: ' - ',
50+
},
51+
{
52+
$formkit: 'primeOutputList',
53+
name: 'list2',
54+
label: 'Converter Function - Char Count',
55+
convertValue: convertValuesCharCount,
56+
},
57+
{
58+
$formkit: 'primeOutputList',
59+
name: 'list2',
60+
label: 'Converter Function - Sorted Reverse',
61+
convertValue: convertValuesSortedReverse,
62+
},
63+
addElement('h3', ['Chips']),
3064
{
3165
$formkit: 'primeOutputList',
3266
name: 'list2',

src/components/PrimeOutputList.vue

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ import FormKitIcon from './FormKitIcon.vue'
88
import FormKitPrefix from './FormKitPrefix.vue'
99
import FormKitSuffix from './FormKitSuffix.vue'
1010
11+
export interface PrimeOutputListProps {
12+
convertValue?: (array: []) => []
13+
}
14+
1115
const props = defineProps({
1216
context: {
13-
type: Object as PropType<FormKitFrameworkContext> & FormKitIconProps,
17+
type: Object as PropType<FormKitFrameworkContext> & FormKitIconProps & PrimeOutputListProps,
1418
required: true,
1519
},
1620
})
@@ -20,34 +24,42 @@ const listStyle = computed(() => {
2024
})
2125
2226
const { hasPrefix, hasPrefixIcon, hasSuffix, hasSuffixIcon } = useFormKitSection(props.context)
27+
28+
const getListValues = computed(() => {
29+
const values = Array.isArray(props.context?._value) ? props.context._value : []
30+
if (typeof props.context?.convertValue === 'function') {
31+
return [props.context.convertValue([...values])]
32+
}
33+
return values
34+
})
2335
</script>
2436

2537
<template>
2638
<div class="p-formkit p-output-list">
2739
<FormKitIcon v-if="hasPrefixIcon" :icon-class="context?.iconPrefix as string" :on-click="context?.onIconPrefixClicked as (() => void)" position="prefix" />
2840
<FormKitPrefix v-if="hasPrefix && listStyle === 'span'" :prefix="context?.prefix as string" />
2941
<span v-if="listStyle === 'span'" :id="context?.id" :style="context?.attrs?.style" class="p-output-list-items" :class="context?.attrs?.class">
30-
<template v-for="(value, index) of context?._value" :key="index">
42+
<template v-for="(value, index) of getListValues" :key="index">
3143
<span v-if="index !== 0" class="p-output-list-divider" :class="context?.dividerClass">{{ context?.divider ?? ', ' }}</span>
3244
<span class="p-output-list-item" :class="context?.itemClass">{{ value }}</span>
3345
</template>
3446
</span>
3547
<div v-if="listStyle === 'div'" :id="context?.id" :style="context?.attrs?.style" class="p-output-list-items" :class="context?.attrs?.class">
36-
<template v-for="(value, index) of context?._value" :key="index">
48+
<template v-for="(value, index) of getListValues" :key="index">
3749
<div class="p-output-list-item" :class="context?.itemClass">
3850
{{ value }}
3951
</div>
4052
</template>
4153
</div>
4254
<ul v-if="listStyle === 'ul'" :id="context?.id" :style="context?.attrs?.style" class="p-output-list-items" :class="context?.attrs?.class">
43-
<li v-for="(value, index) of context?._value" :key="index">
55+
<li v-for="(value, index) of getListValues" :key="index">
4456
<span class="p-output-list-item" :class="context?.itemClass">
4557
{{ value }}
4658
</span>
4759
</li>
4860
</ul>
4961
<ol v-if="listStyle === 'ol'" :id="context?.id" :style="context?.attrs?.style" class="p-output-list-items" :class="context?.attrs?.class">
50-
<li v-for="(value, index) of context?._value" :key="index">
62+
<li v-for="(value, index) of getListValues" :key="index">
5163
<span class="p-output-list-item" :class="context?.itemClass">
5264
{{ value }}
5365
</span>

src/definitions/output.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,5 @@ export const primeOutputDurationDefinition: FormKitTypeDefinition = createInput(
3838
})
3939

4040
export const primeOutputListDefinition: FormKitTypeDefinition = createInput(PrimeOutputList, {
41-
props: ['prefix', 'suffix', 'iconPrefix', 'iconSuffix', 'divider', 'itemClass', 'dividerClass', 'listStyle', 'onIconPrefixClicked', 'onIconSuffixClicked'],
41+
props: ['prefix', 'suffix', 'iconPrefix', 'iconSuffix', 'divider', 'itemClass', 'dividerClass', 'listStyle', 'onIconPrefixClicked', 'onIconSuffixClicked', 'convertValue'],
4242
})

0 commit comments

Comments
 (0)