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

Is it possible to add to sanity field with default value? How can I extend it? I want to create some fi

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744132316a4559896.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信