简体   繁体   English

react-bootstrap 如何使用自定义表单控件组件

[英]react-bootstrap how to use custom form control component

I need to add select input in my form, but there are too many options, so the default way to do it looks really ugly(我需要在我的表单中添加选择输入,但是选项太多,所以默认的方式看起来真的很难看(

<Form.Control as="select">
    {props.options.map(
        (o) => <option>{o}</option>
    )}
</Form.Control>

So I've decided to use react-bootstrap-typeahead because it's already being used in my project and it supports search.所以我决定使用react-bootstrap-typeahead因为它已经在我的项目中使用并且它支持搜索。 However, it didn't work properly in that case.但是,在这种情况下它不能正常工作。

<Form>
    <Form.Group>
        <Form.Label>Provider</Form.Label>
        <Form.Control
            as={Typeahead}
            id="id"
            options={props.options}
         />
    </Form.Group>
</Form>

It looks like this:它看起来像这样: 在此处输入图片说明

Might I've done it not properly, but I can't figure out how to do it in another way(可能我做得不对,但我不知道如何以另一种方式做到这一点(

https://react-bootstrap.github.io/components/forms/#form-control-props https://react-bootstrap.github.io/components/forms/#form-control-props

Ther has a props "as".那里有一个道具“as”。 See exemle bellow.见下面的例子。

export default function Input(props) { 

 const CustomFormControl = React.forwardRef(({ children, onChange}, ref) => (
    <>
     <p>Insert your elements for yout form</p>
     <p>{props.test}</p>
    </>
  ));

return (
    <>
        <Form.Control
        as = {CustomFormControl}
        test = {"hellow world"}
        /** and here u can inser the props you need **/
        />
    </>

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM