简体   繁体   English

如果我在react-select中使用optionsRenderer,如何使用valueComponent属性来渲染所选项目

[英]How do I use valueComponent prop to render the selected Item if I am using optionsRenderer in react-select

Say I have my react-select component with the following props. 说我有带有以下道具的react-select组件。 I am using pug syntax. 我正在使用哈巴狗语法。

What works 什么有效

My dropdown displays every option correctly with all options custom rendered using the optionsRenderer prop... 我的下拉菜单正确显示了每个选项,并使用optionsRenderer自定义呈现了所有选项。

反应选择选项自定义呈现

What I get(as expected) 我得到的(如预期)

However the results I get are that the input value is just a text I have referenced in my option.label field. 但是我得到的结果是输入值只是我在option.label字段中引用的文本。 In the first option, Custom is my label key. 在第一个选项中,“ Custom是我的标签键。

输入的值是选项中的标签键

What I want to achieve 我想要达到的目标

use the valueComponent to display exactly what the same HTML that options renderer is displaying inside the options dropdown. 使用valueComponent来显示选项下拉菜单中显示的与选项渲染器完全相同的HTML。

Like this, styling can be improved of course :) 这样,样式当然可以改善:)

使用valueComponent呈现的自定义值

... ...

const customOption = (option) => pug`
  div
    strong #{option.label}
    p #{option.renderMethod}

  div
   strong $#{option.otherHTMLData}
`

The function below is expected by the optionRenderer props and iterates on the options supplied. 下面的功能由optionRenderer期望,并在提供的选项上进行迭代。

 const customOption = (option) => pug` div strong #{option.label} p #{option.renderMethod} div strong $#{option.otherHTMLData} ` 

But valueComponent only expects 1 static component. 但是valueComponent只需要1个静态组件。 Therefore any function like () => <span>Will show this</span> 因此,任何类似() => <span>Will show this</span>函数() => <span>Will show this</span>

How do I get the option that is selected to be used as the input value using valueComponent ? 如何使用valueComponent获得选择用作输入值的选项?

The label displayed by the ValueContainer depends on isMutli . ValueContainer显示的标签取决于isMutli If isMulti is false, you control this display by overriding the SingleValue component. 如果isMulti为false,则可以通过覆盖SingleValue组件来控制此显示。 If isMulti is true, you control this display by overriding the MultiValue component (more specifically, it's MultiValueLabel component). 如果isMulti为true,则可以通过重写MultiValue组件(更具体地说,它是MultiValueLabel组件)来控制此显示。

See the Components documentation for some simple examples. 有关一些简单示例,请参见组件文档

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

相关问题 (React-Select 挂钩)如何使用 Select 的 onChange 渲染道具更新 state? - (React-Select hooks) How can I update state using Select's onChange render prop? react-select:如何解决“警告:Prop `id` 不匹配” - react-select: How do I resolve “Warning: Prop `id` did not match” React-Select:如何在选定值 onChange 上更新选定选项下拉列表的默认值? - React-Select: How do I update the selected option dropdown's defaultValue on selected value onChange? optionComponent和valueComponent在最新版本的react-select中不起作用 - optionComponent & valueComponent is not working in react-select in latest version react-select中valueComponent和valueRenderer有什么区别? - What is the difference between valueComponent and valueRenderer in react-select? 我需要更改我正在使用 React-Select 的 React Select 框的宽度 - i need to change the width of React Select box i am using React-Select 如何获得 React-Select 选项的值? - How do I get the value of the React-Select option? 如何禁用 Creatable react-select 组件? - How do I disable a Creatable react-select component? React-select multi select 在没有选择值时更新一个道具 - React-select multi select update a prop when no value is selected 使用自定义组件时如何在 React-Select 中设置所选项目的样式? - How to style the selected item in React-Select when using a custom component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM