问题描述
代码如下:
在线测试代码: https://codesandbox.io/s/sou-...
代码如下:
import React from "react";
import "antd/dist/antd.css";
import "./index.css";
import { Select } from "antd";
import { useState } from "react";
const { Option } = Select;
const SearchInput = (props) => {
const [data, setData] = useState([
{
value: "value1",
label: "value1"
},
{
value: "value2",
label: "value2"
}
]);
const [value, setValue] = useState();
const handleSearch = (newValue) => {
console.log(newValue, "xz1122");
window.customNewValue = newValue;
setValue(newValue);
};
const handleChange = (newValue) => {
setValue(newValue);
};
const options = data.map((d) => <Option key={d.value}>{d.text}</Option>);
console.log(value, window.customNewValue, "xz1111");
return (
<Select
showSearch
value={value}
style={props.style}
defaultActiveFirstOption={false}
showArrow={false}
filterOption={false}
onSearch={handleSearch}
onChange={handleChange}
optionLabelProp={"children"}
notFoundContent={null}
onBlur={() => {
// 此时的value已经变成空了
console.log(value, "xz555");
}}
>
{options}
</Select>
);
};
const App = () => (
<SearchInput
placeholder="input search text"
style={{
width: 200
}}
/>
);
export default App;
你可能想要的是AutoComplete