William Iommi
Junior/Senior/Lead Frontend @ Syskoplan CX IT
William is located at Milan, Italy
A dynamic group filled with fields that don't have any defined group.
import {DocumentDefinition, FieldGroupDefinition, ObjectDefinition} from 'sanity'
import {LinkRemovedIcon} from '@sanity/icons'
// this is the definition of our custom group for unmapped fields
const unmappedFieldsGroup: FieldGroupDefinition = {
name: 'unmapped-fields',
icon: LinkRemovedIcon,
title: 'Unmapped Fields',
}
type DocumentObjectDefinitionType = DocumentDefinition | ObjectDefinition
export const enhancedGroupsDefinition = (definitions: DocumentObjectDefinitionType[]): DocumentObjectDefinitionType[] => {
const doMagic = (def: DocumentObjectDefinitionType) => {
// Check if your document/object has any groups defined.
if (def.groups && Array.isArray(def.groups) && def.groups.length > 0) {
// Check if every field has a group defined.
const everyFieldsHaveGroup = !!def.fields.every(
(field) =>
typeof field.group === 'string' || (Array.isArray(field.group) && field.group.length > 0)
)
// If some fields don't have a group, we can add our custom group.
if (!everyFieldsHaveGroup) {
// Add our custom group at the end of the list.
def.groups.push(unmappedFieldsGroup)
// Loop through all fields and add the unmapped group if a group is not defined.
def.fields = def.fields.map((field) => {
if (!field.group || (Array.isArray(field.group) && field.group.length === 0))
field.group = unmappedFieldsGroup.name
return field
})
}
}
return def
}
return definitions.map(doMagic)
}
import {enhancedGroupsDefinition} from '../../some-utility-method-file-blah-blah'
defineConfig({
// your configuration
schema: {
types: [
...enhancedGroupsDefinition([Document1, Document2]),
],
},
})
This is a small customization that allows you to have a dynamic group that only contains fields without an associated group. You can also choose to apply this customization only to a subset of documents.
For some workarounds to remove the 'all fields' tab, you can refer to this GitHub discussion.
Junior/Senior/Lead Frontend @ Syskoplan CX IT
A little 'hack' when you work on custom Document actions
Go to 'Hacking' custom Document Actions 😇A custom validation to check if your Microcopy documents have unique keys and values for a specific namespace.
Go to Is your Microcopy unique?Simple custom validation that force array length according to another field.
Go to Custom validation on array length