@os-design/upload 1.0.291 → 1.0.293
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/ImageUpload/index.tsx"],"names":[],"mappings":"AAIA,OAAO,EACL,KAAK,QAAQ,EAId,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/ImageUpload/index.tsx"],"names":[],"mappings":"AAIA,OAAO,EACL,KAAK,QAAQ,EAId,MAAM,mBAAmB,CAAC;AAI3B,OAAO,EAAE,KAAK,MAAM,EAAe,MAAM,gBAAgB,CAAC;AAC1D,OAAsB,EACpB,KAAK,iBAAiB,EACvB,MAAM,0BAA0B,CAAC;AAElC,KAAK,WAAW,GAAG,IAAI,CACrB,GAAG,CAAC,iBAAiB,CAAC,KAAK,CAAC,EAC5B,cAAc,GAAG,UAAU,GAAG,KAAK,CACpC,CAAC;AACF,MAAM,WAAW,gBAAiB,SAAQ,WAAW,EAAE,QAAQ;IAC7D;;;OAGG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;IACb;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;;OAGG;IACH,MAAM,CAAC,EAAE,iBAAiB,CAAC;IAC3B;;;OAGG;IACH,KAAK,CAAC,EAAE,IAAI,GAAG,IAAI,CAAC;IACpB;;;OAGG;IACH,YAAY,CAAC,EAAE,IAAI,GAAG,IAAI,CAAC;IAC3B;;;OAGG;IACH,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,IAAI,GAAG,IAAI,KAAK,IAAI,CAAC;CACzC;AAsBD,UAAU,YAAY;IACpB,YAAY,EAAE,OAAO,CAAC;IACtB,QAAQ,EAAE,OAAO,CAAC;CACnB;AACD,eAAO,MAAM,OAAO;;SA8IhB,MAAI,WAAW;wHAxHlB,CAAC;AAgGF;;GAEG;AACH,QAAA,MAAM,WAAW,6GAkGhB,CAAC;AAIF,eAAe,WAAW,CAAC"}
|
|
@@ -4,7 +4,7 @@ import { Button, Image } from '@os-design/core';
|
|
|
4
4
|
import { Delete, Picture } from '@os-design/icons';
|
|
5
5
|
import { resetFocusStyles, sizeStyles, transitionStyles } from '@os-design/styles';
|
|
6
6
|
import { ThemeOverrider, clr } from '@os-design/theming';
|
|
7
|
-
import { isTouchDevice, omitEmotionProps
|
|
7
|
+
import { isTouchDevice, omitEmotionProps } from '@os-design/utils';
|
|
8
8
|
import { forwardRef, useCallback, useMemo } from 'react';
|
|
9
9
|
import { useDropzone } from 'react-dropzone';
|
|
10
10
|
import defaultLocale from './utils/defaultLocale.js';
|
|
@@ -121,16 +121,18 @@ const ImageUpload = /*#__PURE__*/forwardRef(({
|
|
|
121
121
|
locale = defaultLocale,
|
|
122
122
|
value,
|
|
123
123
|
defaultValue,
|
|
124
|
-
onChange,
|
|
124
|
+
onChange = () => {},
|
|
125
125
|
size,
|
|
126
126
|
...rest
|
|
127
127
|
}, ref) => {
|
|
128
|
+
// Do not use useForwardedState here!
|
|
129
|
+
// Edge case:
|
|
130
|
+
// 1. Component has a photo: url is specified, value is undefined (inner value is used).
|
|
131
|
+
// 2. The user deletes the photo: url is specified, value is null (inner value is null)
|
|
132
|
+
// 3. The user chooses another photo: url is specified, value is a file (inner value is not used).
|
|
133
|
+
// 4. The user is uploaded the photo: url is changed, value is undefined (inner value is used, but it's null).
|
|
134
|
+
|
|
128
135
|
const touchDevice = useMemo(() => isTouchDevice(), []);
|
|
129
|
-
const [forwardedValue, setForwardedValue] = useForwardedState({
|
|
130
|
-
value,
|
|
131
|
-
defaultValue,
|
|
132
|
-
onChange
|
|
133
|
-
});
|
|
134
136
|
const {
|
|
135
137
|
getRootProps,
|
|
136
138
|
getInputProps,
|
|
@@ -138,18 +140,18 @@ const ImageUpload = /*#__PURE__*/forwardRef(({
|
|
|
138
140
|
} = useDropzone({
|
|
139
141
|
accept,
|
|
140
142
|
multiple: false,
|
|
141
|
-
onDrop: ([file]) =>
|
|
143
|
+
onDrop: ([file]) => onChange(file)
|
|
142
144
|
});
|
|
143
145
|
const source = useMemo(() => {
|
|
144
|
-
if (
|
|
145
|
-
if (
|
|
146
|
+
if (value === null) return null; // If the user select an image
|
|
147
|
+
if (value) return URL.createObjectURL(value); // If the image already exists
|
|
146
148
|
if (url) return url;
|
|
147
149
|
return null;
|
|
148
|
-
}, [
|
|
150
|
+
}, [value, url]);
|
|
149
151
|
const hasImage = useMemo(() => !!source, [source]);
|
|
150
152
|
const renderImage = useCallback(() => {
|
|
151
153
|
if (!source) return null;
|
|
152
|
-
if (typeof
|
|
154
|
+
if (typeof value === 'object' && value !== null) {
|
|
153
155
|
return /*#__PURE__*/_jsx(LocalImage, {
|
|
154
156
|
src: source
|
|
155
157
|
});
|
|
@@ -157,7 +159,7 @@ const ImageUpload = /*#__PURE__*/forwardRef(({
|
|
|
157
159
|
return /*#__PURE__*/_jsx(RemoteImage, {
|
|
158
160
|
url: source
|
|
159
161
|
});
|
|
160
|
-
}, [
|
|
162
|
+
}, [value, source]);
|
|
161
163
|
return /*#__PURE__*/_jsxs(Container, {
|
|
162
164
|
isDragActive: isDragActive,
|
|
163
165
|
hasImage: hasImage,
|
|
@@ -171,7 +173,7 @@ const ImageUpload = /*#__PURE__*/forwardRef(({
|
|
|
171
173
|
hasImage: hasImage,
|
|
172
174
|
...getRootProps({
|
|
173
175
|
onKeyDown: e => {
|
|
174
|
-
if (e.key === 'Backspace')
|
|
176
|
+
if (e.key === 'Backspace') onChange(null);
|
|
175
177
|
}
|
|
176
178
|
}) // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
177
179
|
,
|
|
@@ -190,7 +192,7 @@ const ImageUpload = /*#__PURE__*/forwardRef(({
|
|
|
190
192
|
type: "ghost",
|
|
191
193
|
wide: "never",
|
|
192
194
|
onClick: e => {
|
|
193
|
-
|
|
195
|
+
onChange(null);
|
|
194
196
|
e.stopPropagation();
|
|
195
197
|
},
|
|
196
198
|
children: /*#__PURE__*/_jsx(Delete, {})
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@os-design/upload",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.293",
|
|
4
4
|
"license": "UNLICENSED",
|
|
5
5
|
"repository": "git@gitlab.com:os-team/libs/os-design.git",
|
|
6
6
|
"type": "module",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"access": "public"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@os-design/core": "^1.0.
|
|
32
|
+
"@os-design/core": "^1.0.289",
|
|
33
33
|
"@os-design/icons": "^1.0.69",
|
|
34
34
|
"@os-design/styles": "^1.0.66",
|
|
35
35
|
"@os-design/theming": "^1.0.62",
|
|
@@ -46,5 +46,5 @@
|
|
|
46
46
|
"react": "18",
|
|
47
47
|
"react-dom": "18"
|
|
48
48
|
},
|
|
49
|
-
"gitHead": "
|
|
49
|
+
"gitHead": "6e6cab51eac9762715864797a4c6df89c0573912"
|
|
50
50
|
}
|
|
@@ -9,11 +9,7 @@ import {
|
|
|
9
9
|
transitionStyles,
|
|
10
10
|
} from '@os-design/styles';
|
|
11
11
|
import { ThemeOverrider, clr } from '@os-design/theming';
|
|
12
|
-
import {
|
|
13
|
-
isTouchDevice,
|
|
14
|
-
omitEmotionProps,
|
|
15
|
-
useForwardedState,
|
|
16
|
-
} from '@os-design/utils';
|
|
12
|
+
import { isTouchDevice, omitEmotionProps } from '@os-design/utils';
|
|
17
13
|
import { forwardRef, useCallback, useMemo } from 'react';
|
|
18
14
|
import { type Accept, useDropzone } from 'react-dropzone';
|
|
19
15
|
import defaultLocale, {
|
|
@@ -210,41 +206,43 @@ const ImageUpload = forwardRef<HTMLDivElement, ImageUploadProps>(
|
|
|
210
206
|
locale = defaultLocale,
|
|
211
207
|
value,
|
|
212
208
|
defaultValue,
|
|
213
|
-
onChange,
|
|
209
|
+
onChange = () => {},
|
|
214
210
|
size,
|
|
215
211
|
...rest
|
|
216
212
|
},
|
|
217
213
|
ref
|
|
218
214
|
) => {
|
|
215
|
+
// Do not use useForwardedState here!
|
|
216
|
+
// Edge case:
|
|
217
|
+
// 1. Component has a photo: url is specified, value is undefined (inner value is used).
|
|
218
|
+
// 2. The user deletes the photo: url is specified, value is null (inner value is null)
|
|
219
|
+
// 3. The user chooses another photo: url is specified, value is a file (inner value is not used).
|
|
220
|
+
// 4. The user is uploaded the photo: url is changed, value is undefined (inner value is used, but it's null).
|
|
221
|
+
|
|
219
222
|
const touchDevice = useMemo(() => isTouchDevice(), []);
|
|
220
|
-
const [forwardedValue, setForwardedValue] = useForwardedState({
|
|
221
|
-
value,
|
|
222
|
-
defaultValue,
|
|
223
|
-
onChange,
|
|
224
|
-
});
|
|
225
223
|
|
|
226
224
|
const { getRootProps, getInputProps, isDragActive } = useDropzone({
|
|
227
225
|
accept,
|
|
228
226
|
multiple: false,
|
|
229
|
-
onDrop: ([file]) =>
|
|
227
|
+
onDrop: ([file]) => onChange(file),
|
|
230
228
|
});
|
|
231
229
|
|
|
232
230
|
const source = useMemo(() => {
|
|
233
|
-
if (
|
|
234
|
-
if (
|
|
231
|
+
if (value === null) return null; // If the user select an image
|
|
232
|
+
if (value) return URL.createObjectURL(value); // If the image already exists
|
|
235
233
|
if (url) return url;
|
|
236
234
|
return null;
|
|
237
|
-
}, [
|
|
235
|
+
}, [value, url]);
|
|
238
236
|
|
|
239
237
|
const hasImage = useMemo(() => !!source, [source]);
|
|
240
238
|
|
|
241
239
|
const renderImage = useCallback(() => {
|
|
242
240
|
if (!source) return null;
|
|
243
|
-
if (typeof
|
|
241
|
+
if (typeof value === 'object' && value !== null) {
|
|
244
242
|
return <LocalImage src={source} />;
|
|
245
243
|
}
|
|
246
244
|
return <RemoteImage url={source} />;
|
|
247
|
-
}, [
|
|
245
|
+
}, [value, source]);
|
|
248
246
|
|
|
249
247
|
return (
|
|
250
248
|
<Container
|
|
@@ -263,7 +261,7 @@ const ImageUpload = forwardRef<HTMLDivElement, ImageUploadProps>(
|
|
|
263
261
|
{
|
|
264
262
|
...getRootProps({
|
|
265
263
|
onKeyDown: (e) => {
|
|
266
|
-
if (e.key === 'Backspace')
|
|
264
|
+
if (e.key === 'Backspace') onChange(null);
|
|
267
265
|
},
|
|
268
266
|
} as any) // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
269
267
|
}
|
|
@@ -285,7 +283,7 @@ const ImageUpload = forwardRef<HTMLDivElement, ImageUploadProps>(
|
|
|
285
283
|
type='ghost'
|
|
286
284
|
wide='never'
|
|
287
285
|
onClick={(e) => {
|
|
288
|
-
|
|
286
|
+
onChange(null);
|
|
289
287
|
e.stopPropagation();
|
|
290
288
|
}}
|
|
291
289
|
>
|