[英]How to pass props to custom components in react-select
I am trying to use a custom component as an input field in react-select.我正在尝试使用自定义组件作为 react-select 中的输入字段。 Since I need validation I am trying to use HTML5 oninvalid
( onInvalid
in JSX) for my input tag and set the custom message for oninvalid
.因为我需要验证,所以我尝试使用 HTML5 oninvalid
(JSX 中的onInvalid
)作为我的输入标签,并为oninvalid
设置自定义消息。 However I am unable to pass the message as a prop to the component that I am setting in select.但是我无法将消息作为道具传递给我在选择中设置的组件。 Below is my code.下面是我的代码。
Input.js输入.js
import React from "react";
import { components } from "react-select";
export default class Input extends React.Component {
constructor(props) {
super(props);
}
componentDidMount() {
console.log("component mounted");
}
setInvalidMessage = event => {
event.target.setCustomValidity("Custom Message");
};
render() {
if (this.props.isHidden) {
return <components.Input {...this.props} />;
}
return (
<components.Input
{...this.props}
required
onInvalid={this.setInvalidMessage}
/>
);
}
}
example.js例子.js
import React from "react";
import Input from "./Input";
import Select from "react-select";
import { colourOptions } from "./docs/data";
const InputBoxWithText = props => {
return <Input {...props} />;
};
export default () => (
<form>
<Select
closeMenuOnSelect={true}
components={{ InputBoxWithText }}
options={colourOptions}
/>
<input type="submit" />
</form>
);
If I pass Input in components attribute I get the hard coded message in Input.js.如果我在 components 属性中传递 Input,我会在 Input.js 中得到硬编码消息。 If I pass InputBoxWithText I don't see Input mounting at all.如果我通过 InputBoxWithText,我根本看不到 Input 安装。
index.js索引.js
import React from 'react';
import ReactDOM from 'react-dom';
import Example from './example';
ReactDOM.render(
<Example />,
document.getElementById('root')
);
Here is CodeSandBox.io URL .这是CodeSandBox.io URL 。
Can any one let me know if what am I doing wrong.任何人都可以让我知道我做错了什么。
Not sure if you already found a solution but I managed to pass my custom props using an arrow function不确定您是否已经找到了解决方案,但我设法使用箭头函数传递了我的自定义道具
<Select
closeMenuOnSelect={true}
options={colourOptions}
components={{
Input: (inputProps: InputProps) => (
<components.Input {...inputProps} />
),
}}
/>
It's better to pass custom props via select:最好通过选择传递自定义道具:
props.selectProps
To avoid re-creating of Custom component each time Select updates, what may cause unexpected bugs.避免每次Select更新时都重新创建Custom组件,可能会导致意外的bug。
In my case I was passing errors in such way:在我的情况下,我以这种方式传递错误:
<Select
defaultValue={values}
selectProps={{ errors }}
isMulti
options={inventoryList}
onChange={changeTreeElement}
// @ts-ignore
styles={colourStyles}
/>
Then access it like selectProps.selectProps.errors
in colourStyles methods .然后访问它像selectProps.selectProps.errors
在colourStyles方法。
I don't have the solution (i'm looking for the same thing as well), but you example has multiple errors.我没有解决方案(我也在寻找相同的东西),但是您的示例有多个错误。
To load the input you have to write components={{ Input: InputBoxWithText }}
, since the component name for Input
is not InputBoxWithText
.要加载输入,您必须编写components={{ Input: InputBoxWithText }}
,因为Input
的组件名称不是InputBoxWithText
。
Also the onInvalid
does not seem to be part of the Input
API, so it will never trigger.此外onInvalid
似乎不是Input
API 的一部分,因此它永远不会触发。 Are you trying to use the <input oninvalid="" />
..?您是否尝试使用<input oninvalid="" />
..?
In version 5 the only way to use custom props with typescript is to use module augmentation.在版本 5 中,将自定义道具与打字稿一起使用的唯一方法是使用模块扩充。
So in my project I opened react-app-env.d.ts and added there this:所以在我的项目中,我打开了 react-app-env.d.ts 并添加了这个:
import { GroupBase } from 'react-select'
declare module 'react-select/dist/declarations/src/Select' {
export interface Props<Option, IsMulti extends boolean, Group extends GroupBase<Option>> {
customOnClear: () => void;
}
}
You pass the prop to the select like this:您将 prop 传递给 select,如下所示:
import Select from "react-select";
<Select customOnClear={() => {/* Your custom clear */} />
And use it in your custom component like this:并像这样在您的自定义组件中使用它:
const ClearIndicator = ({ selectProps }: ClearIndicatorProps<Option, false>) => {
const { customOnClear } = selectProps
return <InputClear onClick={customOnClear} />
}
Docs: https://react-select.com/typescript#custom-select-props文档: https ://react-select.com/typescript#custom-select-props
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.