sanity-io / language-filter

A Sanity plugin that supports filtering localized fields by language
MIT License
17 stars 1 forks source link

Type '{ languageFilter: true; }' is not assignable to type 'EnumListProps<string>'. #32

Closed kapyar closed 1 year ago

kapyar commented 1 year ago

What do I do wrong. can turn on localization for field I have the following config

import {MdStar as icon} from 'react-icons/md'
import {defineField, defineType} from 'sanity'
import {LanguageFilterOptions} from '@sanity/language-filter'

export default defineType({
  name: 'ganre',
  title: 'Ganre',
  type: 'document',
  icon,
  fields: [
    defineField({
      name: 'name',
      title: 'Ganre',
      type: 'string',
      validation: (Rule) => Rule.required(),
    }),
    defineField({
      name: 'localeName',
      title: 'Locale',
      type: 'string',
      options: {
        languageFilter: true, // PROBLEM HERE
      },
    }),

    defineField({
      name: 'isFeatured',
      title: 'Is Featured',
      type: 'boolean',
      validation: (Rule) => Rule.required(),
    }),
    defineField({
      name: 'slug',
      title: 'Slug',
      type: 'slug',
      options: {
        source: 'name',
        maxLength: 100,
      },
    }),
    defineField({
      name: 'description',
      title: 'Description',
      type: 'string',
      validation: (Rule) => Rule.required(),
    }),
    defineField({
      name: 'image',
      title: 'Image',
      type: 'image',
      validation: (Rule) => Rule.required(),
      options: {
        hotspot: true,
      },
    }),
  ],
  initialValue: {
    isFeatured: false,
  },
})
kapyar commented 1 year ago

For those who will face the same problem. You need to define localized type on your own, @sanity/language-filter only prides filter. So for example if you want to have localized string you need to do following steps:

  1. Define localeString type

export default defineType({ name: 'localeString', title: 'Locale String', type: 'object', fields: [ { title: 'English', name: 'en', type: 'string', }, { title: 'Ukrainian', name: 'uk', type: 'string', }, ], })

2. Use this field in where you need it

defineField({ name: 'localeName', title: 'Localized String', type: 'localeString', options: { languageFilter: true, }, }),