react-native-switch-selector:React Native 可定制切换选择器组件

Switch Selector for React Native

Branch8Tags9
FilesLast commitLast update
6 years ago
8 years ago
6 years ago
6 years ago
8 years ago
5 years ago
6 years ago
5 years ago
5 years ago
6 years ago
5 years ago
6 years ago

切换选择器

适用于 React Native 的切换选择器。

npm 版本 下载量

切换选择器

SwitchSelector

切换选择器 - 自定义示例

SwitchSelector

安装

yarn add react-native-switch-selector

npm i react-native-switch-selector --save

使用方法

import SwitchSelector from "react-native-switch-selector";
const options = [
  { label: "01:00", value: "1" },
  { label: "01:30", value: "1.5" },
  { label: "02:00", value: "2" }
];

你还可以为每个选项添加 testIDaccessibilityLabel

const options = [
  { label: "01:00", value: "1", testID: "switch-one", accessibilityLabel: "switch-one" },
  { label: "01:30", value: "1.5", testID: "switch-one-thirty", accessibilityLabel: "switch-one-thirty" },
  { label: "02:00", value: "2", testID: "switch-two", accessibilityLabel: "switch-two" }
];
<SwitchSelector
  options={options}
  initial={0}
  onPress={value => console.log(`Call onPress with value: ${value}`)}
/>

自定义示例

<SwitchSelector
  initial={0}
  onPress={value => this.setState({ gender: value })}
  textColor={colors.purple} //'#7a44cf'
  selectedColor={colors.white}
  buttonColor={colors.purple}
  borderColor={colors.purple}
  hasPadding
  options={[
    { label: "Feminino", value: "f", imageIcon: images.feminino }, //images.feminino = require('./path_to/assets/img/feminino.png')
    { label: "Masculino", value: "m", imageIcon: images.masculino } //images.masculino = require('./path_to/assets/img/masculino.png')
  ]}
  testID="gender-switch-selector"
  accessibilityLabel="gender-switch-selector"
/>

属性

属性 类型 默认值 是否必需 说明
options array null 要渲染的选项数组。每个选项包含 label、value 以及可选的图标
options[].label string null 每个选项的标签文本
options[].value string null 每个选项的值
options[].customIcon Jsx element 或 Function null 每个选项的可选自定义图标
options[].imageIcon string null 每个选项的图片图标资源路径。渲染时与标签文本颜色相同
options[].activeColor string null 每个选项被选中时的颜色
options[].testID string undefined 用于测试的每个选项的测试 ID(例如,使用 Appium)
options[].accessibilityLabel string undefined 用于测试的每个选项的无障碍标签(例如,使用 Appium)
initial number -1 初始渲染时选中的选项索引
value number undefined 开关的值(将触发 onPress)
onPress function console.log 更改值后调用的回调函数
disableValueChangeOnPress bool false 当手动更改值时,禁用 onPress 调用
fontSize number null 标签文本的字体大小。如果为 null,则使用应用的默认字体大小
selectedColor string '#fff' 选中项的文本颜色
buttonMargin number 0 选中项与组件边缘的边距
buttonColor string '#BCD635' 选中项的背景颜色
textColor string '#000' 未选中项的文本颜色
backgroundColor string '#ffffff' 组件的背景颜色
borderColor string '#c9c9c9' 组件的边框颜色
borderRadius number 50 组件的边框圆角半径
hasPadding bool false 指示选项是否有内边距
animationDuration number 250 动画持续时间(毫秒)
valuePadding number 1 内边距大小
height number 40 组件高度
bold bool false 指示文本是否为粗体
textStyle object {} 文本样式
selectedTextStyle object {} 选中项的文本样式
textContainerStyle object {} 文本(和图标)容器的样式 (TouchableOpacity)
selectedTextContainerStyle object {} 选中项的文本(和图标)容器的样式 (TouchableOpacity)
imageStyle object {} 图片样式
style object {} 容器样式
returnObject bool false 指示 onPress 函数是否返回整个选项对象而非 option.value
disabled bool false 禁用开关
borderWidth number 1 定义边框宽度
testID string null 用于测试的测试 ID(例如,使用 Appium)
accessibilityLabel string null 用于测试的无障碍标签(例如,使用 Appium)

作者

贡献

我们始终欢迎贡献!请创建一个新的拉取请求。

Introduction

Switch Selector for React Native

Customize your domain
10510115Visit GitHub