如何使用 sanity.io 在字段中创建默认值?
How to create default value in field using sanity.io?
是否可以使用默认值添加到 sanity 字段?我该如何扩展它?我想用默认变量创建一些字段。例如我有这个代码:
export default {
name: 'name',
title: 'name',
type: 'document',
fields: [
{
name: 'title',
title: 'title',
type: 'string',
validation: Rule => Rule.required(),
},
{
name: 'key',
title: 'key',
type: 'slug',
options: {
source: 'title',
maxLength: 96
}
},
{
name: 'geo',
title: 'geo',
type: 'geopoint',
//default: {"lat": 1,"lng": 2}
},
{
name: 'tel',
title: 'tel',
type: 'string',
//default: '122334554565'
},
],
preview: {
select: {
title: 'title'
}
}
}
您现在可以使用 Initial Value Templates 来执行此操作:
export default {
name: 'name',
title: 'name',
type: 'document',
initialValue: {
tel: 122334554565,
geo: {
_type: 'geopoint',
lat: 1,
lng: 2
}
},
fields: [
{
name: 'title',
title: 'title',
type: 'string',
validation: Rule => Rule.required(),
},
{
name: 'key',
title: 'key',
type: 'slug',
options: {
source: 'title',
maxLength: 96
}
},
{
name: 'geo',
title: 'geo',
type: 'geopoint',
},
{
name: 'tel',
title: 'tel',
type: 'string',
},
],
preview: {
select: {
title: 'title'
}
}
}
是否可以使用默认值添加到 sanity 字段?我该如何扩展它?我想用默认变量创建一些字段。例如我有这个代码:
export default {
name: 'name',
title: 'name',
type: 'document',
fields: [
{
name: 'title',
title: 'title',
type: 'string',
validation: Rule => Rule.required(),
},
{
name: 'key',
title: 'key',
type: 'slug',
options: {
source: 'title',
maxLength: 96
}
},
{
name: 'geo',
title: 'geo',
type: 'geopoint',
//default: {"lat": 1,"lng": 2}
},
{
name: 'tel',
title: 'tel',
type: 'string',
//default: '122334554565'
},
],
preview: {
select: {
title: 'title'
}
}
}
您现在可以使用 Initial Value Templates 来执行此操作:
export default {
name: 'name',
title: 'name',
type: 'document',
initialValue: {
tel: 122334554565,
geo: {
_type: 'geopoint',
lat: 1,
lng: 2
}
},
fields: [
{
name: 'title',
title: 'title',
type: 'string',
validation: Rule => Rule.required(),
},
{
name: 'key',
title: 'key',
type: 'slug',
options: {
source: 'title',
maxLength: 96
}
},
{
name: 'geo',
title: 'geo',
type: 'geopoint',
},
{
name: 'tel',
title: 'tel',
type: 'string',
},
],
preview: {
select: {
title: 'title'
}
}
}