vantui-edit
Version:
一套适用于Taro3及React的vantui组件库
111 lines (102 loc) • 4.55 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["src", "round", "width", "height", "radius", "lazyLoad", "showMenuByLongpress", "fit", "showError", "showLoading", "className", "style", "renderError", "renderLoading"];
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 { useEffect, useState, useCallback, useMemo, useLayoutEffect } from 'react';
import { View, Image as TaroImage } from '@tarojs/components';
import * as utils from '../wxs/utils';
import VanIcon from '../icon/index';
import * as computed from './wxs';
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
export function Image(props) {
var src = props.src,
round = props.round,
width = props.width,
height = props.height,
radius = props.radius,
lazyLoad = props.lazyLoad,
showMenuByLongpress = props.showMenuByLongpress,
fit = props.fit,
_props$showError = props.showError,
showError = _props$showError === void 0 ? true : _props$showError,
_props$showLoading = props.showLoading,
showLoading = _props$showLoading === void 0 ? true : _props$showLoading,
className = props.className,
style = props.style,
renderError = props.renderError,
renderLoading = props.renderLoading,
others = _objectWithoutProperties(props, _excluded);
var _useState = useState(),
_useState2 = _slicedToArray(_useState, 2),
loading = _useState2[0],
setLoading = _useState2[1];
var _useState3 = useState(false),
_useState4 = _slicedToArray(_useState3, 2),
error = _useState4[0],
setError = _useState4[1];
useLayoutEffect(function () {
if (error) setError(false);
}, // eslint-disable-next-line react-hooks/exhaustive-deps
[src]);
useEffect(function () {
if (loading === undefined) setLoading(true);
setError(false);
}, [loading]);
var onLoad = useCallback(function () {
setLoading(false);
}, []);
var onError = useCallback(function () {
setError(true);
}, []); //样式挂在给img外层的webCompoent
var styleH5 = useMemo(function () {
var style = {};
if (process.env.TARO_ENV === 'h5') {
if (fit === 'heightFix' || fit === 'widthFix') {
style = {
display: 'flex',
alignItems: 'center',
justifyContent: 'center'
};
}
}
return style;
}, [fit]);
return /*#__PURE__*/_jsxs(View, _objectSpread(_objectSpread({
style: utils.style([computed.rootStyle({
width: width,
height: height,
radius: radius
}), style]),
className: ' ' + utils.bem('image', {
round: round
}) + ' ' + className,
onClick: others.onClick
}, others), {}, {
children: [!error && /*#__PURE__*/_jsx(TaroImage, {
src: src,
mode: computed.mode(fit || 'none'),
lazyLoad: lazyLoad,
className: "image-class van-image__img",
showMenuByLongpress: showMenuByLongpress,
onLoad: onLoad,
onError: onError,
style: styleH5
}), loading && showLoading && /*#__PURE__*/_jsx(View, {
className: "loading-class van-image__loading",
children: renderLoading || /*#__PURE__*/_jsx(VanIcon, {
name: "photo",
className: "van-image__loading-icon"
})
}), error && showError && /*#__PURE__*/_jsx(View, {
className: "error-class van-image__error",
children: renderError || /*#__PURE__*/_jsx(VanIcon, {
name: "photo-fail",
className: "van-image__error-icon"
})
})]
}));
}
export default Image;