简体   繁体   English

在 Reactjs 中使用 Antd 自动完成

[英]Autocomplete using Antd in Reactjs

I am making an autocomplete options in react app and generated the autocomplete field with list of options.我正在反应应用程序中创建一个自动完成选项,并生成带有选项列表的自动完成字段。

Working Example:工作示例:

编辑 react-antd-input-autocomplete(分叉)

List of options:选项列表:

const options = [
  { label: "hello", value: "1" },
  { label: "codesandbox", value: "2" },
  { label: "react", value: "3" },
  { label: "nodejs", value: "4" },
  { label: "java", value: "5" },
  { label: "antd", value: "6" },
  { label: "dependency", value: "7" },
  { label: "less", value: "8" }
];

Here if user select the data react , then the input has the value as 3 .这里如果用户 select 数据react ,那么输入的值为3

Expectation:期待:

Expectation is that the input field needs to have the text as react but the selected value needs to be 3 .期望是输入字段需要将文本作为react ,但所选值需要为3

So for user display, it needs to be in text and the background value will be number.所以对于用户显示,它需要在文本中,背景值将是数字。

Referred this post React Antd v4 autocomplete shows selected value instead of label upon selecting an option but it doesn't help in any way.参考这篇文章React Antd v4 autocomplete 在选择一个选项时显示选定的值而不是 label但它没有任何帮助。

Well AutoComplete is an input component. What you need is那么AutoComplete is an输入component. What you need is component. What you need is select with search`. component. What you need is with搜索功能的 select。 The example you provided can be implemented a bit to make it work as you want.您提供的示例可以稍微实现以使其按您的意愿工作。 You might need to make some modifications.您可能需要进行一些修改。 The below code is just to demonstrate how you can change it, not sure if it will run without errors or not.下面的代码只是为了演示如何更改它,不确定它是否会在没有错误的情况下运行。

import { Select } from 'antd';

const { Option } = Select;

function onChange(value) {
  console.log(`selected ${value}`);
}

const options = [
  { label: "hello", value: "1" },
  { label: "codesandbox", value: "2" },
  { label: "react", value: "3" },
  { label: "nodejs", value: "4" },
  { label: "java", value: "5" },
  { label: "antd", value: "6" },
  { label: "dependency", value: "7" },
  { label: "less", value: "8" }
];

function onSearch(val) {
  console.log('search:', val);
}

ReactDOM.render(
  <Select
    showSearch
    placeholder="Select a person"
    optionFilterProp="children"
    onChange={onChange}
    onSearch={onSearch}
    filterOption={(input, option) =>
      option.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
    }
  >
    { options.map((option => <Option value={option.value}>{option.hellop}</Option>)) }
  </Select>,
  mountNode,
);

As @TheWhiteFang answered, one solution to solve your problem is using Select component with showSearch prop, but if you want to use AutoComplete one another solution could be defining two seprate states, one state for input value and another state for selectedOption from AutoComplete . As @TheWhiteFang answered, one solution to solve your problem is using Select component with showSearch prop, but if you want to use AutoComplete one another solution could be defining two seprate states, one state for input value and another state for selectedOption from AutoComplete . Then you could manage your states by onChange and onSelect props.然后你可以通过onChangeonSelect道具来管理你的状态。 like this:像这样:

const Complete = () => {
  const [selectedOption, setSelectedOption] = React.useState('');
  const [inputValue, setInputValue] = React.useState('');

  const onSelect = (data, option) => {
    setSelectedOption(option);
    setInputValue(option.label);
  };

  const onChange = (data, option) => {
    setInputValue(data);
    setSelectedOption(option); // to remove selected option when user types  something wich doesn't match with any option
  };

  return (
   <AutoComplete
        value={inputValue}
        options={options}
        autoFocus={true}
        style={{width: 200}}
        filterOption={(inputValue, option) =>
          option.label.toUpperCase().indexOf(inputValue.toUpperCase()) !== -1
        }
        onSelect={onSelect}
        onChange={onChange}
      />
  );
};

Maybe you need to make some changes in above example by your requirement, anyway you can find above example Here on stackblitz .也许您需要根据您的要求在上面的示例中进行一些更改,无论如何您可以在 stackblitz 上找到上面的示例。

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

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