简体   繁体   English

react-select:在多选下拉菜单中预先选择一个值

[英]react-select: pre-select a value in multi-select dropdown

I've inherited my first react app, and I need to make a change that looks VERY straight forward. 我继承了我的第一个react应用程序,我需要做一个看起来非常简单的更改。

The change is on an email signup page, where user has a multi-select dropdown menu to indicate what they're interested in hearing about. 更改位于电子邮件注册页面上,在该页面上,用户具有多选下拉菜单,以表明他们有兴趣了解的内容。 My simple goal: make one of the items in the dropdown menu appear pre-selected. 我的简单目标:使下拉菜单中的一项显示为预选状态。 (And ultimately I'll pass along a value so it's only selected sometimes). (最终,我会传递一个值,因此有时只选择它)。 In this example, the list values/labels come from DESSERT_TYPES, and I want to pre-select one of the values in that list: 'Sheet cake'. 在此示例中,列表值/标签来自DESSERT_TYPES,我想预选该列表中的值之一:“ Sheet cake”。

Reading through react-select docs/examples, it looks like I just need to provide a value prop, but I've tried adding different variations of value= 'Sheet cake' , value={{label: "Sheet cake", value: "Sheet cake"}} , etc in the Field definition, in ReactSelectAdapter, or in the class constructor (with a colon instead of =), and nothing seems to work: the menu still just shows the placeholder text with nothing selected. 仔细阅读react-select docs / examples,看来我只需要提供一个值道具,但我尝试添加value= 'Sheet cake'value={{label: "Sheet cake", value: "Sheet cake"}}在字段定义,ReactSelectAdapter或类构造函数中(用冒号而不是=)的value={{label: "Sheet cake", value: "Sheet cake"}}等,似乎没有任何作用:菜单仍只显示未选择任何内容的占位符文本。 I also tried using defaultOptions and loadOptions, but no luck there either. 我也尝试使用defaultOptions和loadOptions,但也没有运气。

All code given below is in the same EmailSignup.js file. 下面给出的所有代码都在同一个EmailSignup.js文件中。

The rendered bit in question is: 有问题的渲染位是:

<div className="email-modal-input-wrap">
  <label className="email-modal-label" htmlFor="interest-input">
    Please select your interest from the list below:
  </label>
  <Field
    name="interest"
    component={ ReactSelectAdapter }
    options={ DESSERT_TYPES }
    placeholder="I'm interested in a project about..."
    isMulti
    value= 'Sheet cakes'
    id="interest-input"
    />
  <label className="email-modal-hidden-label" htmlFor="interest-input">Interest</label>
</div>

That ReactSelectAdapter component is defined this way: 该ReactSelectAdapter组件是通过以下方式定义的:

const ReactSelectAdapter = ({ input, ...rest }) => (
  <Creatable
    { ...input }
    { ...rest }
    styles={DROPDOWN_STYLES}
    classNamePrefix="react-select"
    searchable
  />
)

The constructor for the EmailSignup class has: EmailSignup类的构造函数具有:

class EmailSignup extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      submitted: false,
      success: 'hidden',
      error: 'hidden',
      existing: 'hidden',
      modalIsOpen: true,
      location: undefined
    }

The includes are: 包括:

import React from 'react'
import Modal from 'react-modal'
import { Form, Field } from 'react-final-form'
import axios from 'axios'
import ProjectLogoList from './ProjectLogoList'
import PropTypes from 'prop-types'
import { PROJECT_DATA } from '../Utils/data/ProjectData'
import { DESSERT_TYPES } from '../Utils/data/dessertTypes'
import Creatable from 'react-select/lib/Creatable'
import ReactGA from 'react-ga';

What could I be missing here? 我在这里可能想念什么?

To set the initial value pass an object to the value prop: 要设置初始值,请将一个对象传递给value prop:

<Creatable
    // ...other props
    options={DESSERT_TYPES}
    value={{label: "Cake", value: "cake"}}
/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM