Skip to content

Select

A select field lets editors pick from a fixed set of options. It stores the key of the chosen option, so you can rename labels without touching content. Use Field.select.multiple to allow picking several.

import {Field} from 'alinea'

Field.select('Category', {
  options: {
    news: 'News',
    release: 'Release notes',
    tutorial: 'Tutorial'
  }
})

Options

  • options (required): an object of option keys and their labels, shown in this order.

  • placeholder: text shown while nothing is selected, for example "Choose a category".

  • initialValue: the key selected for new entries.

  • The common options help, width, inline, required, validate, readOnly, hidden and shared.

Value

  • Field.select stores the selected key, or null when nothing is selected. The TypeScript type is the union of your keys ('news' | 'release' | 'tutorial'), plus null when there's no initialValue.

  • Field.select.multiple stores an array of keys, [] by default.

Labels aren't stored. Keep the options in a constant if you need them on your site too, and don't rename keys that are in use: entries keep the old key.

import {Config, Field} from 'alinea'

export const categories = {
  news: 'News',
  release: 'Release notes',
  tutorial: 'Tutorial'
}

export const Post = Config.document('Post', {
  fields: {
    category: Field.select('Category', {options: categories}),
    tags: Field.select.multiple('Tags', {
      options: {design: 'Design', development: 'Development'}
    })
  }
})

export function categoryLabel(key: keyof typeof categories | null) {
  return key ? categories[key] : ''
}

Filtering

Filter on the key. For multiple selects, use includes:

import {cms} from '@/cms'
import {BlogPost} from '@/schema'

const releases = await cms.find({
  type: BlogPost,
  filter: {tags: {includes: 'release'}}
})

See Filtering for all operators.