|
1 |
| -# react-select-with-custom-input |
| 1 | +Responsive React Select with custom user input built with Bootstrap 5. Click on the Other option to input a custom value into the select. |
| 2 | + |
| 3 | +Check out [React Bootstrap Select With Custom Input Documentation](https://mdbootstrap.com/docs/react/extended/select-with-custom-input/) for detailed instructions & even more examples. |
| 4 | + |
| 5 | +## Basic example |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +```js |
| 10 | +import React, { useCallback, useEffect, useMemo, useRef, useState } from "react"; |
| 11 | +import { MDBContainer, MDBInput, MDBSelect } from "mdb-react-ui-kit"; |
| 12 | + |
| 13 | +export default function App() { |
| 14 | + const data = useMemo( |
| 15 | + () => [ |
| 16 | + { text: "One", value: 1 }, |
| 17 | + { text: "Two", value: 2 }, |
| 18 | + { text: "Three", value: 3 }, |
| 19 | + { text: "Four", value: 4 }, |
| 20 | + { text: "Other", value: 5 }, |
| 21 | + ], |
| 22 | + [] |
| 23 | + ); |
| 24 | + |
| 25 | + const [currentValue, setCurrentValue] = useState(data[0]); |
| 26 | + const [inputActive, setInputActive] = useState(false); |
| 27 | + |
| 28 | + const otherInputEl = useRef(null); |
| 29 | + |
| 30 | + const inputOnBlur = useCallback((e) => { |
| 31 | + if (!e.target.value) { |
| 32 | + setInputActive(false); |
| 33 | + setCurrentValue(data[data.length - 1]) |
| 34 | + } |
| 35 | + }, []); |
| 36 | + |
| 37 | + useEffect(() => { |
| 38 | + console.log(currentValue); |
| 39 | + const lastIndex = data.length - 1; |
| 40 | + if (currentValue.value == data[lastIndex].value) { |
| 41 | + setInputActive(true); |
| 42 | + setTimeout(() => { |
| 43 | + otherInputEl.current?.focus(); |
| 44 | + }); |
| 45 | + } else { |
| 46 | + setInputActive(false); |
| 47 | + } |
| 48 | + }, [currentValue]); |
| 49 | + |
| 50 | + return ( |
| 51 | + <MDBContainer style={{ width: "300px" }} className="mt-5"> |
| 52 | + {inputActive ? ( |
| 53 | + <MDBInput onBlur={inputOnBlur} inputRef={otherInputEl} label="Other" id="form1" type="text" /> |
| 54 | + ) : ( |
| 55 | + <MDBSelect |
| 56 | + onValueChange={(e) => setCurrentValue(e)} |
| 57 | + label="Example label" |
| 58 | + data={data} |
| 59 | + /> |
| 60 | + )} |
| 61 | + </MDBContainer> |
| 62 | + ); |
| 63 | +} |
| 64 | +``` |
| 65 | +
|
| 66 | +
|
| 67 | +## How to use? |
| 68 | +
|
| 69 | +1. Download MDB React - free UI KIT |
| 70 | +
|
| 71 | +2. Choose your favourite customized component and click on the image |
| 72 | +
|
| 73 | +3. Copy & paste the code into your MDB project |
| 74 | +
|
| 75 | +[▶️ Subscribe to YouTube channel for web development tutorials & resources](https://www.youtube.com/MDBootstrap?sub_confirmation=1) |
| 76 | +
|
| 77 | +___ |
| 78 | +
|
| 79 | +## More extended Bootstrap documentation |
| 80 | +
|
| 81 | +<ul> |
| 82 | +<li><a href="https://mdbootstrap.com/docs/react/extended/multiselect/">React multiselect</a></li> |
| 83 | +<li><a href="https://mdbootstrap.com/docs/react/forms/select/">React select</a></li> |
| 84 | +<li><a href="https://mdbootstrap.com/docs/react/forms/overview/">React forms</a></li> |
| 85 | +<li><a href="https://mdbootstrap.com/docs/react/forms/input-fields/">React input fields</a></li> |
| 86 | +</ul> |
0 commit comments