英文:
React-Bootstrap-Typeahead: Using custom input hides selected tokens
问题
我试图使用自定义输入,并且我已经从RBT Rendering example中复制了示例。我的主要目标是为输入添加一个测试标识(test-id),移除提示,以及为输入使用自定义组件。但出现了一个问题,当我从菜单中选择一个选项时,它不会像示例中那样出现在我的自定义输入中。显然,它被设置到状态中,因为它从可用选项中消失了。我是否需要在我的Form.Control
输入周围包装其他内容?
这是一个可工作的沙盒,但为了避免链接失效,我也会粘贴组件代码如下:
<Typeahead
id={`${testIdPrefix}-medal`}
labelKey="medal"
multiple
placeholder="Choose up to 2"
options={[
{ medal: "Gold" },
{ medal: "Silver" },
{ medal: "Bronze" },
{ medal: "Tin" }
]}
selected={multiSelections}
onChange={(selections) => setMultiSelections(selections.slice(-2))}
renderInput={({ inputRef, referenceElementRef, ...inputProps }) => (
<Form.Control
test-id={`${testIdPrefix}-medal`}
{...inputProps}
ref={(node) => {
inputRef(node);
referenceElementRef(node);
}}
/>
)}
renderToken={(option, { onRemove }, index) => (
<Token
key={index}
onRemove={onRemove}
option={option}
className="medal-token"
>
{`${option.medal}`}
</Token>
)}
/>
复现步骤:
- 加载沙盒
- 从菜单中选择一个选项
- 不会出现令牌(但我期望会出现一个)
注意:即使没有自定义的Token
也会出现这个问题,但我包含它以防它与解决方案相关。(我需要解决方案与自定义令牌一起使用。)
英文:
I'm trying to use a custom input and I've copied the example from the RBT Rendering example. My main goals are to add a test-id to the input, to remove the hint, and to use a custom component for the input. For some reason, when I select an option from the menu, it does not appear in my custom input like in the example. It's clearly set into state as it disappears from the available options. Do I need to wrap something else around my Form.Control
input?
Here's a working sandbox, but to avoid link-rot, I'll paste the component code below as well.
<Typeahead
id={`${testIdPrefix}-medal`}
labelKey="medal"
multiple
placeholder="Choose up to 2"
options={[
{ medal: "Gold" },
{ medal: "Silver" },
{ medal: "Bronze" },
{ medal: "Tin" }
]}
selected={multiSelections}
onChange={(selections) => setMultiSelections(selections.slice(-2))}
renderInput={({ inputRef, referenceElementRef, ...inputProps }) => (
<Form.Control
test-id={`${testIdPrefix}-medal`}
{...inputProps}
ref={(node) => {
inputRef(node);
referenceElementRef(node);
}}
/>
)}
renderToken={(option, { onRemove }, index) => (
<Token
key={index}
onRemove={onRemove}
option={option}
className="medal-token"
>
{`${option.medal}`}
</Token>
)}
/>
To reproduce:
- Load the sandbox
- Select an option from the menu
- No token will appear (but I expect one to)
Note: this also happens without the custom Token
but I included it in case that's relevant to the solution. (I need the solution to work with a custom token.)
答案1
得分: 1
Yes, multiple={true}
是问题所在。 renderToken
是一个用于自定义令牌渲染的钩子,但如果使用 renderInput
,它将不会被调用。在这种情况下,您需要自己处理将选择项渲染为令牌的工作。
作为一个快速示例/测试,您应该能够在 renderInput
中简单地在输入框下方渲染选择项:
renderInput={({ inputRef, referenceElementRef, onRemove, selected ...inputProps }) => (
<>
<Form.Control
test-id={`${testIdPrefix}-medal`}
{...inputProps}
ref={(node) => {
inputRef(node);
referenceElementRef(node);
}}
/>
{selected.map((option, idx) => (
<Token
key={idx}
onRemove={onRemove}
option={option}
className="medal-token"
>
{option.medal}
</Token>
))}
</>
)}
英文:
Yes, multiple={true}
is the issue. renderToken
is a hook into the built-in multi-selection component to customize token rendering, but won't be called if you use renderInput
. In that case, you need to handle rendering the selections as tokens yourself.
As a quick example/test, you should be able to simply render the selections below your input in renderInput
:
renderInput={({ inputRef, referenceElementRef, onRemove, selected ...inputProps }) => (
<>
<Form.Control
test-id={`${testIdPrefix}-medal`}
{...inputProps}
ref={(node) => {
inputRef(node);
referenceElementRef(node);
}}
/>
{selected.map((option, idx) => (
<Token
key={idx}
onRemove={onRemove}
option={option}
className="medal-token"
>
{option.medal}
</Token>
))}
</>
)}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论