@fruits-chain/react-native-xiaoshu
Version:
🌈 React Native UI library
142 lines (140 loc) • 5.27 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _index = _interopRequireWildcard(require("../helpers/index.js"));
var helpers = _index;
var _index2 = require("../hooks/index.js");
var _index3 = _interopRequireDefault(require("../theme/index.js"));
var _style = require("./style.js");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
/**
* Progress 进度条
*/
const Progress = ({
testID,
theme,
percentage = 0,
pivotText,
color,
trackColor,
pivotColor,
textColor,
strokeHeight,
inactive = false,
showPivot = true,
square = false,
animated = false,
animationDuration,
onAnimationEnd
}) => {
const AnimatedValue = (0, _react.useRef)(new _reactNative.Animated.Value(0)).current;
const StartPercentage = (0, _react.useRef)(percentage);
const onAnimationEndPersistFn = (0, _index2.usePersistFn)(n => {
onAnimationEnd?.(n);
});
const [CV,, TOKENS] = _index3.default.useStyle({
varCreator: _style.varCreator,
theme
});
// 默认值
color = (0, _index.getDefaultValue)(color, CV.progress_color);
if (inactive) {
color = '#cacaca';
}
trackColor = (0, _index.getDefaultValue)(trackColor, CV.progress_background_color);
pivotColor = (0, _index.getDefaultValue)(pivotColor, color);
textColor = (0, _index.getDefaultValue)(textColor, CV.progress_pivot_text_color);
pivotText = (0, _index.getDefaultValue)(pivotText, `${percentage}%`);
strokeHeight = (0, _index.getDefaultValue)(strokeHeight, CV.progress_height);
animationDuration = (0, _index.getDefaultValue)(animationDuration, TOKENS.animation_duration_base);
const borderRadius = square ? 0 : strokeHeight / 2;
const [progressLayout, setProgressLayout] = (0, _react.useState)({
width: 0,
height: 0
});
const [textLayout, setTextLayout] = (0, _react.useState)({
width: 0,
height: 0
});
(0, _react.useEffect)(() => {
const action = _reactNative.Animated.timing(AnimatedValue, {
toValue: progressLayout.width * percentage / 100,
duration: animated ? animationDuration : 0,
easing: helpers.easing.easeInCubic,
useNativeDriver: false
});
action.start(({
finished
}) => {
if (finished) {
onAnimationEndPersistFn(percentage);
}
});
return () => {
action.stop();
};
}, [AnimatedValue, percentage, animationDuration, progressLayout.width, animated, onAnimationEndPersistFn]);
const barStyle = {
position: 'absolute',
left: 0,
top: 0,
width: AnimatedValue,
height: strokeHeight,
backgroundColor: color,
borderRadius: borderRadius
};
const textBoxStyle = {
position: 'absolute',
left: AnimatedValue,
top: 0,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: pivotColor,
paddingHorizontal: CV.progress_pivot_padding_horizontal,
borderRadius: TOKENS.border_radius_max,
transform: [{
translateX: -textLayout.width / 2
}, {
translateY: -(textLayout.height - strokeHeight) / 2
}]
};
const onLayoutProgress = (0, _react.useCallback)(e => {
AnimatedValue.setValue(e.nativeEvent.layout.width * StartPercentage.current / 100);
setProgressLayout(e.nativeEvent.layout);
}, [AnimatedValue]);
const onLayoutText = (0, _react.useCallback)(e => {
setTextLayout(e.nativeEvent.layout);
}, []);
return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
testID: testID,
onLayout: onLayoutProgress,
style: {
height: strokeHeight,
backgroundColor: trackColor,
borderRadius: borderRadius
},
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Animated.View, {
style: barStyle
}), showPivot ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Animated.View, {
onLayout: onLayoutText,
style: textBoxStyle,
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
style: {
color: textColor,
fontSize: CV.progress_pivot_font_size,
lineHeight: CV.progress_pivot_line_height_scale * CV.progress_pivot_font_size
},
children: pivotText
})
}) : null]
});
};
var _default = exports.default = /*#__PURE__*/(0, _react.memo)(Progress);
//# sourceMappingURL=progress.js.map
;