3 Star 0 Fork 0

mirrors_mdbootstrap / react-select-with-custom-input

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

Responsive React Select with custom user input built with Bootstrap 5. Click on the Other option to input a custom value into the select.

Check out React Bootstrap Select With Custom Input Documentation for detailed instructions & even more examples.

Basic example

React Bootstrap 5 Select With Custom Input

import React, { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { MDBContainer, MDBInput, MDBSelect } from "mdb-react-ui-kit";

export default function App() {
  const data = useMemo(
    () => [
      { text: "One", value: 1 },
      { text: "Two", value: 2 },
      { text: "Three", value: 3 },
      { text: "Four", value: 4 },
      { text: "Other", value: 5 },
    ],
    []
  );

  const [currentValue, setCurrentValue] = useState(data[0]);
  const [inputActive, setInputActive] = useState(false);

  const otherInputEl = useRef(null);

  const inputOnBlur = useCallback((e) => {
    if (!e.target.value) {
      setInputActive(false);
      setCurrentValue(data[data.length - 1])
    }
  }, []);

  useEffect(() => {
    console.log(currentValue);
    const lastIndex = data.length - 1;
    if (currentValue.value == data[lastIndex].value) {
      setInputActive(true);
      setTimeout(() => {
        otherInputEl.current?.focus();
      });
    } else {
      setInputActive(false);
    }
  }, [currentValue]);

  return (
    <MDBContainer style={{ width: "300px" }} className="mt-5">
      {inputActive ? (
        <MDBInput onBlur={inputOnBlur} inputRef={otherInputEl} label="Other" id="form1" type="text" />
      ) : (
        <MDBSelect
          onValueChange={(e) => setCurrentValue(e)}
          label="Example label"
          data={data}
        />
      )}
    </MDBContainer>
  );
}

How to use?

  1. Download MDB React - free UI KIT

  2. Choose your favourite customized component and click on the image

  3. Copy & paste the code into your MDB project

▶️ Subscribe to YouTube channel for web development tutorials & resources


More extended Bootstrap documentation

空文件

简介

取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors_mdbootstrap/react-select-with-custom-input.git
git@gitee.com:mirrors_mdbootstrap/react-select-with-custom-input.git
mirrors_mdbootstrap
react-select-with-custom-input
react-select-with-custom-input
main

搜索帮助

344bd9b3 5694891 D2dac590 5694891