@pingux/astro 1.12.0-alpha.0 → 1.12.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/components/Image/Image.js +17 -8
- package/lib/cjs/components/Image/Image.stories.js +46 -3
- package/lib/cjs/components/Image/Image.test.js +26 -1
- package/lib/components/Image/Image.js +18 -9
- package/lib/components/Image/Image.stories.js +29 -1
- package/lib/components/Image/Image.test.js +26 -1
- package/package.json +1 -1
@@ -88,7 +88,12 @@ var Image = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
|
|
88
88
|
var _useState3 = (0, _react.useState)(false),
|
89
89
|
_useState4 = (0, _slicedToArray2["default"])(_useState3, 2),
|
90
90
|
loadedSuccessfully = _useState4[0],
|
91
|
-
setLoadedSuccessfully = _useState4[1];
|
91
|
+
setLoadedSuccessfully = _useState4[1];
|
92
|
+
|
93
|
+
var _useState5 = (0, _react.useState)(false),
|
94
|
+
_useState6 = (0, _slicedToArray2["default"])(_useState5, 2),
|
95
|
+
hasTimedOut = _useState6[0],
|
96
|
+
setHasTimedOut = _useState6[1]; // we need to use useRef here with useState so it will be updated in setTimeout and onload
|
92
97
|
// https://github.com/facebook/react/issues/14010#issuecomment-433788147
|
93
98
|
|
94
99
|
|
@@ -99,11 +104,17 @@ var Image = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
|
|
99
104
|
isLoadingRef.current = newState;
|
100
105
|
};
|
101
106
|
|
102
|
-
var
|
103
|
-
|
104
|
-
|
105
|
-
|
107
|
+
var setImgSrc = function setImgSrc() {
|
108
|
+
if (!loadedSuccessfully && !(isLoadingRef !== null && isLoadingRef !== void 0 && isLoadingRef.current) || hasTimedOut) {
|
109
|
+
return fallbackImage;
|
110
|
+
}
|
111
|
+
|
112
|
+
return src;
|
113
|
+
};
|
106
114
|
|
115
|
+
var imgSrc = (0, _react.useMemo)(function () {
|
116
|
+
return setImgSrc();
|
117
|
+
}, [src, isLoading, hasTimedOut]);
|
107
118
|
var imgRef = (0, _react.useRef)();
|
108
119
|
/* istanbul ignore next */
|
109
120
|
|
@@ -126,20 +137,18 @@ var Image = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
|
|
126
137
|
var onImageLoad = function onImageLoad() {
|
127
138
|
if (isLoadingRef !== null && isLoadingRef !== void 0 && isLoadingRef.current) {
|
128
139
|
setIsLoadingWithRef(false);
|
129
|
-
setImgSrc(src);
|
130
140
|
setLoadedSuccessfully(true);
|
131
141
|
}
|
132
142
|
};
|
133
143
|
|
134
144
|
var onImageError = function onImageError() {
|
135
145
|
setIsLoadingWithRef(false);
|
136
|
-
setImgSrc(fallbackImage || null);
|
137
146
|
};
|
138
147
|
|
139
148
|
var onFallbackTimeout = function onFallbackTimeout() {
|
140
149
|
if (isLoadingRef !== null && isLoadingRef !== void 0 && isLoadingRef.current) {
|
141
150
|
setIsLoadingWithRef(false);
|
142
|
-
|
151
|
+
setHasTimedOut(true);
|
143
152
|
}
|
144
153
|
};
|
145
154
|
|
@@ -2,17 +2,25 @@
|
|
2
2
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
|
4
4
|
|
5
|
+
var _Object$getOwnPropertyDescriptor = require("@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor");
|
6
|
+
|
5
7
|
var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");
|
6
8
|
|
9
|
+
var _typeof = require("@babel/runtime-corejs3/helpers/typeof");
|
10
|
+
|
11
|
+
var _WeakMap = require("@babel/runtime-corejs3/core-js-stable/weak-map");
|
12
|
+
|
7
13
|
_Object$defineProperty(exports, "__esModule", {
|
8
14
|
value: true
|
9
15
|
});
|
10
16
|
|
11
|
-
exports["default"] = exports.WithSkeletonLoadTimeout = exports.WithSkeletonLoadSuccess = exports.FallbackImage = exports.Disabled = exports.Default = exports.CustomSizeAndRadius = exports.Avatar = void 0;
|
17
|
+
exports["default"] = exports.WithSkeletonLoadTimeout = exports.WithSkeletonLoadSuccess = exports.UpdatingImageSrc = exports.FallbackImage = exports.Disabled = exports.Default = exports.CustomSizeAndRadius = exports.Avatar = void 0;
|
18
|
+
|
19
|
+
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/slicedToArray"));
|
12
20
|
|
13
21
|
var _extends2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/extends"));
|
14
22
|
|
15
|
-
var _react =
|
23
|
+
var _react = _interopRequireWildcard(require("react"));
|
16
24
|
|
17
25
|
var _isChromatic = _interopRequireDefault(require("chromatic/isChromatic"));
|
18
26
|
|
@@ -22,8 +30,14 @@ var _imageConstants = require("./imageConstants");
|
|
22
30
|
|
23
31
|
var _htmlElements = require("../../utils/devUtils/constants/htmlElements");
|
24
32
|
|
33
|
+
var _index = require("../../index");
|
34
|
+
|
25
35
|
var _react2 = require("@emotion/react");
|
26
36
|
|
37
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof _WeakMap !== "function") return null; var cacheBabelInterop = new _WeakMap(); var cacheNodeInterop = new _WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
38
|
+
|
39
|
+
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; }
|
40
|
+
|
27
41
|
var _default = {
|
28
42
|
title: 'Image',
|
29
43
|
component: _["default"],
|
@@ -130,4 +144,33 @@ var WithSkeletonLoadTimeout = function WithSkeletonLoadTimeout() {
|
|
130
144
|
});
|
131
145
|
};
|
132
146
|
|
133
|
-
exports.WithSkeletonLoadTimeout = WithSkeletonLoadTimeout;
|
147
|
+
exports.WithSkeletonLoadTimeout = WithSkeletonLoadTimeout;
|
148
|
+
|
149
|
+
var UpdatingImageSrc = function UpdatingImageSrc() {
|
150
|
+
var _useState = (0, _react.useState)(_imageConstants.chiefIdentityChampions),
|
151
|
+
_useState2 = (0, _slicedToArray2["default"])(_useState, 2),
|
152
|
+
image = _useState2[0],
|
153
|
+
setImage = _useState2[1];
|
154
|
+
|
155
|
+
var handleButtonPress = function handleButtonPress() {
|
156
|
+
var src = image === _imageConstants.pingImg ? _imageConstants.chiefIdentityChampions : _imageConstants.pingImg;
|
157
|
+
setImage(src);
|
158
|
+
};
|
159
|
+
|
160
|
+
return (0, _react2.jsx)(_index.Box, {
|
161
|
+
sx: {
|
162
|
+
width: '200px'
|
163
|
+
}
|
164
|
+
}, (0, _react2.jsx)(_index.Button, {
|
165
|
+
onPress: handleButtonPress
|
166
|
+
}, "Change Image"), (0, _react2.jsx)(_["default"], {
|
167
|
+
src: image,
|
168
|
+
sx: {
|
169
|
+
width: '200px',
|
170
|
+
height: '200px',
|
171
|
+
mt: '25px'
|
172
|
+
}
|
173
|
+
}));
|
174
|
+
};
|
175
|
+
|
176
|
+
exports.UpdatingImageSrc = UpdatingImageSrc;
|
@@ -48,7 +48,12 @@ var defaultProps = {
|
|
48
48
|
|
49
49
|
var getComponent = function getComponent() {
|
50
50
|
var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
51
|
-
|
51
|
+
|
52
|
+
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
|
53
|
+
_ref$renderFn = _ref.renderFn,
|
54
|
+
renderFn = _ref$renderFn === void 0 ? _testWrapper.render : _ref$renderFn;
|
55
|
+
|
56
|
+
return renderFn((0, _react3.jsx)(_["default"], (0, _extends2["default"])({}, defaultProps, props)));
|
52
57
|
}; // Need to be added to each test file to test accessibility using axe.
|
53
58
|
|
54
59
|
|
@@ -96,6 +101,26 @@ test('image shows disabled status', function () {
|
|
96
101
|
|
97
102
|
expect(img).toHaveClass('is-disabled');
|
98
103
|
});
|
104
|
+
test('image source is able to be changed', function () {
|
105
|
+
var src2 = 'second-src';
|
106
|
+
|
107
|
+
var _getComponent = getComponent(),
|
108
|
+
rerender = _getComponent.rerender;
|
109
|
+
|
110
|
+
(0, _react2.act)(function () {
|
111
|
+
fallbackImageObj.onImageLoad();
|
112
|
+
});
|
113
|
+
expect(_testWrapper.screen.getByRole('img')).toHaveAttribute('src', testSrc);
|
114
|
+
getComponent({
|
115
|
+
src: src2
|
116
|
+
}, {
|
117
|
+
renderFn: rerender
|
118
|
+
});
|
119
|
+
(0, _react2.act)(function () {
|
120
|
+
fallbackImageObj.onImageLoad();
|
121
|
+
});
|
122
|
+
expect(_testWrapper.screen.getByRole('img')).toHaveAttribute('src', src2);
|
123
|
+
});
|
99
124
|
describe('test Image component with useFallbackImage hook', function () {
|
100
125
|
test('image component will use src if loaded correctly', function () {
|
101
126
|
getComponent();
|
@@ -20,7 +20,7 @@ function ownKeys(object, enumerableOnly) { var keys = _Object$keys(object); if (
|
|
20
20
|
|
21
21
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var _context, _context2; var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? _forEachInstanceProperty(_context = ownKeys(Object(source), !0)).call(_context, function (key) { _defineProperty(target, key, source[key]); }) : _Object$getOwnPropertyDescriptors ? _Object$defineProperties(target, _Object$getOwnPropertyDescriptors(source)) : _forEachInstanceProperty(_context2 = ownKeys(Object(source))).call(_context2, function (key) { _Object$defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key)); }); } return target; }
|
22
22
|
|
23
|
-
import React, { forwardRef, useRef, useImperativeHandle, useState } from 'react';
|
23
|
+
import React, { forwardRef, useRef, useImperativeHandle, useState, useMemo } from 'react';
|
24
24
|
import PropTypes from 'prop-types';
|
25
25
|
import { Image as ThemeUIImage } from 'theme-ui';
|
26
26
|
import { useHover } from '@react-aria/interactions';
|
@@ -52,7 +52,12 @@ var Image = /*#__PURE__*/forwardRef(function (props, ref) {
|
|
52
52
|
var _useState3 = useState(false),
|
53
53
|
_useState4 = _slicedToArray(_useState3, 2),
|
54
54
|
loadedSuccessfully = _useState4[0],
|
55
|
-
setLoadedSuccessfully = _useState4[1];
|
55
|
+
setLoadedSuccessfully = _useState4[1];
|
56
|
+
|
57
|
+
var _useState5 = useState(false),
|
58
|
+
_useState6 = _slicedToArray(_useState5, 2),
|
59
|
+
hasTimedOut = _useState6[0],
|
60
|
+
setHasTimedOut = _useState6[1]; // we need to use useRef here with useState so it will be updated in setTimeout and onload
|
56
61
|
// https://github.com/facebook/react/issues/14010#issuecomment-433788147
|
57
62
|
|
58
63
|
|
@@ -63,11 +68,17 @@ var Image = /*#__PURE__*/forwardRef(function (props, ref) {
|
|
63
68
|
isLoadingRef.current = newState;
|
64
69
|
};
|
65
70
|
|
66
|
-
var
|
67
|
-
|
68
|
-
|
69
|
-
|
71
|
+
var setImgSrc = function setImgSrc() {
|
72
|
+
if (!loadedSuccessfully && !(isLoadingRef !== null && isLoadingRef !== void 0 && isLoadingRef.current) || hasTimedOut) {
|
73
|
+
return fallbackImage;
|
74
|
+
}
|
75
|
+
|
76
|
+
return src;
|
77
|
+
};
|
70
78
|
|
79
|
+
var imgSrc = useMemo(function () {
|
80
|
+
return setImgSrc();
|
81
|
+
}, [src, isLoading, hasTimedOut]);
|
71
82
|
var imgRef = useRef();
|
72
83
|
/* istanbul ignore next */
|
73
84
|
|
@@ -90,20 +101,18 @@ var Image = /*#__PURE__*/forwardRef(function (props, ref) {
|
|
90
101
|
var onImageLoad = function onImageLoad() {
|
91
102
|
if (isLoadingRef !== null && isLoadingRef !== void 0 && isLoadingRef.current) {
|
92
103
|
setIsLoadingWithRef(false);
|
93
|
-
setImgSrc(src);
|
94
104
|
setLoadedSuccessfully(true);
|
95
105
|
}
|
96
106
|
};
|
97
107
|
|
98
108
|
var onImageError = function onImageError() {
|
99
109
|
setIsLoadingWithRef(false);
|
100
|
-
setImgSrc(fallbackImage || null);
|
101
110
|
};
|
102
111
|
|
103
112
|
var onFallbackTimeout = function onFallbackTimeout() {
|
104
113
|
if (isLoadingRef !== null && isLoadingRef !== void 0 && isLoadingRef.current) {
|
105
114
|
setIsLoadingWithRef(false);
|
106
|
-
|
115
|
+
setHasTimedOut(true);
|
107
116
|
}
|
108
117
|
};
|
109
118
|
|
@@ -1,9 +1,11 @@
|
|
1
|
+
import _slicedToArray from "@babel/runtime-corejs3/helpers/esm/slicedToArray";
|
1
2
|
import _extends from "@babel/runtime-corejs3/helpers/esm/extends";
|
2
|
-
import React from 'react';
|
3
|
+
import React, { useState } from 'react';
|
3
4
|
import isChromatic from 'chromatic/isChromatic';
|
4
5
|
import Image from '.';
|
5
6
|
import { chiefIdentityChampions, pingImg } from './imageConstants';
|
6
7
|
import { htmlElements } from '../../utils/devUtils/constants/htmlElements';
|
8
|
+
import { Box, Button } from '../../index';
|
7
9
|
import { jsx as ___EmotionJSX } from "@emotion/react";
|
8
10
|
export default {
|
9
11
|
title: 'Image',
|
@@ -90,4 +92,30 @@ export var WithSkeletonLoadTimeout = function WithSkeletonLoadTimeout() {
|
|
90
92
|
height: '150px'
|
91
93
|
}
|
92
94
|
});
|
95
|
+
};
|
96
|
+
export var UpdatingImageSrc = function UpdatingImageSrc() {
|
97
|
+
var _useState = useState(chiefIdentityChampions),
|
98
|
+
_useState2 = _slicedToArray(_useState, 2),
|
99
|
+
image = _useState2[0],
|
100
|
+
setImage = _useState2[1];
|
101
|
+
|
102
|
+
var handleButtonPress = function handleButtonPress() {
|
103
|
+
var src = image === pingImg ? chiefIdentityChampions : pingImg;
|
104
|
+
setImage(src);
|
105
|
+
};
|
106
|
+
|
107
|
+
return ___EmotionJSX(Box, {
|
108
|
+
sx: {
|
109
|
+
width: '200px'
|
110
|
+
}
|
111
|
+
}, ___EmotionJSX(Button, {
|
112
|
+
onPress: handleButtonPress
|
113
|
+
}, "Change Image"), ___EmotionJSX(Image, {
|
114
|
+
src: image,
|
115
|
+
sx: {
|
116
|
+
width: '200px',
|
117
|
+
height: '200px',
|
118
|
+
mt: '25px'
|
119
|
+
}
|
120
|
+
}));
|
93
121
|
};
|
@@ -29,7 +29,12 @@ var defaultProps = {
|
|
29
29
|
|
30
30
|
var getComponent = function getComponent() {
|
31
31
|
var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
32
|
-
|
32
|
+
|
33
|
+
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
|
34
|
+
_ref$renderFn = _ref.renderFn,
|
35
|
+
renderFn = _ref$renderFn === void 0 ? render : _ref$renderFn;
|
36
|
+
|
37
|
+
return renderFn(___EmotionJSX(Image, _extends({}, defaultProps, props)));
|
33
38
|
}; // Need to be added to each test file to test accessibility using axe.
|
34
39
|
|
35
40
|
|
@@ -71,6 +76,26 @@ test('image shows disabled status', function () {
|
|
71
76
|
var img = screen.getByRole('img');
|
72
77
|
expect(img).toHaveClass('is-disabled');
|
73
78
|
});
|
79
|
+
test('image source is able to be changed', function () {
|
80
|
+
var src2 = 'second-src';
|
81
|
+
|
82
|
+
var _getComponent = getComponent(),
|
83
|
+
rerender = _getComponent.rerender;
|
84
|
+
|
85
|
+
act(function () {
|
86
|
+
fallbackImageObj.onImageLoad();
|
87
|
+
});
|
88
|
+
expect(screen.getByRole('img')).toHaveAttribute('src', testSrc);
|
89
|
+
getComponent({
|
90
|
+
src: src2
|
91
|
+
}, {
|
92
|
+
renderFn: rerender
|
93
|
+
});
|
94
|
+
act(function () {
|
95
|
+
fallbackImageObj.onImageLoad();
|
96
|
+
});
|
97
|
+
expect(screen.getByRole('img')).toHaveAttribute('src', src2);
|
98
|
+
});
|
74
99
|
describe('test Image component with useFallbackImage hook', function () {
|
75
100
|
test('image component will use src if loaded correctly', function () {
|
76
101
|
getComponent();
|