英文:
How to make a multiselectpicker in reactnative
问题
在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
答案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(_ => 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
? "Make a selection.."
: 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={["🥝","🍑","🍎"]} />
)
<!-- language: lang-css -->
.multiselect { padding: 0.25rem; background-color: #ffc; border: 1px solid #555; }
button::after { content: " ✖️"; }
.multiselect .selected { opacity: 0.2; }
<!-- language: 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>
<!-- 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 "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;
Output:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论