zarm
Version:
基于 React 的移动端UI库
121 lines (108 loc) • 4.33 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import { createBEM } from '@zarm-design/bem';
import isEqual from 'lodash/isEqual';
import * as React from 'react';
import { ConfigContext } from '../config-provider';
import { useSafeState } from '../utils/hooks';
import Wheel from '../wheel';
import { isCascader, resolved } from './utils';
var PickerView = /*#__PURE__*/React.forwardRef(function (props, ref) {
var className = props.className,
style = props.style,
cols = props.cols,
dataSource = props.dataSource,
fieldNames = props.fieldNames,
itemRender = props.itemRender,
disabled = props.disabled,
onChange = props.onChange;
var _React$useContext = React.useContext(ConfigContext),
prefixCls = _React$useContext.prefixCls;
var bem = createBEM('picker-view', {
prefixCls: prefixCls
});
var _React$useState = React.useState(resolved(props).value),
_React$useState2 = _slicedToArray(_React$useState, 2),
innerValue = _React$useState2[0],
setInnerValue = _React$useState2[1];
var _useSafeState = useSafeState(false),
_useSafeState2 = _slicedToArray(_useSafeState, 2),
stopScroll = _useSafeState2[0],
setStopScroll = _useSafeState2[1];
React.useEffect(function () {
if (props.value === undefined) return;
if (isEqual(props.value, innerValue)) return;
setInnerValue(resolved(props).value);
}, [props.value]);
var _React$useMemo = React.useMemo(function () {
return resolved(_objectSpread(_objectSpread({}, props), {}, {
value: innerValue
}));
}, [cols, innerValue, dataSource, fieldNames]),
columns = _React$useMemo.columns,
items = _React$useMemo.items;
var reset = function reset() {
setStopScroll(true);
setTimeout(function () {
setInnerValue(resolved(props).value);
setStopScroll(false);
}, 0);
};
React.useImperativeHandle(ref, function () {
return {
value: innerValue,
items: items,
reset: reset
};
});
var onValueChange = React.useCallback(function (selected, index) {
var value = innerValue.slice();
if (isCascader(props.dataSource)) {
value.length = index + 1;
}
value[index] = selected;
var next = resolved(_objectSpread(_objectSpread({}, props), {}, {
value: value
}));
setInnerValue(next.value);
onChange === null || onChange === void 0 ? void 0 : onChange(next.value, next.items, index);
}, [innerValue]);
return /*#__PURE__*/React.createElement("div", {
className: bem([className]),
style: style
}, /*#__PURE__*/React.createElement("div", {
className: bem('content')
}, columns.map(function (column, index) {
return /*#__PURE__*/React.createElement(Wheel, {
key: +index,
dataSource: column,
value: innerValue === null || innerValue === void 0 ? void 0 : innerValue[index],
fieldNames: fieldNames,
itemRender: itemRender ? function (item) {
return itemRender(item, index);
} : undefined,
disabled: disabled,
onChange: function onChange(selected) {
return onValueChange(selected, index);
},
stopScroll: stopScroll
});
})), /*#__PURE__*/React.createElement("div", {
className: bem('mask', [{
top: true
}])
}), /*#__PURE__*/React.createElement("div", {
className: bem('mask', [{
bottom: true
}])
}));
});
PickerView.defaultProps = {
defaultValue: [],
dataSource: [],
cols: Infinity,
disabled: false
};
export default PickerView;