reactjs - 如何使用 react-select 设置公司名称?
问题描述
伙计们,任何人都可以帮助我实现 react-select,似乎缺少一些验证,因为当我执行 console.log() 时数据没有显示为值。预先感谢,React 总是一个复杂的话题。这是文本输入
import React from 'react';
import TextField from '@material-ui/core/TextField';
import InputLabel from '@material-ui/core/InputLabel';
import '../../style/TextInput.scss';
const TextInput = (props) => {
// Parsing props
const {
value,
setValue,
placeholder,
variant = 'standard', // Can be standard, filled or outlined
label,
showLabel,
inputClassName = '',
labelClassName = '',
containerClassName = '',
fullWidth = false,
indexName,
type = 'text',
inputProps = {},
error = false,
errorText,
onBlur,
} = props;
// Handling input change
const handleChange = (event) => {
const newKey = indexName || label;
const { target } = event;
if (!target) { setValue(''); return; }
const { value: newValue } = target;
setValue(newKey, newValue);
};
// Rendering input label
const renderLabel = () => {
if (!showLabel) { return null; }
return (
<InputLabel
variant="standard"
className={`text-input-label ${labelClassName}`}
>
{label}
</InputLabel>
);
};
// Rendering
return (
<div className={containerClassName}>
{renderLabel()}
<TextField
name={indexName}
value={value}
onChange={handleChange}
placeholder={placeholder}
variant={variant}
className={`text-input ${inputClassName}`}
color="primary"
notched="false"
fullWidth={fullWidth}
type={type}
InputProps={inputProps}
error={error}
helperText={errorText || ''}
onBlur={onBlur}
/>
</div>
);
};
export default TextInput;
这是选择组件
import React, { useState } from 'react';
import Select from 'react-select';
import InputLabel from '@material-ui/core/InputLabel';
import '../../style/SelectInput.scss';
const options = [
{ id: 'o1', value: 'Bank', label: 'Bank' },
{ id: 'o2', value: 'Insurance Co.', label: 'Insurance Co.' },
{ id: 'o3', value: 'REIT', label: 'REIT' },
{ id: 'o4', value: 'Hedge Fund', label: 'Hedge Fund' },
{ id: 'o5', value: 'Investment Bank', label: 'Investment Bank' },
{ id: 'o6', value: 'Family Office', label: 'Family Office' },
{ id: 'o7', value: 'Other', label: 'Other' },
];
const SelectField = (props) => {
const [selectedOption, setSelectedOption] = useState('none');
// Parsing props
const {
placeholder,
variant = 'standard', // Can be standard, filled or outlined
label,
showLabel,
inputClassName = '',
labelClassName = '',
containerClassName = '',
fullWidth = false,
indexName,
type = 'select',
inputProps = {},
error = false,
errorText,
onBlur,
} = props;
const handleChange = (event) => {
setSelectedOption(event.value);
};
const renderLabel = () => {
if (!showLabel) { return null; }
return (
<InputLabel
variant="standard"
className={`select-input-label ${labelClassName}`}
>
{label}
</InputLabel>
);
};
return (
<div className={containerClassName}>
{renderLabel()}
<Select
options={options}
name={indexName}
value={options.filter((option) => option.value === selectedOption)}
onChange={handleChange}
placeholder={placeholder}
variant={variant}
className={`select-input ${inputClassName}`}
color="primary"
notched="false"
fullWidth={fullWidth}
type={type}
InputProps={inputProps}
error={error}
helperText={errorText || ''}
onBlur={onBlur}
label="Single select"
key={options.id}
/>
</div>
);
};
export default SelectField;
解决方案
推荐阅读
- python - 在列名之前将标题添加到 df
- excel - 计算“修剪”加权平均值
- python - ImportError:没有名为“tensorflow.python”的模块;'tensorflow' 不是一个包
- aws-api-gateway - 我可以通过 ACM 私有证书使用 VPC 链接到 NLB 吗?
- c# - ASP.NET Core Identity - 使令牌(电子邮件确认、密码重置等)在不同服务器上有效?
- c# - 如何与警报 Sur-Gard System 1 通信
- javascript - 从 unix 时间戳获取 ISOString(以微秒为单位)
- vue.js - 当我尝试在 VueJS 中加载组件时,为什么会出现此错误?
- spring-mvc - 使用 Spring 5.2 删除 eot 字体的“Vary”标头
- vue.js - vue-cli 与 vuetify 项目无法运行 IE 11 polyfilling 问题?