最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to create default value in field using sanity.io? - Stack Overflow

programmeradmin4浏览0评论

Is it possible to add to sanity field with default value? How can I extend it? I want to create some fields with default variables. For example I have this code:

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'
    }
  }
}

Is it possible to add to sanity field with default value? How can I extend it? I want to create some fields with default variables. For example I have this code:

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'
    }
  }
}

Share Improve this question edited Feb 19, 2019 at 20:51 kizoso asked Feb 19, 2019 at 16:00 kizosokizoso 1,2462 gold badges18 silver badges31 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

You can now use Initial Value Templates to do this:

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'
    }
  }
}
发布评论

评论列表(0)

  1. 暂无评论