Custom field input
11/19/26About 1 min
Custom field input
Use this when the built-in field inputs (textarea, select, autocomplete, etc.) do not cover your use case.
Steps
1. Create the Vue component
<!-- src/components/RatingInput.vue -->
<script setup lang="ts">
import { computed } from 'vue';
const props = defineProps({
modelValue: { type: Number, default: null },
appliedOptions: { type: Object, default: () => ({}) },
});
const emit = defineEmits(['update:modelValue']);
const rating = computed({
get: () => props.modelValue,
set: (v) => emit('update:modelValue', v),
});
</script>
<template>
<div class="rating-input">
<button
v-for="n in (appliedOptions.max ?? 5)"
:key="n"
:class="{ active: rating >= n }"
type="button"
@click="rating = n"
>
{{ n }}
</button>
</div>
</template>The component receives:
modelValue— the current field value (v-model)appliedOptions— thefieldInput.optionsobject fromresource.json
Emit update:modelValue to write back to the form.
2. Register in CroutonPlugin
// src/custom-components.ts
import { customComponentIs, type CustomComponentEntry } from '@ghentcdh/crouton-vue';
import { markRaw } from 'vue';
import RatingInput from './components/RatingInput.vue';
export const customComponents: CustomComponentEntry[] = [
{
tester: customComponentIs('rating', 10),
renderer: markRaw(RatingInput),
},
];// src/main.ts
import { CroutonPlugin } from '@ghentcdh/crouton-vue';
import { customComponents } from './custom-components';
app.use(CroutonPlugin(apiUrl, { customComponents }));3. Reference from resource.json
Set fieldInput.type to "custom" and fieldInput.options.customComponent to the name you passed to customComponentIs. Pass any component-specific config under fieldInput.options too:
"rating": {
"label": "Rating",
"fieldInput": {
"type": "custom",
"options": {
"customComponent": "rating",
"max": 5
}
}
}4. Validate
npx crouton validate resources resources/book/resource.jsoncrouton validate does not check that a component name is registered (that is a runtime concern), but it will catch typos in the field shape.
Complete example
Full resource.json for a resource with a custom rating field:
{
"$schema": "https://ghentcdh.github.io/crouton/schema/v1/resource.schema.json",
"schemaVersion": 1,
"name": "book",
"source": {
"model": "Book"
},
"title": "Books",
"columns": {
"id": {
"idField": true,
"hiddenInForm": true
},
"title": {
"sortable": true
},
"rating": {
"label": "Rating",
"hiddenInTable": true,
"fieldInput": {
"type": "custom",
"options": {
"customComponent": "rating",
"max": 5
}
}
}
},
"operations": {
"findAll": {
"search": [
"title"
]
}
}
}Custom table cell renderer
To replace only the table cell (not the form field), use fieldTable.options.customComponent instead:
"rating": {
"label": "Rating",
"fieldInput": { "type": "custom", "options": { "customComponent": "rating" } },
"fieldTable": {
"options": { "customComponent": "RatingStars" }
}
}Register RatingStars the same way as above. See Custom renderers for the full API.