UNPKG

mp-colorui

Version:

MP ColorUI 是一款基于 Taro 框架并且联合 Color-UI CSS 库开发的多端 UI 组件库(支持小程序端和H5端)

63 lines (61 loc) 2.34 kB
import Nerv from "nervjs"; import Taro from "@tarojs/taro-h5"; import { Text, View } from "@tarojs/components"; import ListRadio from "./components/ListRadio"; import { classNames } from "../../lib/index"; export default class RadioH5 extends Taro.Component { render() { const props = this.props; const type = props.type || "normal"; const shapeClassName = props.shape || "normal"; const title = props.title || ""; const colorClassName = props.color || "green"; const directionClassName = props.direction === "horizontal" ? "flex" : ""; const list = props.radioGroup || []; const [activeValue, setActiveValue] = Taro.useState(props.checkedValue); const clickRadio = (name, index) => { setActiveValue(name); }; Taro.useEffect(() => { props.onChange && props.onChange(activeValue); }, [activeValue]); const radioComponent = list.map((item, index) => <View className="padding-xs" key={item.value}> <Text className="padding-right-sm">{item.key}</Text> <View onClick={() => { if (!props.disabled) { clickRadio(item.value, index); } }} className={classNames(["h5-radio", colorClassName, shapeClassName, { checked: activeValue === item.value, disabled: props.disabled }])}> <View className="h5-radio-wrapper"> <View className="h5-radio-input h5-radio-input-checked" /> </View> </View> </View>); const formRadioComponent = <View className="cu-form-group"> <View className="title">{title}</View> <View className={directionClassName}>{radioComponent}</View> </View>; const renderListComponent = () => <ListRadio onChange={value => { setActiveValue(value); }} list={list} checkedValue={activeValue} disabled={props.disabled} />; const formOrNormalComponent = <View className="h5-radio-group"> {type === "form" ? formRadioComponent : <View className={directionClassName}>{radioComponent}</View>} </View>; const weappComponent = type === "list" ? renderListComponent() : formOrNormalComponent; return <View>{weappComponent}</View>; } } RadioH5.defaultProps = { type: "normal", shape: "normal", title: "", colorClassName: "green", directionClassName: "", radioGroup: [] }; RadioH5.options = { addGlobalClass: true };