ReactNative 自定义封装Radio单选组件

Radio单选框

1、功能说明

用于在多个备选项中选中单个状态。

Radio 所有选项默认可见,方便用户在比较中选择,因此选项不宜过多

2、组件效果

3、代码演示

import React, { Component } from "react";
import { Alert, View, Text, StyleSheet, TouchableOpacity, Image, ScrollView } from "react-native";
import { Radio } from "@/rn-design";
const RadioButton = Radio.Button;
const styles = StyleSheet.create({
    content: {
        flexDirection: "row",
        alignItems: "center",
        paddingHorizontal: 15,
        backgroundColor: "#fff",
        justifyContent: "space-between"
    },
    radioContent: {
        flexDirection: "row",
        alignItems: "center"
    },
    innerStyle: {
        height: 50
    }
});

export default class DemoTest extends Component {
    constructor(props) {
        super(props);
        this.state = {
            radioData: [
                { content: "商家评分", value: "1", disabled: false },
                { content: "个人评分", value: "2", disabled: false }
            ],
            selected: 2
        };
    }

    componentDidMount() {}

    render() {
        // console.log(RadioButton);
        return (
            <View>
                <View style={styles.content}>
                    <View>
                        <Text>测试Radio</Text>
                    </View>
                    <Radio
                        style={styles.radioContent}
                        dataOption={this.state.radioData}
                        options={{ value: "value", text: "content", disabled: "disabled" }}
                        selectedValue={this.state.selected}
                        disabledAll={false}
                        innerStyle={styles.innerStyle}
                        onChange={(item) => {
                            console.log(item);
                        }}
                        // txtColor="#ff552e"
                    />
                </View>
                {/* <RadioButton /> */}
            </View>
        );
    }
}

4、API

参数说明类型默认值
style当前Radio group的父级样式object
dataOptionRadio渲染当前Radio的数据array
options匹配Radio组件渲染时的text和value值(必填)
{
value:“对应dataOption中确定当前唯一标识的key”,
text:“对应dataOption中要展示的文案”,
disabled:“对应dataOption中当前选中是否禁用的标识,如果dataOption中没有改属性,默认全部是false,radio无禁用状态”
}
objectoption:{value:“value”,
text:“text”,
disabled:“disabled”}
selectedValue用于设置当前选中的值,也可以设置默认值any
disabledAll是否禁用全部Radiobooleanfalse
seledImgRadio选中图标 传值示例:{uri:"…"} 不支持本地图片object-
selImgRadio未选中图标 传值示例:{uri:"…"} 不支持本地图片object-
innerStyle单个Radio样式模块Object-
txtColorRadio文案颜色string-
onChange选项变化时的回调函数function

RadioButton

1、组件效果

2、代码演示

import React, { Component } from "react";
import { Alert, View, Text, StyleSheet, TouchableOpacity, Image, ScrollView } from "react-native";
import { Radio } from "@/rn-design";
const RadioButton = Radio.Button;
const styles = StyleSheet.create({
    content: {
        // flexDirection: "row",
        // alignItems: "center",
        paddingHorizontal: 15,
        backgroundColor: "#fff"
        // justifyContent: "space-between"
    },
    radioContent: {
        flexDirection: "row",
        alignItems: "center"
    },
    innerStyle: {
        height: 50
    }
});

export default class DemoTest extends Component {
    constructor(props) {
        super(props);
        this.state = {
            radioBtn: [
                { text: "不限", value: "0" },
                { text: "1万公里内", value: "0_1" },
                { text: "3万公里内", value: "0_3" },
                { text: "5万公里内", value: "0_5" },
                { text: "8万公里内", value: "0_8" }
            ],
            selected: "0_1"
        };
    }

    componentDidMount() {}

    render() {
        return (
            <View>
                <View style={styles.content}>
                    <View style={{ height: 45, justifyContent: "center" }}>
                        <Text>测试Radio</Text>
                    </View>
                    <RadioButton
                        dataOption={this.state.radioBtn}
                        options={{ value: "value", text: "text", disabled: "disabled" }}
                        selectedValue={this.state.selected}
                        onChange={(item) => {
                            console.log(item);
                        }}
                        size={[78, 37]}
                        // seledImg={require("./../img/filter/9@3x.png")}
                    />
                </View>
            </View>
        );
    }
}

3、API

参数说明类型默认值
dataOptionRadio渲染当前Radio的数据array
options匹配Radio组件渲染时的text和value值(必填)
{
value:“对应dataOption中确定当前唯一标识的key”,
text:“对应dataOption中要展示的文案”,
disabled:“对应dataOption中当前选中是否禁用的标识,如果dataOption中没有改属性,默认全部是false,radio无禁用状态”
}
objectoption:{value:“value”,
text:“text”,
disabled:“disabled”}
selectedValue用于设置当前选中的值,也可以设置默认值any
disabledAll是否禁用全部Radiobooleanfalse
seledImgRadio选中图标 传值示例:{uri:"…"} 不支持本地图片object-
onChange选项变化时的回调函数function
txtColorRadio字体颜色string“#555555”
activeTxtColorRadio选中字体颜色string“#FF552E”
backgroundColorRadio按钮块背景颜色string“#F6F6F6”
activeBackgroundColorRadio按钮块选中背景颜色string“#FFEFEB”
sizeRadio按钮块大小 如[80,38]int[][78,37]

源码地址

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值