import { Radio, RadioGroup, Text, View } from "@tarojs/components"; import Taro, { useEffect, useState } from "@tarojs/taro"; import H5Radio from "./h5"; import ListRadio from "./components/ListRadio"; import { IProps } from "../../../@types/radio"; import { classNames, isWeApp } from "../../lib"; export default function ClRadio(props: IProps) { 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 [checkedValue, setCheckedValue] = useState(props.checkedValue); useEffect(() => { setCheckedValue(checkedValue); }, [props.checkedValue]); const changeRadio = e => { props.onChange && props.onChange(e.detail.value); }; const radioComponent = list.map(item => ( { !props.disabled && setCheckedValue(item.value); }} > {item.key} )); const formRadioComponent = ( {title} {radioComponent} ); const renderListComponent = () => ( { changeRadio({ detail: { value } }); }} list={list} checkedValue={props.checkedValue} disabled={props.disabled} /> ); const formOrNormalComponent = ( {type === "form" ? ( formRadioComponent ) : ( {radioComponent} )} ); const weappComponent = type === "list" ? renderListComponent() : formOrNormalComponent; const RadioComponent = !isWeApp ? : weappComponent; return ( {RadioComponent} ); } ClRadio.defaultProps = { type: "normal", shape: "normal", title: "", colorClassName: "green", directionClassName: "", radioGroup: [] } as IProps; ClRadio.options = { addGlobalClass: true };