
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeYes, absolutely! You can add a "generate" button to your shortID string field by creating a custom input component. This is a perfect use case for custom input components in Sanity Studio.
Here's how to implement it:
In your schema, add a custom input component to your shortID field:
import {Button, Stack, TextInput} from '@sanity/ui'
import {set, unset} from 'sanity'
const ShortIDInput = (props) => {
const {elementProps, onChange, value} = props
const generateShortID = () => {
// Your ID generation logic here
const newID = Math.random().toString(36).substring(2, 9)
onChange(set(newID))
}
return (
<Stack space={3}>
<TextInput
{...elementProps}
value={value || ''}
onChange={(event) =>
onChange(event.currentTarget.value ? set(event.currentTarget.value) : unset())
}
/>
<Button
text="Generate ID"
tone="primary"
onClick={generateShortID}
/>
</Stack>
)
}
// In your schema:
{
name: 'shortID',
type: 'string',
components: {
input: ShortIDInput
},
initialValue: () => Math.random().toString(36).substring(2, 9)
}For a more production-ready version that includes proper spacing and respects read-only states:
import {Button, Stack, TextInput} from '@sanity/ui'
import {set, unset} from 'sanity'
const ShortIDInput = (props) => {
const {
elementProps,
onChange,
value = '',
readOnly
} = props
const generateShortID = () => {
// Use your preferred ID generation method
const newID = Math.random().toString(36).substring(2, 9)
onChange(set(newID))
}
return (
<Stack space={3}>
<TextInput
{...elementProps}
value={value}
readOnly={readOnly}
onChange={(event) =>
onChange(event.currentTarget.value ? set(event.currentTarget.value) : unset())
}
/>
<Button
text="Generate New ID"
tone="primary"
onClick={generateShortID}
disabled={readOnly}
mode="ghost"
/>
</Stack>
)
}The key points from the custom input components documentation:
onChange prop with set() to update the field valueunset() when clearing the valuevalue, onChange, readOnly, and other useful propsStack from @sanity/ui for proper spacing between elementsreadOnly state to disable the button when appropriateYou can customize the ID generation logic to match your needs - whether that's using a library like nanoid, shortid, or your own custom algorithm for creating URL-friendly unique identifiers. Since you already have it working as an initialValue, you can reuse that same generation logic in your generate button handler!
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store