Thành phần SwitchSelector tương tự như một nút bật tắt radio. Nó cho phép bạn chọn nhiều hơn 2 giá trị.
Để làm việc với SwitchSelector, bạn phải cài đặt gói như hình dưới đây -
npm i react-native-switch-selector --save-dev
SwitchSelector cơ bản trông như sau -
<SwitchSelector
106
React Native – Q&A
options={youroptions}
initial={0}
onPress={value => console.log(`value selected is : ${value}`)}
/> Dưới đây là một số thuộc tính quan trọng của SwitchSelector -
| Đạo cụ | Mô tả |
|---|---|
| tùy chọn | Yêu cầu một mảng với id nhãn, giá trị và hình ảnh. |
| tên đầu tiên | Mục ban đầu từ mảng sẽ được chọn riêng. |
| giá trị | Giá trị công tắc sẽ khả dụng khi không có sự kiện Nhấn |
| onPress | Sự kiện có chức năng gọi lại sẽ được gọi khi Công tắc được thay đổi. |
| fontSize | Kích thước fontS được xem xét cho nhãn. |
| selectColor | Màu của mục đã chọn. |
| buttonColor | Màu nền cho mục đã chọn. |
| textColor | Màu nhãn cho các mục không được chọn. |
| backgroundColor | Màu nền cho thành phần chọn công tắc. |
| borderColor | Màu đường viền sẽ được cung cấp cho thành phần. |
Ví dụ:Hoạt động của SwitchSelector
Để làm việc với SwitchSelector, chúng ta phải nhập thành phần như sau -
import SwitchSelector from "react-native-switch-selector";
Bên trong SwitchSelector, chúng tôi sẽ không hiển thị hai tùy chọn:Nữ / Nam.
Trong ví dụ này, chúng tôi đang sử dụng hình ảnh nữ và nam và điều tương tự cũng được sử dụng trong mảng tùy chọn.
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 trông như sau -
<SwitchSelector
initial={0}
onPress={value => this.setState({ gender: value })}
textColor='#ccceeaa'
selectedColor='#7a44cf'
buttonColor='#ccc'
borderColor='#ccc'
hasPadding
options={options}
/> Đây là mã đầy đủ của SwitchSelector -
Ví dụ
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"
},
}); Đầu ra
Kết quả như sau -
Ví dụ 2:SwitchSelector với ba tùy chọn
Trong ví dụ dưới đây, chúng tôi đã sử dụng ba tùy chọn -
const options =[
{ label: "First", value: "a"},
{ label: "Second", value: "b" } ,
{ label: "Third", value: "c" }
]; Mã đầy đủ để hiển thị ba tùy chọn như sau -
Ví dụ
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
}
}); Đầu ra