Skip to content

Commit 4de9c03

Browse files
committed
Update README.md and add package.json
1 parent 9962cc7 commit 4de9c03

File tree

3 files changed

+114
-1
lines changed

3 files changed

+114
-1
lines changed

README.md

+86-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,86 @@
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+
![React Bootstrap 5 Select With Custom Input](/assets/basic.png)
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>

assets/basic.png

9.3 KB
Loading

package.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "@mdbootstrap/react-select-with-custom-input",
3+
"version": "1.0.0",
4+
"description": "Responsive React Select with custom user input built with Bootstrap 5. Click on the Other option to input a custom value into the select.",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [
10+
"bootstrap",
11+
"navigation",
12+
"bootstrap5",
13+
"responsive-design",
14+
"bootstrap-template",
15+
"template",
16+
"react"
17+
],
18+
"repository": {
19+
"type": "git",
20+
"url": "git+https://github.com/mdbootstrap/react-select-with-custom-input.git"
21+
},
22+
"author": "",
23+
"license": "ISC",
24+
"bugs": {
25+
"url": "https://github.com/mdbootstrap/react-select-with-custom-input/issues"
26+
},
27+
"homepage": "https://mdbootstrap.com/docs/react/extended/select-with-custom-input/"
28+
}

0 commit comments

Comments
 (0)