标签: react-select

如何防止用户使用 React-Select Creatable 创建重复值?

我正在使用“react-select”Creatable创建下拉列表并允许用户在列表中创建新项目。

这就是我所拥有的:

<Creatable
  name="form-field-name"
  value={this.props.selectWorker}
  options={this.props.selectWorkers}
  onChange={this.props.handleSelectWorker}
/>
Run Code Online (Sandbox Code Playgroud)

现在,用户可以创建新名称,即使它已经存在,也可以创建重复项,如下所示。

姓名重复

我看到有一个名为isOptionUniquereact-select site”的选项。

在选项集中搜索任何匹配的选项。此功能可防止创建重复选项。默认情况下,这是标签和值的基本、区分大小写的比较。预期签名:({ option: Object, options: Array, labelKey: string, valueKey: string }): boolean

我还没能使用它。我已经尝试过isOptionUnique=trueisOptionUnique={options:this.props.workers}但出现Creatable.js:173 Uncaught TypeError: isOptionUnique is not a function错误。

我找不到 的示例isOptionUnique,过滤反应选择以防止重复使用的最佳方法是什么Creatable

reactjs react-select

5
推荐指数
1
解决办法
5089
查看次数

React 选择多行标​​签

有没有一种方法可以使用react-select 获得多行标签或为一个选项提供两个标签?

我通过值和标签传递选择组件选项

value: someValue,
label: "This is my first value - Name: Value 1 \n Foo Bar Date Insert 
Today's Date Here"
Run Code Online (Sandbox Code Playgroud)

我尝试插入换行符,但文本仍保留在选择输入中的一行上。我正在寻找一种解决方案,每个选项都有两行。

reactjs react-select

5
推荐指数
1
解决办法
1万
查看次数

onBlur 重置反应选择中的值

我的反应选择有问题。当我在可搜索输入中输入一些文本并单击外部时,我的文本消失了。我尝试过onBlur={() => input.onBlur({value: input.value})},它工作正常,但我在控制台中出现了丑陋的错误。就像是Uncaught ReferenceError: input is not defined

onChangeValue = (newValue) => {
  const inputValue = newValue.replace(/\W/g, '');
  this.setState({ inputValue });
  this.props.nameFilter(inputValue);
};

handleCloseMenu = () => {
  this.setState(({
    menuIsOpen: false,
  }))
  input.onBlur(input.value)
};

handleOpenMenu = () => {
  this.setState(({
    menuIsOpen: true,
  }))
  input.onBlur(input.value)
};

render() {
  return (
   <div className="container">
    <AsyncSelect 
      className="search-input"
      onFocus={this.handleOpenMenu}
      onBlur={this.handleCloseMenu}
      menuIsOpen={this.state.menuIsOpen}
      loadOptions={this.loadOptions}
      defaultOptions
      onInputChange={this.onChangeValue}
      placeholder="Search..."
    />
  </div>    
  )};
};
Run Code Online (Sandbox Code Playgroud)

有没有不使用 redux-form 的解决方法?

与这里相同的问题,但我没有使用 redux-form 并且这些解决方案不起作用。

onblur reactjs react-select redux

5
推荐指数
1
解决办法
1万
查看次数

AsyncSelect 不会更改加载消息,也不会更改选项消息(反应选择库)

我一直在尝试自定义react-select AsyncSelect 加载消息和无选项消息,但到目前为止没有运气。

这是我当前的代码:

                    <AsyncSelect
                        noOptionsMessage={() => 'nothing found'}
                        LoadingMessage={() => 'searching...'}
                        cacheOptions
                        loadOptions={searchForPointsOfInterest}
                        getOptionLabel={(option) => option.name}
                        getOptionValue={(option) => option}
                        defaultOptions
                        isRtl={true}
                        isClearable={true}
                        placeholder="example"
                        isLoading={isLoading}
                        onChange={onPointOfInterestChange}
                        defaultValue={props.area}
                    />
Run Code Online (Sandbox Code Playgroud)

reactjs react-select

5
推荐指数
1
解决办法
5318
查看次数

React-select 的辅助功能 ARIA 问题

我们正在我们的一个项目中使用反应选择组件并尝试解决可访问性问题。目前,无法向具有 aria-autocomplete="list" 属性的输入元素提供所需的 aria 属性。

https://www.digitala11y.com/aria-autocomplete-properties/

上面是我们想要实现的目标的参考链接。以下是上述链接中的短篇故事。

If an element has aria-autocomplete set to list or both, authors MUST ensure both of the following conditions are met:

The element has a value specified for aria-controls that refers to the element that contains the collection of suggested values.
Either the element or a containing element with role combobox has a value for aria-haspopup that matches the role of the element that contains the collection of suggested values.
Run Code Online (Sandbox Code Playgroud)

我打开了一个 PR,以添加将 ARIA 属性添加到输入元素的功能。 …

accessibility autocomplete wai-aria react-select

5
推荐指数
0
解决办法
2831
查看次数

如何删除 IndicatorContainer(IndicatorsContainer 的第一个子级)的填充?

我正在尝试缩小反应选择的选择组件的大小。除了一个例外,一切都运行得很好。

\n

我希望删除其填充的元素具有此类:css-tlfecz-indicatorContainer并且是反应选择的子组件Select组件的子组件,更准确地说,是负责渲染\xc3\x97符号以清除选择的子组件。

\n

我尝试将其放入 Select 的 styles 属性中:

\n
indicatorsContainer: (provided, state) => {\n  return {\n    ...provided,\n    padding: \'0px\',\n    paddingLeft: \'0px\',\n    paddingTop: \'0px\',\n    paddingRight: \'0px\',\n    paddingDown: \'0px\',\n  };\n},\n
Run Code Online (Sandbox Code Playgroud)\n

但这不起作用。我期望有一个 IndicatorContainer 可样式组件,这样我就不必用自己的组件覆盖组件,但我认为这是唯一的方法。

\n

如果我从 DevTools\' Elements 选项卡手动更改样式,则选择的屏幕截图如下:

\n

截屏

\n

没有错误消息。

\n

谢谢。

\n

javascript reactjs react-select

5
推荐指数
1
解决办法
2135
查看次数

在移动设备上使用 Formik 进行 React-select 时验证工作不正确

我已将 React-select 元素与 Formik 连接起来。验证在桌面浏览器中运行良好。但在移动浏览器或移动浏览器模拟器中,我在第一次 React-select 元素值更改时收到验证错误。但我在下一个值更改时没有收到错误。这是 Formik 或 React-select 中的错误吗?有可能解决这个问题吗?

这是codesandbox上的示例: https://codesandbox.io/s/musing-mcnulty-62euu ?file=/index.js

请仅在移动浏览器或移动浏览器模拟器中检查它,因为验证在桌面浏览器中工作正常。

reactjs react-select yup formik

5
推荐指数
2
解决办法
957
查看次数

立即更新 useState

useState不会立即更新状态。

\n

我正在使用react-select,我需要加载multi根据请求结果选择的 ( ) 选项的组件。

\n

因此,我创建了 statedefaultOptions来存储常量的值queues

\n

事实证明,加载组件时,值 \xe2\x80\x8b\xe2\x80\x8bar 仅在第二次显示。

\n

console.log在 中做了一个queues,返回与空不同。

\n

我对状态做了同样的事情defaultOptions,返回的是空的。

\n

我创建了一个codesandbox以便更好地查看。

\n
const options = [\n  {\n    label: "Queue 1",\n    value: 1\n  },\n  {\n    label: "Queue 2",\n    value: 2\n  },\n  {\n    label: "Queue 3",\n    value: 3\n  },\n  {\n    label: "Queue 4",\n    value: 4\n  },\n  {\n    label: "Queue 5",\n    value: 5\n  }\n];\n\nconst CustomSelect = (props) …
Run Code Online (Sandbox Code Playgroud)

reactjs react-select react-hooks

5
推荐指数
1
解决办法
1万
查看次数

将自定义 data- 属性添加到选项组件react-select

我正在使用反应选择库为我的下拉可搜索组件创建一个测试。在自定义该组件时,
我无法data-testid向组件添加属性,因为它是在反应选择文档中定义的。Option

data-testid属性不会显示在Option元素的 DOM 中。

选项组件

import Select, { components } from 'react-select';

const CustomOption = (props: OptionProps<SearchDropdownOption, false>) => (
  <components.Option {...props} data-testid="test-id" />
);
Run Code Online (Sandbox Code Playgroud)

例如,我成功使用了DOM 中显示Input的下拉列表和属性的搜索版本组件:data-testid

输入组件

import Select, { components } from 'react-select';

const CustomInput = (props: InputProps<SearchDropdownOption, false>) => (
  <components.Input {...props} data-testid="test-id" />
);
Run Code Online (Sandbox Code Playgroud)

而不是在Select组件中使用它:

<Select<SearchDropdownOption, false>
  components={{
    Input: CustomInput,
    Option: CustomOption,
  }}
  isSearchable={isSearchable}
/>
Run Code Online (Sandbox Code Playgroud)

testing components reactjs react-select

5
推荐指数
1
解决办法
5340
查看次数

自定义react-select可创建选择组件和reach-hook-form打字稿错误

我有一个使用反应选择的自定义组件。这使用了Createable Selectand 看起来像:

import CreatableSelect from "react-select/creatable";
import {
  ActionMeta,
  MultiValue,
  MultiValueGenericProps,
  OptionProps,
  StylesConfig,
  components,
} from "react-select";


type UserOption = {
  value: number;
  label: string;
  logo?: string;
};


type CustomUserInputProps = {
  userOptions: UserOption[];
  onChange: (
    newValue: MultiValue<UserOption>,
    actionMeta: ActionMeta<UserOption>
  ) => void;
  onBlur: ChangeEventHandler;
  value: UserOption[];
  name: string;
}


export function CustomUserInput ({
  userOptions,
  onChange,
  onBlur,
  value,
  name,
  disabled,
  isEdited,
}: CustomUserInputProps ) {
   ...
   return (
         <CreatableSelect
          options={userOptions}
          onChange={onChange}
          placeholder={""}
          isMulti={true}
          onBlur={onBlur}
          value={value}
          name={name}
          components={{ …
Run Code Online (Sandbox Code Playgroud)

typescript reactjs react-select react-hook-form

5
推荐指数
1
解决办法
715
查看次数