UNPKG

@mijadesign/mjui-react-taro

Version:

京东风格的轻量级移动端 React 组件库,支持一套代码生成 H5 和小程序

146 lines (145 loc) 6.45 kB
import { _ as __rest } from "./tslib.es6-iWu3F_1J.js"; import { a, b as a$1 } from "./Unselected-CDv_w30k.js"; /* empty css */ import classNames from "classnames"; import React, { useCallback, useContext } from "react"; import { u as usePropsValue } from "./use-props-value-DuhZMy4U.js"; import { CheckChecked, CheckNormal } from "@nutui/icons-react-taro"; import { C as ComponentDefaults } from "./typings-DV9RBfhj.js"; const RadioContext = React.createContext(null); const defaultProps$1 = { labelPosition: "right", onChange: (value) => { }, direction: "vertical", options: [] }; const classPrefix = "nut-radiogroup"; const RadioGroup = React.forwardRef((props, ref) => { const _a = Object.assign(Object.assign({}, defaultProps$1), props), { children, className, value, defaultValue, onChange, shape, labelPosition, direction, options, disabled } = _a, rest = __rest(_a, ["children", "className", "value", "defaultValue", "onChange", "shape", "labelPosition", "direction", "options", "disabled"]); const [val2State, setVal2State] = usePropsValue({ defaultValue: props.defaultValue, value: props.value, finalValue: "", onChange }); const renderOptionsChildren = useCallback(() => { return options === null || options === void 0 ? void 0 : options.map((_a2) => { var { label, value: value2, disabled: disabled2, onChange: onChange2 } = _a2, rest2 = __rest(_a2, ["label", "value", "disabled", "onChange"]); return React.createElement(Radio, Object.assign({}, rest2, { key: value2 === null || value2 === void 0 ? void 0 : value2.toString(), children: label, value: value2, disabled: disabled2, onChange: onChange2, labelPosition, checked: value2 === val2State })); }); }, [options]); return React.createElement( RadioContext.Provider, { value: { labelPosition: labelPosition || "right", disabled, shape, value: val2State, check: (value2) => { setVal2State(value2); }, uncheck: () => { setVal2State(""); }, direction } }, React.createElement("div", Object.assign({ className: classNames(classPrefix, { [`${classPrefix}-${props.direction}`]: props.direction }, className) }, rest), (options === null || options === void 0 ? void 0 : options.length) ? renderOptionsChildren() : children) ); }); RadioGroup.defaultProps = defaultProps$1; RadioGroup.displayName = "NutRadioGroup"; const defaultProps = Object.assign(Object.assign({}, ComponentDefaults), { disabled: false, shape: "round", value: "", labelPosition: "right", icon: null, activeIcon: null, onChange: (checked) => { } }); const Radio$1 = (props) => { const classPrefix2 = "nut-radio"; const _a = Object.assign(Object.assign({}, defaultProps), props), { children, className, style, checked, defaultChecked, shape, value, icon, activeIcon, onChange } = _a, others = __rest(_a, ["children", "className", "style", "checked", "defaultChecked", "shape", "value", "icon", "activeIcon", "onChange"]); let { labelPosition, disabled } = others, rest = __rest( others, ["labelPosition", "disabled"] ); let [checkedStatement, setCheckedStatement] = usePropsValue({ value: checked, defaultValue: defaultChecked, finalValue: false, onChange }); const context = useContext(RadioContext); if (context) { checkedStatement = context.value === value; if (context.labelPosition !== void 0) { labelPosition = context.labelPosition; } if (context.disabled !== void 0) { disabled = context.disabled; } setCheckedStatement = (value2) => { if (value2) { context.check(props.value === void 0 ? "" : props.value); } else { context.uncheck(); } }; } const renderLabel = () => { return React.createElement("div", { className: classNames(`${classPrefix2}-label`, { [`${classPrefix2}-label-disabled`]: disabled }) }, children); }; const renderButton = () => { return React.createElement("div", { className: classNames(`${classPrefix2}-button`, { [`${classPrefix2}-button-active`]: checkedStatement, [`${classPrefix2}-button-disabled`]: disabled }) }, children); }; const color = () => { return { [`${classPrefix2}-iconfont`]: true, [`${classPrefix2}-icon-disabled`]: disabled, [`${classPrefix2}-icon`]: !checkedStatement, [`${classPrefix2}-icon-checked`]: checkedStatement }; }; const renderIcon = () => { const { icon: icon2, activeIcon: activeIcon2 } = props; if (checkedStatement) { return React.isValidElement(activeIcon2) ? React.cloneElement(activeIcon2, Object.assign(Object.assign({}, activeIcon2.props), { className: classNames(color()) })) : React.createElement(CheckChecked, { className: classNames(color()) }); } return React.isValidElement(icon2) ? React.cloneElement(icon2, Object.assign(Object.assign({}, icon2.props), { className: classNames(color()) })) : React.createElement(CheckNormal, { className: classNames(color()) }); }; const renderByShape = (shape2) => { return shape2 === "button" ? renderButton() : React.createElement( React.Fragment, null, renderIcon(), renderLabel() ); }; const renderRadioItem = () => { return renderByShape(context && context.shape ? context.shape : shape); }; const handleClick = (e) => { if (disabled || checkedStatement) return; setCheckedStatement(!checkedStatement); }; return React.createElement("div", Object.assign({ className: classNames(classPrefix2, { [`${classPrefix2}-reverse`]: labelPosition === "left" }, className), style, onClick: handleClick }, rest), renderRadioItem()); }; Radio$1.defaultProps = defaultProps; Radio$1.displayName = "NutRadio"; Radio$1.Group = RadioGroup; const Radio = (props) => { const { className, activeIcon = React.createElement(a, null), icon = React.createElement(a$1, null) } = props, rest = __rest(props, ["className", "activeIcon", "icon"]); const context = useContext(RadioContext); const direction = (context === null || context === void 0 ? void 0 : context.direction) || "horizontal"; return React.createElement(Radio$1, Object.assign({ style: { padding: direction === "horizontal" ? "0" : "" }, className: classNames("mj-radio", className), activeIcon, icon }, rest)); }; Radio.Group = RadioGroup; export { Radio as R, RadioGroup as a };