Reactjs 如何将redux表单的输入道具传递到自定义选择下拉列表
我创建一个自定义选择,而不使用标记和选择选项 因为我想要自定义每个项目的样式(等效选项标记)。 但我也想使用redux表单,我不知道我能做什么,我选择下拉工作与输入道具的redux表单的redux表单控制它Reactjs 如何将redux表单的输入道具传递到自定义选择下拉列表,reactjs,redux-form,Reactjs,Redux Form,我创建一个自定义选择,而不使用标记和选择选项 因为我想要自定义每个项目的样式(等效选项标记)。 但我也想使用redux表单,我不知道我能做什么,我选择下拉工作与输入道具的redux表单的redux表单控制它 const Select = ({options = [], value, selecionou, inputLabel, valueLabel, input}) => { const [listOpen, setListVisibility] = useState(fals
const Select = ({options = [], value, selecionou, inputLabel, valueLabel, input}) => {
const [listOpen, setListVisibility] = useState(false);
const [innerValue, setValue] = useState(value)
const selectItem = o => {
setListVisibility(false)
setValue(o[valueLabel])
selecionou(o[valueLabel])
}
const itens = options.map(o => <li key={o[valueLabel]} onClick={() => selectItem(o)}
className={'select-list-item'}>{o[inputLabel]}</li>)
const getValue = () => {
const opt = options.filter(v => v[valueLabel] === innerValue)[0]
if(opt) return opt[inputLabel]
}
return (
<ClickOutside clickOutside={() => setListVisibility(false)}>
<div className={'select-container'}>
<div className={'input select-header'} onClick={() => setListVisibility(!listOpen)}>
<div className={'select-header-title'}>{innerValue === '' || innerValue == null ? <span className={'placeholder'}>Selecione</span> : getValue()}</div>
<i className={`fas fa-caret-down ${listOpen ? 'down' : 'up'}`} />
</div>
{(listOpen) && <ul className={'select-list'}>{itens}</ul>}
</div>
</ClickOutside>
);
}
export default Select;
const Select=({options=[],value,selecionou,inputLabel,valueLabel,input})=>{
const[listOpen,setListVisibility]=useState(false);
const[innerValue,setValue]=useState(值)
const selectItem=o=>{
setListVisibility(假)
设置值(o[valueLabel])
selecionou(o[valueLabel])
}
const itens=options.map(o=>selectItem(o)}
className={'select-list-item'}>{o[inputLabel]} )
常量getValue=()=>{
const opt=options.filter(v=>v[valueLabel]==innerValue)[0]
if(opt)返回opt[inputLabel]
}
返回(
setListVisibility(false)}>
setListVisibility(!listOpen)}>
{innerValue==''| | innerValue==null?Selecione:getValue()}
{(listOpen)&&{itens}
}
);
}
导出默认选择;
在尝试使用redux表单之前,我可以获取并更改值,但我是redux表单的新手,在文档中搜索引用,但没有找到解决我的问题的方法。在您的还原程序中,您的初始状态需要如下所示
const initialState = {
options: [],
value: '',
selecionou: '',
inputLabel: '', // change to your default values
valueLabel: '',
input: ''
};
您的组件,我已将其重命名为SelectInput
,以便在将状态映射到道具时使用Select
const SelectInput = ({
options = [],
value,
selecionou,
inputLabel,
valueLabel,
input
}) => {
const [listOpen, setListVisibility] = React.useState(false);
const [innerValue, setValue] = React.useState(value);
const selectItem = o => {
setListVisibility(false);
setValue(o[valueLabel]);
selecionou(o[valueLabel]);
};
const itens = options.map(o => (
<li
key={o[valueLabel]}
onClick={() => selectItem(o)}
className={'select-list-item'}
>
{o[inputLabel]}
</li>
));
const getValue = () => {
const opt = options.filter(v => v[valueLabel] === innerValue)[0];
if (opt) return opt[inputLabel];
};
return (
<ClickOutside clickOutside={() => setListVisibility(false)}>
<div className={'select-container'}>
<div
className={'input select-header'}
onClick={() => setListVisibility(!listOpen)}
>
<div className={'select-header-title'}>
{innerValue === '' || innerValue == null ? (
<span className={'placeholder'}>Selecione</span>
) : (
getValue()
)}
</div>
<i
className={`fas fa-caret-down ${listOpen ? 'down' : 'up'}`}
/>
</div>
{listOpen && <ul className={'select-list'}>{itens}</ul>}
</div>
</div>
);
};
在初始状态下,需要如下所示
const initialState = {
options: [],
value: '',
selecionou: '',
inputLabel: '', // change to your default values
valueLabel: '',
input: ''
};
您的组件,我已将其重命名为SelectInput
,以便在将状态映射到道具时使用Select
const SelectInput = ({
options = [],
value,
selecionou,
inputLabel,
valueLabel,
input
}) => {
const [listOpen, setListVisibility] = React.useState(false);
const [innerValue, setValue] = React.useState(value);
const selectItem = o => {
setListVisibility(false);
setValue(o[valueLabel]);
selecionou(o[valueLabel]);
};
const itens = options.map(o => (
<li
key={o[valueLabel]}
onClick={() => selectItem(o)}
className={'select-list-item'}
>
{o[inputLabel]}
</li>
));
const getValue = () => {
const opt = options.filter(v => v[valueLabel] === innerValue)[0];
if (opt) return opt[inputLabel];
};
return (
<ClickOutside clickOutside={() => setListVisibility(false)}>
<div className={'select-container'}>
<div
className={'input select-header'}
onClick={() => setListVisibility(!listOpen)}
>
<div className={'select-header-title'}>
{innerValue === '' || innerValue == null ? (
<span className={'placeholder'}>Selecione</span>
) : (
getValue()
)}
</div>
<i
className={`fas fa-caret-down ${listOpen ? 'down' : 'up'}`}
/>
</div>
{listOpen && <ul className={'select-list'}>{itens}</ul>}
</div>
</div>
);
};
我做了一些不同的事情,但你的例子给了我这个想法。非常感谢你,我做了一些不同的事情,但是你的例子给了我这个想法。非常感谢你