ส่วนประกอบ SwitchSelector คล้ายกับปุ่มสลับแบบวิทยุ ให้คุณเลือกได้มากกว่า 2 ค่า
ในการทำงานกับ SwitchSelector คุณต้องติดตั้งแพ็คเกจดังที่แสดงด้านล่าง -
npm i react-native-switch-selector --save-dev
SwitchSelector พื้นฐานมีลักษณะดังนี้ -
<SwitchSelector
106
React Native – Q&A
options={youroptions}
initial={0}
onPress={value => console.log(`value selected is : ${value}`)}
/> นี่คือคุณสมบัติที่สำคัญบางประการของ SwitchSelector -
| อุปกรณ์ประกอบฉาก | คำอธิบาย |
|---|---|
| ตัวเลือก | ไม่ระบุอาร์เรย์ที่มีป้ายกำกับ ค่า และไอคอนรูปภาพ |
| เริ่มต้น | รายการเริ่มต้นจากอาร์เรย์ที่จะถูกเลือก |
| ค่า | ค่าสวิตช์ที่จะสามารถใช้ได้กับonPress event |
| onPress | เหตุการณ์ที่มีฟังก์ชันเรียกกลับที่จะถูกเรียกเมื่อสวิตช์ถูกเปลี่ยน |
| fontSize | ขนาดแบบอักษรที่จะพิจารณาสำหรับป้ายกำกับ |
| สีที่เลือก | สีของรายการที่เลือก |
| ปุ่มสี | สีพื้นหลังสำหรับรายการที่เลือก |
| สีข้อความ | สีฉลากสำหรับสินค้าที่ไม่ได้เลือก |
| สีพื้นหลัง | สีพื้นหลังสำหรับส่วนประกอบตัวเลือกสวิตช์ |
| สีเส้นขอบ | กำหนดสีเส้นขอบให้กับส่วนประกอบ |
ตัวอย่าง:การทำงานของ SwitchSelector
ในการทำงานกับ SwitchSelector เราต้องนำเข้าส่วนประกอบดังต่อไปนี้ −
import SwitchSelector from "react-native-switch-selector";
ภายใน SwitchSelector เราจะไม่แสดงสองตัวเลือก:หญิง/ชาย
ในตัวอย่าง เรากำลังใช้ภาพผู้หญิงและผู้ชาย และภาพเดียวกันนี้ใช้ในอาร์เรย์ตัวเลือก
let male = require('C:/myfirstapp/myfirstapp/assets/male.png');
let female = require('C:/myfirstapp/myfirstapp/assets/female.png');
const images = {
"female": female,
"male": male,
};
const options =[
{ label: "Female", value: "f", imageIcon: images.female },
{ label: "Male", value: "m", imageIcon: images.male }
]; SwitchSelector มีลักษณะดังนี้ -
<SwitchSelector
initial={0}
onPress={value => this.setState({ gender: value })}
textColor='#ccceeaa'
selectedColor='#7a44cf'
buttonColor='#ccc'
borderColor='#ccc'
hasPadding
options={options}
/> นี่คือรหัสเต็มของ SwitchSelector -
ตัวอย่าง
import React, { Component } from 'react';
import { StyleSheet, SafeAreaView } from 'react-native';
import SwitchSelector from "react-native-switch-selector";
let male = require('C:/myfirstapp/myfirstapp/assets/male.png');
let female = require('C:/myfirstapp/myfirstapp/assets/female.png');
const images = {
"female": female,
"male": male,
};
const options =[
{ label: "Female", value: "f", imageIcon: images.female },
{ label: "Male", value: "m", imageIcon: images.male }
];
export default class MySwitchSelectorComponent extends Component {
render() {
return (
<SafeAreaView style={styles.container}>
<SwitchSelector
initial={0}
onPress={value => this.setState({ gender: value })}
textColor='#ccceeaa'
selectedColor='#7a44cf'
buttonColor='#ccc'
borderColor='#ccc'
hasPadding
options={options}
/>
</SafeAreaView>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: "center",
justifyContent: "center"
},
}); ผลลัพธ์
ผลลัพธ์จะเป็นดังนี้ −

ตัวอย่าง 2:SwitchSelector ที่มีสามตัวเลือก
ในตัวอย่างด้านล่าง เราใช้สามตัวเลือก −
const options =[
{ label: "First", value: "a"},
{ label: "Second", value: "b" } ,
{ label: "Third", value: "c" }
]; รหัสเต็มเพื่อแสดงสามตัวเลือกมีดังนี้ −
ตัวอย่าง
import React, { Component } from 'react';
import { StyleSheet, SafeAreaView } from 'react-native';
import SwitchSelector from "react-native-switch-selector";
const options =[
{ label: "First", value: "a"},
{ label: "Second", value: "b" } ,
{ label: "Third", value: "c" }
];
export default class MySwitchSelectorComponent extends Component {
render() {
return (
<SafeAreaView style={styles.container}>
<SwitchSelector
initial={0}
onPress={value => this.setState({ gender: value })}
textColor='#ccceeaa'
selectedColor='#7a44cf'
buttonColor='#ccc'
borderColor='#ccc'
fontSize='30'
hasPadding
options={options}
/>
</SafeAreaView>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1
}
}); ผลลัพธ์
