gui-one-nutui-react-taro
Version:
京东风格的轻量级移动端 React 组件库,支持一套代码生成 H5 和小程序
228 lines (226 loc) • 9.25 kB
JavaScript
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
var _excluded = ["className", "value", "onChange", "textPosition", "direction", "options"],
_excluded2 = ["label", "value", "disabled", "onChange"],
_excluded3 = ["className", "disabled", "checked", "shape", "textPosition", "value", "iconName", "iconActiveName", "iconSize", "onChange", "iconClassPrefix", "iconFontClassName"];
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
import React__default, { useState, useEffect, useCallback, useContext } from 'react';
import { I as Icon } from './icon.taro-1d0d4fb7.js';
import { c as cn } from './bem-893ad28d.js';
import { C as ComponentDefaults } from './typings-1c5f2628.js';
var radioContext = {
onChange: function onChange(val) {}
};
var RadioContext = React__default.createContext(radioContext);
var defaultProps$1 = {
value: null,
textPosition: 'right',
onChange: function onChange(value) {},
direction: 'vertical',
options: []
};
var RadioGroup = React__default.forwardRef(function (props, ref) {
var _defaultProps$1$props = _objectSpread(_objectSpread({}, defaultProps$1), props),
children = _defaultProps$1$props.children;
cn('RadioGroup');
var className = props.className,
value = props.value,
_onChange = props.onChange,
textPosition = props.textPosition,
direction = props.direction,
options = props.options,
rest = _objectWithoutProperties(props, _excluded);
var _useState = useState(value),
_useState2 = _slicedToArray(_useState, 2),
val2State = _useState2[0],
setVal2State = _useState2[1];
// useImperativeHandle(ref, () => ({
// toggleAll(state: boolean) {
// console.log(state)
// if (state === false) {
// setInnerValue([])
// } else {
// const childrenLabel: string[] = []
// React.Children.map(children, (child) => {
// const childProps = (child as any).props
// childrenLabel.push(childProps.label || (child as any).children)
// })
// setInnerValue(childrenLabel)
// }
// },
// }))
useEffect(function () {
setVal2State(value);
}, [value]);
// function handleChildChange(state: boolean, label: string) {
// if (innerValue) {
// let clippedValue = []
// if (state) {
// clippedValue = [...innerValue, label]
// } else {
// innerValue?.splice(innerValue?.indexOf(label), 1)
// clippedValue = [...innerValue]
// }
// setInnerValue(clippedValue)
// onChange && onChange(clippedValue)
// }
// }
function validateChildChecked(child) {
if (val2State === null) return false;
return val2State === child.props.value;
}
function validateChecked(value) {
if (val2State === null) return false;
return val2State === value;
}
function cloneChildren() {
return React__default.Children.map(children, function (child, index) {
var childChecked = validateChildChecked(child);
if (child.type.displayName !== 'NutRadio') {
return React__default.cloneElement(child);
}
return React__default.cloneElement(child, {
textPosition: textPosition,
checked: childChecked
// onChange: handleChildChange,
});
});
}
var renderOptionsChildren = useCallback(function () {
return options === null || options === void 0 ? void 0 : options.map(function (_ref) {
var label = _ref.label,
value = _ref.value,
disabled = _ref.disabled,
onChange = _ref.onChange,
rest = _objectWithoutProperties(_ref, _excluded2);
var childChecked = validateChecked(value);
return React__default.createElement(Radio, _objectSpread(_objectSpread({
key: value === null || value === void 0 ? void 0 : value.toString(),
children: label,
value: value,
disabled: disabled,
onChange: onChange
}, rest), {}, {
textPosition: textPosition,
checked: childChecked
}));
});
}, [val2State, options]);
return React__default.createElement(RadioContext.Provider, {
value: {
onChange: function onChange(val) {
setVal2State(val);
_onChange && _onChange(val);
}
}
}, React__default.createElement("div", _objectSpread({
className: "nut-radiogroup nut-radiogroup--".concat(props.direction, " ").concat(className || '')
}, rest), options !== null && options !== void 0 && options.length ? renderOptionsChildren() : cloneChildren()));
});
RadioGroup.defaultProps = defaultProps$1;
RadioGroup.displayName = 'NutRadioGroup';
var defaultProps = _objectSpread(_objectSpread({}, ComponentDefaults), {}, {
disabled: false,
checked: false,
shape: 'round',
value: '',
textPosition: 'right',
iconName: 'check-normal',
iconActiveName: 'check-checked',
iconSize: 18,
onChange: function onChange(e) {}
});
var Radio$1 = function Radio$1(props) {
var _defaultProps$props = _objectSpread(_objectSpread({}, defaultProps), props),
children = _defaultProps$props.children;
var className = props.className,
disabled = props.disabled,
checked = props.checked,
shape = props.shape,
textPosition = props.textPosition,
value = props.value,
iconName = props.iconName,
iconActiveName = props.iconActiveName,
iconSize = props.iconSize,
onChange = props.onChange,
iconClassPrefix = props.iconClassPrefix,
iconFontClassName = props.iconFontClassName,
rest = _objectWithoutProperties(props, _excluded3);
var componentName = 'nut-radio';
var _useState3 = useState(checked),
_useState4 = _slicedToArray(_useState3, 2),
checkedStatement = _useState4[0],
setCheckedStatement = _useState4[1];
var _useState5 = useState(value),
_useState6 = _slicedToArray(_useState5, 2),
valueStatement = _useState6[0],
setValueStatement = _useState6[1];
var _useState7 = useState(disabled),
_useState8 = _slicedToArray(_useState7, 2),
disabledStatement = _useState8[0],
setDisabledStatement = _useState8[1];
var context = useContext(RadioContext);
useEffect(function () {
setDisabledStatement(disabled);
setValueStatement(value);
setCheckedStatement(checked);
}, [disabled, value, checked]);
var renderLabel = function renderLabel() {
return React__default.createElement("div", {
className: "".concat(componentName, "__label ").concat(disabledStatement ? "".concat(componentName, "__label--disabled") : '')
}, children);
};
var renderButton = function renderButton() {
return React__default.createElement("div", {
className: "".concat(componentName, "__button ").concat(checkedStatement && "".concat(componentName, "__button--active"), " ").concat(disabledStatement ? "".concat(componentName, "__button--disabled") : '')
}, children);
};
var color = function color() {
if (disabledStatement) {
return 'nut-radio__icon--disable';
}
if (checkedStatement) {
return 'nut-radio__icon';
}
return 'nut-radio__icon--unchecked';
};
var renderIcon = function renderIcon() {
var iconName = props.iconName,
iconSize = props.iconSize,
iconActiveName = props.iconActiveName;
return React__default.createElement(Icon, {
classPrefix: iconClassPrefix,
fontClassName: iconFontClassName,
name: checkedStatement ? iconActiveName : iconName,
size: iconSize,
className: color()
});
};
var renderRadioItem = function renderRadioItem() {
if (shape === 'button') {
return renderButton();
}
if (reverseState) {
return [renderLabel(), renderIcon()];
}
return [renderIcon(), renderLabel()];
};
var handleClick = function handleClick(e) {
if (disabledStatement || checkedStatement) return;
setCheckedStatement(!checkedStatement);
onChange && onChange(e);
context && context.onChange(valueStatement);
};
var reverseState = textPosition === 'left';
return React__default.createElement("div", _objectSpread({
className: "nut-radio ".concat(className),
onClick: handleClick
}, rest), renderRadioItem());
};
Radio$1.defaultProps = defaultProps;
Radio$1.displayName = 'NutRadio';
Radio$1.RadioGroup = RadioGroup;
var Radio = Radio$1;
export { Radio as R, RadioGroup as a };