JGuang

Picker

Picker · Ant Design Mobile

Preview
npm install antd-mobile@5.43.0

Picker

Displays a scrollable list of one or more option sets.

Install

npm install antd-mobile@5.43.0

antd-mobile is for mobile web, not React Native or Mini Programs. React 19 projects must register createRoot with unstableSetRender at the entry, or imperative APIs such as Toast and Dialog cannot render. Switch dark mode with <html data-prefers-color-scheme="dark">.

Examples come from the official Ant Design Mobile repository (MIT) at the commit published as npm 5.43.0. Presentation helpers such as DemoBlock are JGuang rewrites, not antd-mobile APIs.

Displays a scrollable list of one or more option sets.

When to Use

  • Provides one or more sets of association options for the user to choose from.
  • When there are less than 5 options, it is recommended to tile the options directly, using Radio is a better choice.

The Picker series includes three components: Picker, CascadePicker and DatePicker.

Picker

Demos

Props

type PickerColumnItem = {
  label: ReactNode
  value: string
  key?: string | number
}

type PickerColumn = (string | PickerColumnItem)[]

type PickerValue = string | null

type PickerValueExtend = {
  items: (PickerColumnItem | null)[]
}
NameDescriptionTypeDefault
cancelTextText of the cancel buttonReactNode'取消'
childrenRender function of the selected options(items: PickerColumnItem[], actions: PickerActions) => ReactNode-
closeOnMaskClickWhether to close after clicking the mask layerbooleantrue
columnsOptions to configure each columnPickerColumn[] | ((value: PickerValue[]) => PickerColumn[])-
confirmTextText of the ok buttonReactNode'确定'
defaultValueDefault selected optionsPickerValue[][]
destroyOnCloseDestroy dom when not visiblebooleanfalse
forceRenderRender content forcelybooleanfalse
mouseWheelWhether to allow interact with mouse wheelbooleanfalse
onCancelTriggered when cancelling() => void-
onCloseTriggered when confirming or cancelling() => void-
onConfirmTriggered when confirming(value: PickerValue[], extend: PickerValueExtend) => void-
onSelectTriggered when the options are changed(value: PickerValue[], extend: PickerValueExtend) => void-
popupClassNameThe custom class name of the popupstring-
popupStyleThe custom style of the popupReact.CSSProperties -
renderLabelThe function to custom rendering the label shown on a column(item: PickerColumnItem) => ReactNode(item) => item.label
titleTitleReactNode-
valueSelected optionsPickerValue[]-
visibleWhether to show or hide the Pickerbooleanfalse
loadingShould the Picker displays as loading statebooleanfalse
loadingContentThe loading content displayed in loading stateReactNodeprovide a default SpinLoading content

In addition, the following attributes of Popup are supported: getContainer afterShow afterClose onClick stopPropagation.

Please note that the type of the columns property is a two-level array, the first level corresponds to each column, and the second level corresponds to each option in a column. Therefore, the following writing is wrong:

<Picker
   columns={[
     { label: 'Foo', value: 'foo' },
     { label: 'Bar', value: 'bar' },
   ]}
/>

need to be written as:

<Picker
   columns={[
     [
       { label: 'Foo', value: 'foo' },
       { label: 'Bar', value: 'bar' },
     ]
   ]}
/>

PickerActions

NameDescriptionType
closeClose Picker.() => void
openOpen Picker.() => void
toggleToggle the visible state of Picker.() => void

Ref

Same as PickerActions.

CSS Variables

NameDescriptionDefaultGlobal
--header-button-font-sizeFont size of confirm and cancel button.15px-
--item-font-sizeFont size of option items.16px-
--item-heightHeight of option item. Only supports px rem and vw units.34px-
--title-font-sizeFont size of title.15px-

CascadePicker

Demos

Props

type CascadePickerOption = {
  label: string
  value: string
  children?: CascadePickerOption[]
}
NameDescriptionTypeDefault
optionsData of the tree optionsCascadePickerOption[]-

Other props are the same as Picker, but columns are not supported.

Ref

Same as Picker.

CSS Variables

Same as Picker.

DatePicker

Demos

Props

type PickerDate = Date & {
  tillNow?: boolean
}
NameDescriptionTypeDefaultVersion
childrenThe rendering function of the selected items(value: PickerDate, actions: PickerActions) => ReactNode-
defaultValueDefault selected valuePickerDate-
destroyOnCloseDestroy dom when not visiblebooleanfalse
filterFilter available timeDatePickerFilter-
forceRenderRender content forcelybooleanfalse
maxMax valuePickerDateten years later
minMinimum valuePickerDateten years ago
mouseWheelWhether to allow interact with mouse wheelbooleanfalse
onConfirmTriggered when confirming(value: PickerDate) => void-
onSelectTriggered when the options are changed(value: PickerDate) => void-
precisionPrecision'year' | 'month' | 'day' | 'hour' | 'minute' | 'second' | 'week' | 'week-day' | 'quarter''day'
renderLabelThe function to custom rendering the label shown on a column. type means any value in precision or now, data means the default number(type: Precision | 'now', data: number, info: { selected: boolean }) => ReactNode-5.40.0
tillNowShow till now in listboolean-5.27.0
valueSelected valuePickerDate-
type DatePickerFilter = Partial<
  Record<
    Precision,
    (
      val: number,
      extend: {
        date: Date
      }
    ) => boolean
  >
>

In addition, the following attributes of Picker are supported: onCancel onClose closeOnMaskClick visible confirmText cancelText getContainer afterShow afterClose onClick title stopPropagation loading loadingContent

Ref

Same as Picker.

CSS Variables

Same as Picker.

Imperative Call

Picker supports Imperative Call and provides the prompt method:

prompt: (props: Omit<PickerProps, 'value' | 'visible' | 'children'>) => Promise<PickerValue[] | null>

The return value of the prompt method is a Promise. If the user clicks OK, the Promise can be resolved to PickerValue[], and if the user triggers the cancellation operation, the value in the Promise is null. You can get the value through await or .then():

const value = await Picker.prompt({
  columns: yourColumnsConfig,
})
Picker.prompt({
  columns: yourColumnsConfig,
}).then((value) => {
  // ...
})

Similarly, CascadePicker and DatePicker also support the prompt method, the specific usage will not be repeated here.

FAQ

How to highlight the selected items?

The DOM element of selected items has a data-selected="true" attribute. You can use this mark to add custom CSS styles to selected items.

Why is the name of the component called "DatePicker" instead of "DatetimePicker"?

Because the type of value is Date.

Why is there no "hour-minute" or "month-day" option?

Different from the Picker component, the value type of the DatePicker is a Date object, so it needs to be selected from the beginning of the year. Only the parameters of hour and minutes cannot construct a Date object.

If you need to use DatePicker like that, you can implement it yourself through the Picker component.