Custom Fields
The built-in field types cover most content. When editors need a different input, like a color picker or a star rating, a custom field type adds one. You write the input with plain DOM, Vue, React or any other library, and Tenon stores the value like any other field. This page shows how to add one.
Add a custom field
Use the type
A field of type custom:<name> uses the input you register under <name>:
export const products = defineCollection({
fields: {
title: { type: "text", required: true },
color: { type: "custom:color", label: "Color" },
},
});
Write the input
Write the input in plain DOM, or as a Vue or React component. It gets the field's context, with the value and onChange:
// src/admin/color.client.ts
import type { FieldContext, Mount } from "tenoncms/admin";
export const mountColor: Mount<FieldContext> = (el, { value, field, onChange }) => {
const input = Object.assign(document.createElement("input"), {
type: "color",
value: value ?? "#000000",
disabled: field.readOnly,
});
input.oninput = () => onChange(input.value);
el.append(input);
return () => input.remove();
};
<!-- src/admin/ColorInput.vue -->
<script setup lang="ts">
import type { FieldContext } from "tenoncms/admin";
defineProps<FieldContext>();
</script>
<template>
<input
type="color"
:value="value ?? '#000000'"
:disabled="field.readOnly"
@input="onChange(($event.target as HTMLInputElement).value)"
/>
</template>
// src/admin/ColorInput.tsx
import type { FieldContext } from "tenoncms/admin";
export function ColorInput({ value, field, onChange }: FieldContext) {
return (
<input
type="color"
value={value ?? "#000000"}
disabled={field.readOnly}
onChange={(event) => onChange(event.target.value)}
/>
);
}
In plain DOM, the input is a mount function: it gets an empty element and the context, and returns a cleanup. It lives in a module whose name ends in .client.ts, so it only runs in the browser. See Browser modules.
Register it
A component needs one more line: renderVue() or renderReact() turns it into the mount function, in a .client.ts module:
// src/admin/color.client.ts
import { renderVue } from "tenoncms/admin/vue";
import ColorInput from "./ColorInput.vue";
export const mountColor = renderVue(ColorInput);
// src/admin/color.client.ts
import { renderReact } from "tenoncms/admin/react";
import { ColorInput } from "./ColorInput";
export const mountColor = renderReact(ColorInput);
The components run with your own vue or react-dom, so your framework compiles them. The admin brings its own Vue, and you install nothing extra. See Vue, React and your framework.
Then import the input in your config, and register it under the name:
import { mountColor } from "./src/admin/color.client";
export default defineCms({
// ...
admin: {
fields: { color: { mount: mountColor } },
},
});
The admin now shows a color picker for color. The site reads the value as a string, like "#ff6600".
What the admin does
- It renders the label and the description. Your input only renders the control.
- It saves what you pass to
onChangewith the document. - A custom field stores a string. Without a registered input, the admin shows a text input.
- The admin's
.inputand.btnclasses style your markup like the built-in inputs. See Styles and UI libraries.
The input gets this context:
| Key | Value |
|---|---|
name | The field name, like color |
field | type, label, required and readOnly from the config |
value | The current value, or null / undefined when empty |
onChange | Sets the value |
data | What the field's load returned. undefined without one. |
tenon | Helpers from the admin, like locale |
The Vue and React components get new props on every change. The plain DOM input renders once. To follow a new value there, return { update, unmount }. See Build a custom field. Other libraries, like Svelte, work the same way: mount into the element, and unmount in the cleanup.
Related resources
- Build a custom field: updates, server data, translations and a description that follows the value.
- Fields: the built-in field types.
- Custom pages and actions: your own pages in the admin.
- Browser modules:
.client.tsmodules andclientRef().