I need to create a custom renderer for phone numbers with two inputs, but then when saving the data, they need to be joined together in a string. Is there a specific way to do this?
So far, what I have is:
<template>
<div>
<v-combobox v-model="countryCode"
:items="COUNTRY_CODES"
item-title="value"
placeholder="Country Code"
@change="input" />
<v-text-field v-model="phoneNumber" placeholder="Phone Number" @input="input" />
</div>
</template>
<script lang="ts">
import { ControlElement, JsonFormsRendererRegistryEntry, rankWith, scopeEndsWith } from '@jsonforms/core'
import { RendererProps, rendererProps, useJsonFormsControl } from '@jsonforms/vue'
import { defineComponent } from 'vue'
import { useVanillaControl } from '@jsonforms/vue-vanilla/src/util'
import { COUNTRY_CODES } from './constants'
const phoneNumberRenderer = defineComponent({
name: 'phone-number-renderer',
props: {
...rendererProps<ControlElement>(),
},
emits: ['input'],
setup (props: RendererProps<ControlElement>) {
return {
COUNTRY_CODES,
...useVanillaControl(useJsonFormsControl(props), target => target.value || undefined),
}
},
data () {
return {
countryCode: '' as string,
phoneNumber: '' as string,
}
},
methods: {
input () {
if (!this.countryCode || !this.phoneNumber) {
return
}
this.$emit('input', `${this.countryCode}${this.phoneNumber}`)
},
},
})
export default phoneNumberRenderer
export const entry: JsonFormsRendererRegistryEntry = {
renderer: phoneNumberRenderer,
tester: rankWith(3, scopeEndsWith('phone')),
}
</script>
The emit
is not working, so probably I need to use something specific, like the onChange
, but not sure how to make it work as it requires an Event
as the parameter.
Thanks in advance!