@fruits-chain/react-native-xiaoshu
Version:
React Native UI library
144 lines (121 loc) • 5.48 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _arrow = require("../icon/helper/arrow");
var _hooks = require("../hooks");
var _helpers = require("../helpers");
var _theme = require("../theme");
var _style = require("./style");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
/**
* Collapse 折叠面板
*/
const Collapse = _ref => {
let {
children,
title,
titleStyle,
titleTextStyle,
iconStyle,
iconColor,
iconSize,
bodyStyle,
renderTitle,
renderBody,
defaultCollapse,
collapse,
onAnimationEnd,
bodyPadding = true
} = _ref;
const THEME_VAR = (0, _theme.useTheme)();
const STYLES = (0, _theme.widthStyle)(THEME_VAR, _style.createStyles);
const onAnimationEndPersistFn = (0, _hooks.usePersistFn)(v => {
onAnimationEnd === null || onAnimationEnd === void 0 ? void 0 : onAnimationEnd(v);
});
/** 记录当前是否课件,在不断 onLayout 的时候可以有一个判断的依据 */
const Visible = (0, _react.useRef)((0, _helpers.isValue)(collapse) ? collapse : (0, _helpers.isValue)(defaultCollapse) ? defaultCollapse : false);
const HeightMap = (0, _react.useRef)({
start: 0,
end: 0
});
const [show, setShow] = (0, _react.useState)(Visible.current);
const AnimatedValue = (0, _react.useRef)(new _reactNative.Animated.Value(0)).current;
const setVisible = (0, _react.useCallback)(v => {
Visible.current = v; // 展开的时候立即响应
if (v) {
setShow(v);
}
const action = _reactNative.Animated.timing(AnimatedValue, {
toValue: v ? HeightMap.current.start + HeightMap.current.end : HeightMap.current.start,
duration: THEME_VAR.collapse_transition_duration,
useNativeDriver: false,
easing: v ? _helpers.easing.easeOutCirc : _helpers.easing.easeInCubic
});
action.start(_ref2 => {
let {
finished
} = _ref2;
if (finished) {
// 收起的时候等待动画结束再响应
if (!v) {
setShow(v);
}
onAnimationEndPersistFn(v);
}
});
}, [AnimatedValue, onAnimationEndPersistFn, THEME_VAR.collapse_transition_duration]);
(0, _hooks.useUpdateEffect)(() => {
if (collapse !== Visible.current) {
// 同步外界状态
setVisible(!Visible.current);
}
}, [collapse, setVisible]); // title 是否要自定义渲染
if (renderTitle) {
title = renderTitle(show);
}
const onPressTitle = (0, _react.useCallback)(() => {
setVisible(!Visible.current);
}, [setVisible]);
const onLayoutTitle = (0, _react.useCallback)(e => {
HeightMap.current.start = e.nativeEvent.layout.height;
if (!Visible.current) {
setVisible(false);
}
}, [setVisible]);
const onLayoutBody = (0, _react.useCallback)(e => {
// 有点疑惑,折叠的过程中,高度在动态变化
HeightMap.current.end = e.nativeEvent.layout.height;
if (Visible.current) {
setVisible(true);
}
}, [setVisible]);
const titleJSX = (0, _helpers.renderTextLikeJSX)(title, [STYLES.title_text, titleTextStyle]);
const ArrowOutline = (0, _arrow.getArrowOutline)(show ? 'up' : 'down');
return /*#__PURE__*/_react.default.createElement(_reactNative.Animated.View, {
style: [STYLES.collapse, {
height: AnimatedValue
}]
}, /*#__PURE__*/_react.default.createElement(_reactNative.TouchableHighlight, {
underlayColor: THEME_VAR.cell_active_color,
onPress: onPressTitle,
onLayout: onLayoutTitle
}, /*#__PURE__*/_react.default.createElement(_reactNative.View, {
style: [STYLES.title, titleStyle]
}, titleJSX, /*#__PURE__*/_react.default.createElement(ArrowOutline, {
style: iconStyle,
color: (0, _helpers.isValue)(iconColor) ? iconColor : THEME_VAR.collapse_title_icon_color,
size: (0, _helpers.isValue)(iconSize) ? iconSize : THEME_VAR.collapse_title_icon_size
}))), /*#__PURE__*/_react.default.createElement(_reactNative.View, {
collapsable: false,
onLayout: onLayoutBody,
style: [STYLES.body, bodyPadding ? STYLES.body_padding : null, bodyStyle]
}, renderBody ? renderBody(show) : children));
};
var _default = /*#__PURE__*/(0, _react.memo)(Collapse);
exports.default = _default;
//# sourceMappingURL=index.js.map