@nutui/nutui-react
Version:
京东风格的轻量级移动端 React 组件库,支持一套代码生成 H5 和小程序
51 lines (50 loc) • 2.23 kB
JavaScript
import { _ as __rest } from "./tslib.es6.js";
import React__default, { useCallback } from "react";
import classNames from "classnames";
import { R as Radio, a as RadioContext } from "./radio2.js";
import { u as usePropsValue } from "./use-props-value.js";
const defaultProps = {
labelPosition: "right",
onChange: (value) => {
},
direction: "vertical",
options: []
};
const classPrefix = "nut-radiogroup";
const RadioGroup = React__default.forwardRef((props, ref) => {
const _a = Object.assign(Object.assign({}, defaultProps), 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__default.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__default.createElement(
RadioContext.Provider,
{ value: {
labelPosition: labelPosition || "right",
disabled,
shape,
value: val2State,
check: (value2) => {
setVal2State(value2);
},
uncheck: () => {
setVal2State("");
}
} },
React__default.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.displayName = "NutRadioGroup";
export {
RadioGroup as default
};