如何在React Native中创建多选选择器。

huangapple go评论69阅读模式
英文:

How to make a multiselectpicker in reactnative

问题

在React Native中创建类似的东西该如何实现。这应该是一个选择器,你选择的值应该保留在选择器视图内。这里有一张图片如何在React Native中创建多选选择器。

英文:

How to make something like this in reactnative. This should be some picker and the values that you select should stay inside the picker view. Here is a picture如何在React Native中创建多选选择器。

答案1

得分: 1

这是一个最小、可重现的例子。使用CSS进行满意的样式设计 -

<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: true -->

<!-- 语言: lang-js -->

    function Multiselect({ items }) {
      const [open, setOpen] = React.useState(false)
      const [selected, setSelected] = React.useState(_ => new Set())
      const onClick = event => setOpen(!open)
      const remove = item => event => {
        event.stopPropagation()
        const s = new Set(selected)
        s.delete(item)
        setSelected(s)
      }
      const add = item => event => {
        if (selected.has(item)) return
        const s = new Set(selected)
        s.add(item)
        setSelected(s)
      }
      return <div
        onClick={onClick}
        className="multiselect"
      >
        <div>
          { selected.size == 0
          ? "进行选择.."
          : Array.from(selected, item =>
              <button key={item} onClick={remove(item)} children={item} />
            )
          }
        </div>
        {open && items.map(item =>
          <span key={item} onClick={add(item)} className={selected.has(item) ? "selected" : ""} children={item} />
        )}
      </div>
    }

    ReactDOM.createRoot(document.querySelector("#app")).render(
      <Multiselect items={["🥝","🍑","🍎"]} />
    )

<!-- 语言: lang-css -->

    .multiselect { padding: 0.25rem; background-color: #ffc; border: 1px solid #555; }
    button::after { content: " ✖️"; }
    .multiselect .selected { opacity: 0.2; }

<!-- 语言: lang-html -->

    <script crossorigin src="https://unpkg.com/react@18/umd/react.development.js"></script>
    <script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
    <div id="app"></div>

<!-- 结束代码片段 -->
英文:

Here's a minimal, reproducible example. Use CSS to style to your satisfaction -

<!-- begin snippet: js hide: false console: true babel: true -->

<!-- language: lang-js -->

function Multiselect({ items }) {
  const [open, setOpen] = React.useState(false)
  const [selected, setSelected] = React.useState(_ =&gt; new Set())
  const onClick = event =&gt; setOpen(!open)
  const remove = item =&gt; event =&gt; {
    event.stopPropagation()
    const s = new Set(selected)
    s.delete(item)
    setSelected(s)
  }
  const add = item =&gt; event =&gt; {
    if (selected.has(item)) return
    const s = new Set(selected)
    s.add(item)
    setSelected(s)
  }
  return &lt;div
    onClick={onClick}
    className=&quot;multiselect&quot;
  &gt;
    &lt;div&gt;
      { selected.size == 0
      ? &quot;Make a selection..&quot;
      : Array.from(selected, item =&gt;
          &lt;button key={item} onClick={remove(item)} children={item} /&gt;
        )
      }
    &lt;/div&gt;
    {open &amp;&amp; items.map(item =&gt;
      &lt;span key={item} onClick={add(item)} className={selected.has(item) ? &quot;selected&quot; : &quot;&quot;} children={item} /&gt;
    )}
  &lt;/div&gt;
}

ReactDOM.createRoot(document.querySelector(&quot;#app&quot;)).render(
  &lt;Multiselect items={[&quot;&#129373;&quot;,&quot;&#127825;&quot;,&quot;&#127822;&quot;]} /&gt;
)

<!-- language: lang-css -->

.multiselect { padding: 0.25rem; background-color: #ffc; border: 1px solid #555; }
button::after { content: &quot; ✖️&quot;; }
.multiselect .selected { opacity: 0.2; }

<!-- language: lang-html -->

&lt;script crossorigin src=&quot;https://unpkg.com/react@18/umd/react.development.js&quot;&gt;&lt;/script&gt;
&lt;script crossorigin src=&quot;https://unpkg.com/react-dom@18/umd/react-dom.development.js&quot;&gt;&lt;/script&gt;
&lt;div id=&quot;app&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 1

这对你来说会奏效。根据你的需求使用CSS进行进一步的样式设置。

import React, { useState } from "react";
import { View, Text, Picker } from "react-native";

const ItemPicker = () => {
  const [selectedValue, setSelectedValue] = useState("");
  const [values, setValues] = useState([]);

  const handleChange = (value) => {
    setSelectedValue(value);
    setValues([...values, value]);
  };

  return (
    <View>
      <Picker selectedValue={selectedValue} onValueChange={handleChange}>
        <Picker.Item label="Item 1" value="1 selected" />
        <Picker.Item label="Item 2" value="2 selected" />
        <Picker.Item label="Item 3" value="3 selected" />
        <Picker.Item label="Item 4" value="4 selected" />
      </Picker>
      <View style={{ flexDirection: "row" }}>
        {values.map((value) => (
          <Text key={value} style={{ marginHorizontal: 5 }}>
            {value}
          </Text>
        ))}
      </View>
    </View>
  );
};

export default ItemPicker;
英文:

This will works for you. Use CSS for further styling as per your requirement.

import React, { useState } from &quot;react&quot;;
import { View, Text, Picker } from &quot;react-native&quot;;

const ItemPicker = () =&gt; {
  const [selectedValue, setSelectedValue] = useState(&quot;&quot;);
  const [values, setValues] = useState([]);

  const handleChange = (value) =&gt; {
    setSelectedValue(value);
    setValues([...values, value]);
  };

  return (
    &lt;View&gt;
      &lt;Picker selectedValue={selectedValue} onValueChange={handleChange}&gt;
        &lt;Picker.Item label=&quot;Item 1&quot; value=&quot;1 selected&quot; /&gt;
        &lt;Picker.Item label=&quot;Item 2&quot; value=&quot;2 selected&quot; /&gt;
        &lt;Picker.Item label=&quot;Item 3&quot; value=&quot;3 selected&quot; /&gt;
        &lt;Picker.Item label=&quot;Item 4&quot; value=&quot;4 selected&quot; /&gt;
      &lt;/Picker&gt;
      &lt;View style={{ flexDirection: &quot;row&quot; }}&gt;
        {values.map((value) =&gt; (
          &lt;Text key={value} style={{ marginHorizontal: 5 }}&gt;
            {value}
          &lt;/Text&gt;
        ))}
      &lt;/View&gt;
    &lt;/View&gt;
  );
};

export default ItemPicker;

Output:

如何在React Native中创建多选选择器。

huangapple
  • 本文由 发表于 2023年2月26日 23:38:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75573077.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定