[英]Adding <textarea> tag inside a <select> tag after clicking an option on React.js
So basically, I got the following JSX file and I want to have an event handler that listens to an event that is generated after clicking "Other" in the manufacturer section. 因此,基本上,我得到了以下JSX文件,并且我想要一个事件处理程序,该事件处理程序侦听在制造商部分中单击“其他”后生成的事件。 Please find the mentioned option as the last
<option>
tag inside the <select>
tag with the name "colour", and inside the <Fragment>
tag. 请在
<select>
标记内名为“ colour”的最后一个<option>
标记内以及<Fragment>
标记内找到上述选项。 Also, please ignore the OnClick function that is already there; 另外,请忽略已经存在的OnClick函数。 it has no use in this problem.
它在这个问题上没有用。
The event that I want to be generated is quite particular. 我想生成的事件非常特殊。 It should add a text area (or input, not entirely sure) where the "Other" option inside the drop-down menu is located so that the user is able to type the other option they didn't find in the current selection of colours.
它应该在下拉菜单内的“其他”选项所在的位置添加一个文本区域(或输入,不确定),以便用户可以键入在当前颜色选择中找不到的其他选项。
How would I be able to achieve this? 我将如何实现这一目标?
import React, { Fragment} from 'react';
import { StyledSection, StyledSectionColLeft, StyledSectionCol } from '../../../common/theme/cssStyledColumns';
import { Textarea, Select, Input, MandatoryInfo } from '../../../common/form';
const ApplianceDetails = () => (
<Fragment>
<h2>Appliance details</h2>
<MandatoryInfo />
<StyledSection>
<StyledSectionColLeft>
<Select name="colour" label="Colour *">
<option value="">-Select-</option>
<option value="Vokera">Blue</option>
<option value="Warmflow">Red</option>
<option value="Worcester">Yellow</option>
<option onClick={console.log('Click happened')} value="Other">Other</option>
</Select>
<Input name="model" label="Model *" />
</StyledSectionColLeft>
<StyledSectionCol>
<Input name="location" label="Location *" />
</StyledSectionCol>
</StyledSection>
</Fragment>
);
export default ApplianceDetails;
You can set a class on an input
/ textarea
based on the value of select
. 您可以基于
select
的值在input
/ textarea
上设置类。 Like this: 像这样:
<Select value={this.state.selectedOption} onChange={this.handleSelect} name="colour" label="Colour *">
<option value="">-Select-</option>
<option value="Vokera">Blue</option>
<option value="Warmflow">Red</option>
<option value="Worcester">Yellow</option>
<option value="Other">Other</option>
</Select>
// Or textarea
<input type="text" value="" name=" className={this.state.selectedOption !== 'Other'?"hidden":""}/>
This adds a class of hidden
to your input. 这为您的输入添加了一个
hidden
类。 Now you can use .hidden{ diplay:none; }
现在您可以使用
.hidden{ diplay:none; }
.hidden{ diplay:none; }
And this function will handle change of your values: 此函数将处理您的值更改:
handleSelect(evt){
this.setState({
selectedOption: evt.target.value
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.