@norges-domstoler/dds-components 10.1.0 → 10.1.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/dist/_virtual/_rollupPluginBabelHelpers.js +292 -1
- package/dist/cjs/index.d.ts +2 -0
- package/dist/cjs/index.js +1628 -588
- package/dist/components/DescriptionList/DescriptionList.js +1 -1
- package/dist/components/FileUploader/ErrorList.js +28 -0
- package/dist/components/FileUploader/File.js +69 -0
- package/dist/components/FileUploader/FileUploader.js +153 -0
- package/dist/components/FileUploader/FileUploader.tokens.js +31 -0
- package/dist/components/FileUploader/fileUploaderReducer.js +35 -0
- package/dist/components/FileUploader/useFileUploader.js +202 -0
- package/dist/components/FileUploader/utils.js +32 -0
- package/dist/components/FormGenerator/FormGenerator.js +239 -0
- package/dist/components/FormGenerator/FormGenerator.styles.js +39 -0
- package/dist/components/FormGenerator/FormGenerator.tokens.js +21 -0
- package/dist/components/Search/SearchSuggestions.js +1 -1
- package/dist/components/SkipToContent/SkipToContent.js +1 -1
- package/dist/components/Tooltip/Tooltip.styles.js +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/package.json +2 -2
|
@@ -19,7 +19,7 @@ var DList = styled.dl.withConfig({
|
|
|
19
19
|
componentId: "sc-1ob73hm-0"
|
|
20
20
|
})(["margin:0;*::selection{", "}", " & > dt:first-of-type{margin-top:", ";}& > dd:last-child{margin-bottom:", ";}dd + dt{margin-top:", ";}"], selection, function (_ref) {
|
|
21
21
|
var appearance = _ref.appearance;
|
|
22
|
-
return appearance && css(["dt{color:", ";", " ", ""], term.appearance[appearance].color, getFontStyling(termTypographyTypes[appearance]), appearance === 'bold' && css(["font-weight:600;"]));
|
|
22
|
+
return appearance && css(["dt{color:", ";", " ", "}"], term.appearance[appearance].color, getFontStyling(termTypographyTypes[appearance]), appearance === 'bold' && css(["font-weight:600;"]));
|
|
23
23
|
}, term.firstOfType.marginTop, desc.lastChild.marginBottom, list.beforeNextTerm.marginTop);
|
|
24
24
|
var DescriptionList = /*#__PURE__*/forwardRef(function (props, ref) {
|
|
25
25
|
var _props$appearance = props.appearance,
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { jsx } from 'react/jsx-runtime';
|
|
2
|
+
import styled from 'styled-components';
|
|
3
|
+
import { InputMessage } from '../InputMessage/InputMessage.js';
|
|
4
|
+
import { errorsTokens } from './FileUploader.tokens.js';
|
|
5
|
+
|
|
6
|
+
var MessageContainer = styled.div.withConfig({
|
|
7
|
+
displayName: "ErrorList__MessageContainer",
|
|
8
|
+
componentId: "sc-tndb6q-0"
|
|
9
|
+
})(["display:flex;flex-direction:column;gap:", ";"], errorsTokens.gap);
|
|
10
|
+
var ErrorList = function ErrorList(props) {
|
|
11
|
+
var errors = props.errors;
|
|
12
|
+
if (errors.length < 1) {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
return jsx(MessageContainer, {
|
|
16
|
+
children: errors.map(function (_ref) {
|
|
17
|
+
var id = _ref.id,
|
|
18
|
+
message = _ref.message;
|
|
19
|
+
return jsx(InputMessage, {
|
|
20
|
+
id: id,
|
|
21
|
+
message: message,
|
|
22
|
+
messageType: "error"
|
|
23
|
+
}, id);
|
|
24
|
+
})
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export { ErrorList, MessageContainer };
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { jsxs, jsx } from 'react/jsx-runtime';
|
|
2
|
+
import styled from 'styled-components';
|
|
3
|
+
import 'tslib';
|
|
4
|
+
import '../../icons/utils/StyledSvg.js';
|
|
5
|
+
import { CloseIcon } from '../../icons/tsx/close.js';
|
|
6
|
+
import { derivativeIdGenerator, spaceSeparatedIdListGenerator } from '../../utils/idGenerator.js';
|
|
7
|
+
import '../../utils/color.js';
|
|
8
|
+
import { Button } from '../Button/Button.js';
|
|
9
|
+
import { ErrorList } from './ErrorList.js';
|
|
10
|
+
import { fileTokens } from './FileUploader.tokens.js';
|
|
11
|
+
|
|
12
|
+
var FileWrapper = styled.li.withConfig({
|
|
13
|
+
displayName: "File__FileWrapper",
|
|
14
|
+
componentId: "sc-1mozy6v-0"
|
|
15
|
+
})([""]);
|
|
16
|
+
var FileNameWrapper = styled.span.withConfig({
|
|
17
|
+
displayName: "File__FileNameWrapper",
|
|
18
|
+
componentId: "sc-1mozy6v-1"
|
|
19
|
+
})(["word-break:break-all;"]);
|
|
20
|
+
var FileElement = styled.div.withConfig({
|
|
21
|
+
displayName: "File__FileElement",
|
|
22
|
+
componentId: "sc-1mozy6v-2"
|
|
23
|
+
})(["border-width:2px;border-style:solid;border-color:", ";margin-top:", ";padding:", " ", ";background-color:", ";display:flex;justify-content:space-between;align-items:center;gap:", ";"], function (_ref) {
|
|
24
|
+
var isValid = _ref.isValid;
|
|
25
|
+
return isValid ? fileTokens.backgroundColor : fileTokens.invalid.borderColor;
|
|
26
|
+
}, fileTokens.marginTop, fileTokens.paddingLeftRight, fileTokens.paddingTopBottom, fileTokens.backgroundColor, fileTokens.textToIconsGap);
|
|
27
|
+
var RemoveFileButton = styled(Button).withConfig({
|
|
28
|
+
displayName: "File__RemoveFileButton",
|
|
29
|
+
componentId: "sc-1mozy6v-3"
|
|
30
|
+
})(["padding:0;"]);
|
|
31
|
+
var File = function File(props) {
|
|
32
|
+
var parentId = props.parentId,
|
|
33
|
+
index = props.index,
|
|
34
|
+
stateFile = props.file,
|
|
35
|
+
removeFile = props.removeFile,
|
|
36
|
+
isValid = props.isValid;
|
|
37
|
+
var errorsList = stateFile.errors.map(function (e, errorIndex) {
|
|
38
|
+
return {
|
|
39
|
+
id: derivativeIdGenerator(parentId, "file-".concat(index, "-error-").concat(errorIndex)),
|
|
40
|
+
message: e
|
|
41
|
+
};
|
|
42
|
+
});
|
|
43
|
+
return jsxs(FileWrapper, {
|
|
44
|
+
children: [jsxs(FileElement, Object.assign({}, props, {
|
|
45
|
+
children: [jsx(FileNameWrapper, {
|
|
46
|
+
children: stateFile.file.name
|
|
47
|
+
}), jsx(RemoveFileButton, {
|
|
48
|
+
size: "small",
|
|
49
|
+
appearance: "borderless",
|
|
50
|
+
purpose: "secondary",
|
|
51
|
+
type: "button",
|
|
52
|
+
onClick: removeFile,
|
|
53
|
+
icon: CloseIcon,
|
|
54
|
+
htmlProps: {
|
|
55
|
+
'aria-label': "Fjern fil, ".concat(stateFile.file.name),
|
|
56
|
+
'aria-invalid': !isValid ? true : undefined,
|
|
57
|
+
'aria-errormessage': !isValid ? 'Ugyldig fil' : undefined,
|
|
58
|
+
'aria-describedby': spaceSeparatedIdListGenerator(errorsList.map(function (e) {
|
|
59
|
+
return e.id;
|
|
60
|
+
}))
|
|
61
|
+
}
|
|
62
|
+
})]
|
|
63
|
+
})), jsx(ErrorList, {
|
|
64
|
+
errors: errorsList
|
|
65
|
+
})]
|
|
66
|
+
}, stateFile.file.name);
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export { File };
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { toConsumableArray as _toConsumableArray } from '../../_virtual/_rollupPluginBabelHelpers.js';
|
|
2
|
+
import { jsx, jsxs } from 'react/jsx-runtime';
|
|
3
|
+
import { useId, useEffect } from 'react';
|
|
4
|
+
import styled from 'styled-components';
|
|
5
|
+
import 'tslib';
|
|
6
|
+
import '../../icons/utils/StyledSvg.js';
|
|
7
|
+
import { UploadIcon } from '../../icons/tsx/upload.js';
|
|
8
|
+
import { derivativeIdGenerator, spaceSeparatedIdListGenerator } from '../../utils/idGenerator.js';
|
|
9
|
+
import '../../utils/color.js';
|
|
10
|
+
import { Button } from '../Button/Button.js';
|
|
11
|
+
import { InputMessage } from '../InputMessage/InputMessage.js';
|
|
12
|
+
import '../Typography/Typography/Typography.js';
|
|
13
|
+
import { Label } from '../Typography/Label/Label.js';
|
|
14
|
+
import '../Typography/Link/Link.js';
|
|
15
|
+
import { VisuallyHidden } from '../VisuallyHidden/VisuallyHidden.js';
|
|
16
|
+
import { ErrorList } from './ErrorList.js';
|
|
17
|
+
import { File } from './File.js';
|
|
18
|
+
import { rootTokens } from './FileUploader.tokens.js';
|
|
19
|
+
import { useFileUploader } from './useFileUploader.js';
|
|
20
|
+
|
|
21
|
+
var defaultWidth = '320px';
|
|
22
|
+
var Wrapper = styled.div.withConfig({
|
|
23
|
+
displayName: "FileUploader__Wrapper",
|
|
24
|
+
componentId: "sc-pcnrgy-0"
|
|
25
|
+
})(["width:", ";"], function (_ref) {
|
|
26
|
+
var width = _ref.width;
|
|
27
|
+
return width ? width : defaultWidth;
|
|
28
|
+
});
|
|
29
|
+
var Root = styled.div.withConfig({
|
|
30
|
+
displayName: "FileUploader__Root",
|
|
31
|
+
componentId: "sc-pcnrgy-1"
|
|
32
|
+
})(["box-sizing:border-box;border-width:", ";border-style:dashed;border-color:", ";padding:", ";display:flex;flex-direction:column;gap:", ";background-color:", ";"], function (_ref2) {
|
|
33
|
+
var hasRootErrors = _ref2.hasRootErrors;
|
|
34
|
+
return hasRootErrors ? '2px' : '1px';
|
|
35
|
+
}, function (_ref3) {
|
|
36
|
+
var isDragActive = _ref3.isDragActive,
|
|
37
|
+
hasRootErrors = _ref3.hasRootErrors;
|
|
38
|
+
return isDragActive ? rootTokens.dragActive.borderColor : hasRootErrors ? rootTokens.borderColorError : rootTokens.borderColor;
|
|
39
|
+
}, function (_ref4) {
|
|
40
|
+
var hasRootErrors = _ref4.hasRootErrors;
|
|
41
|
+
return hasRootErrors ? "calc(".concat(rootTokens.paddingLeftRightTop, " - 1px) calc(").concat(rootTokens.paddingLeftRightTop, " - 1px) ").concat(rootTokens.paddingBottom) : "".concat(rootTokens.paddingLeftRightTop, " ").concat(rootTokens.paddingLeftRightTop, " ").concat(rootTokens.paddingBottom);
|
|
42
|
+
}, rootTokens.gap, function (_ref5) {
|
|
43
|
+
var isDragActive = _ref5.isDragActive;
|
|
44
|
+
return isDragActive ? rootTokens.dragActive.backgroundColor : rootTokens.backgroundColor;
|
|
45
|
+
});
|
|
46
|
+
var FileUploaderInput = styled.input.withConfig({
|
|
47
|
+
displayName: "FileUploader__FileUploaderInput",
|
|
48
|
+
componentId: "sc-pcnrgy-2"
|
|
49
|
+
})([""]);
|
|
50
|
+
var FileListElement = styled.ul.withConfig({
|
|
51
|
+
displayName: "FileUploader__FileListElement",
|
|
52
|
+
componentId: "sc-pcnrgy-3"
|
|
53
|
+
})(["margin:0;padding:0;list-style-type:none;"]);
|
|
54
|
+
var FileUploader = function FileUploader(props) {
|
|
55
|
+
var id = props.id,
|
|
56
|
+
label = props.label,
|
|
57
|
+
tip = props.tip,
|
|
58
|
+
_props$required = props.required,
|
|
59
|
+
required = _props$required === void 0 ? false : _props$required,
|
|
60
|
+
initialFiles = props.initialFiles,
|
|
61
|
+
accept = props.accept,
|
|
62
|
+
maxFiles = props.maxFiles,
|
|
63
|
+
disabled = props.disabled,
|
|
64
|
+
onChange = props.onChange,
|
|
65
|
+
width = props.width;
|
|
66
|
+
var generatedId = useId();
|
|
67
|
+
var uniqueId = id !== null && id !== void 0 ? id : "".concat(generatedId, "-fileUploader");
|
|
68
|
+
var _useFileUploader = useFileUploader({
|
|
69
|
+
id: id,
|
|
70
|
+
initialFiles: initialFiles,
|
|
71
|
+
accept: accept,
|
|
72
|
+
disabled: disabled,
|
|
73
|
+
maxFiles: maxFiles
|
|
74
|
+
}),
|
|
75
|
+
_useFileUploader$stat = _useFileUploader.state,
|
|
76
|
+
stateFiles = _useFileUploader$stat.files,
|
|
77
|
+
isDragActive = _useFileUploader$stat.isDragActive,
|
|
78
|
+
rootErrors = _useFileUploader$stat.rootErrors,
|
|
79
|
+
getRootProps = _useFileUploader.getRootProps,
|
|
80
|
+
getInputProps = _useFileUploader.getInputProps,
|
|
81
|
+
getButtonProps = _useFileUploader.getButtonProps,
|
|
82
|
+
_removeFile = _useFileUploader.removeFile;
|
|
83
|
+
useEffect(function () {
|
|
84
|
+
onChange(stateFiles.map(function (f) {
|
|
85
|
+
return f.file;
|
|
86
|
+
}));
|
|
87
|
+
}, [stateFiles]);
|
|
88
|
+
var hasLabel = label !== undefined;
|
|
89
|
+
var hasTip = tip !== undefined;
|
|
90
|
+
var hasRootErrors = rootErrors.length > 0;
|
|
91
|
+
var showRequiredMarker = required;
|
|
92
|
+
var tipId = derivativeIdGenerator(uniqueId, 'tip');
|
|
93
|
+
var fileListElements = stateFiles.map(function (stateFile, index) {
|
|
94
|
+
return jsx(File, {
|
|
95
|
+
parentId: uniqueId,
|
|
96
|
+
index: index,
|
|
97
|
+
file: stateFile,
|
|
98
|
+
isValid: stateFile.errors.length === 0,
|
|
99
|
+
removeFile: function removeFile() {
|
|
100
|
+
return _removeFile(stateFile);
|
|
101
|
+
}
|
|
102
|
+
}, stateFile.file.name);
|
|
103
|
+
});
|
|
104
|
+
var rootErrorsList = rootErrors.map(function (e, index) {
|
|
105
|
+
return {
|
|
106
|
+
id: derivativeIdGenerator(uniqueId, "error-".concat(index)),
|
|
107
|
+
message: e
|
|
108
|
+
};
|
|
109
|
+
});
|
|
110
|
+
return jsxs(Wrapper, Object.assign({
|
|
111
|
+
width: width
|
|
112
|
+
}, {
|
|
113
|
+
children: [hasLabel && jsx(Label, Object.assign({
|
|
114
|
+
showRequiredStyling: showRequiredMarker,
|
|
115
|
+
htmlFor: uniqueId
|
|
116
|
+
}, {
|
|
117
|
+
children: label
|
|
118
|
+
})), hasTip && jsx(InputMessage, {
|
|
119
|
+
id: tipId,
|
|
120
|
+
message: tip,
|
|
121
|
+
messageType: "tip"
|
|
122
|
+
}), jsxs(Root, Object.assign({}, getRootProps(), {
|
|
123
|
+
isDragActive: isDragActive,
|
|
124
|
+
hasRootErrors: rootErrors.length > 0
|
|
125
|
+
}, {
|
|
126
|
+
children: [jsx(FileUploaderInput, Object.assign({}, getInputProps())), "Dra og slipp filer her eller", ' ', jsx(VisuallyHidden, Object.assign({
|
|
127
|
+
as: "span"
|
|
128
|
+
}, {
|
|
129
|
+
children: "velg fil med p\xE5f\xF8lgende knapp"
|
|
130
|
+
})), jsx(Button, Object.assign({}, getButtonProps(), {
|
|
131
|
+
id: uniqueId,
|
|
132
|
+
size: "medium",
|
|
133
|
+
type: "button",
|
|
134
|
+
appearance: "filled",
|
|
135
|
+
purpose: "secondary",
|
|
136
|
+
label: "Velg fil",
|
|
137
|
+
icon: UploadIcon,
|
|
138
|
+
htmlProps: {
|
|
139
|
+
'aria-invalid': hasRootErrors ? true : undefined,
|
|
140
|
+
'aria-describedby': spaceSeparatedIdListGenerator([hasTip ? tipId : undefined].concat(_toConsumableArray(rootErrorsList.map(function (e) {
|
|
141
|
+
return e.id;
|
|
142
|
+
}))))
|
|
143
|
+
}
|
|
144
|
+
}))]
|
|
145
|
+
})), jsx(ErrorList, {
|
|
146
|
+
errors: rootErrorsList
|
|
147
|
+
}), jsx(FileListElement, {
|
|
148
|
+
children: fileListElements
|
|
149
|
+
})]
|
|
150
|
+
}));
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
export { FileUploader };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { ddsBaseTokens } from '@norges-domstoler/dds-design-tokens';
|
|
2
|
+
|
|
3
|
+
var colors = ddsBaseTokens.colors,
|
|
4
|
+
spacing = ddsBaseTokens.spacing;
|
|
5
|
+
var rootTokens = {
|
|
6
|
+
borderColor: colors.DdsColorNeutralsGray5,
|
|
7
|
+
borderColorError: colors.DdsColorDangerBase,
|
|
8
|
+
paddingLeftRightTop: spacing.SizesDdsSpacingLocalX15,
|
|
9
|
+
paddingBottom: spacing.SizesDdsSpacingLocalX2,
|
|
10
|
+
gap: spacing.SizesDdsSpacingLocalX1,
|
|
11
|
+
backgroundColor: colors.DdsColorNeutralsWhite,
|
|
12
|
+
dragActive: {
|
|
13
|
+
borderColor: colors.DdsColorInteractiveBase,
|
|
14
|
+
backgroundColor: colors.DdsColorInteractiveLightest
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var fileTokens = {
|
|
18
|
+
marginTop: spacing.SizesDdsSpacingLocalX05,
|
|
19
|
+
paddingLeftRight: spacing.SizesDdsSpacingLocalX05,
|
|
20
|
+
paddingTopBottom: spacing.SizesDdsSpacingLayoutX1,
|
|
21
|
+
backgroundColor: colors.DdsColorNeutralsGray1,
|
|
22
|
+
textToIconsGap: spacing.SizesDdsSpacingLocalX075,
|
|
23
|
+
invalid: {
|
|
24
|
+
borderColor: colors.DdsColorDangerBase
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
var errorsTokens = {
|
|
28
|
+
gap: spacing.SizesDdsSpacingLocalX025
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export { errorsTokens, fileTokens, rootTokens };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
var fileUploaderReducer = function fileUploaderReducer(state, action) {
|
|
2
|
+
switch (action.type) {
|
|
3
|
+
case 'focus':
|
|
4
|
+
return Object.assign(Object.assign({}, state), {
|
|
5
|
+
isFocused: true
|
|
6
|
+
});
|
|
7
|
+
case 'blur':
|
|
8
|
+
return Object.assign(Object.assign({}, state), {
|
|
9
|
+
isFocused: false
|
|
10
|
+
});
|
|
11
|
+
case 'dragEnter':
|
|
12
|
+
return Object.assign(Object.assign({}, state), {
|
|
13
|
+
isDragActive: true
|
|
14
|
+
});
|
|
15
|
+
case 'dragLeave':
|
|
16
|
+
return Object.assign(Object.assign({}, state), {
|
|
17
|
+
isDragActive: false
|
|
18
|
+
});
|
|
19
|
+
case 'onSetFiles':
|
|
20
|
+
return Object.assign(Object.assign({}, state), {
|
|
21
|
+
isDragActive: false,
|
|
22
|
+
rootErrors: action.payload.rootErrors,
|
|
23
|
+
files: action.payload.files
|
|
24
|
+
});
|
|
25
|
+
case 'onRemoveFile':
|
|
26
|
+
return Object.assign(Object.assign({}, state), {
|
|
27
|
+
rootErrors: action.payload.rootErrors,
|
|
28
|
+
files: action.payload.files
|
|
29
|
+
});
|
|
30
|
+
default:
|
|
31
|
+
return state;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export { fileUploaderReducer };
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { slicedToArray as _slicedToArray, regeneratorRuntime as _regeneratorRuntime, toConsumableArray as _toConsumableArray } from '../../_virtual/_rollupPluginBabelHelpers.js';
|
|
2
|
+
import { __awaiter } from 'tslib';
|
|
3
|
+
import { useRef, useReducer, useCallback } from 'react';
|
|
4
|
+
import { fileUploaderReducer } from './fileUploaderReducer.js';
|
|
5
|
+
import { fromEvent } from 'file-selector';
|
|
6
|
+
import { preventDefaults, isEventWithFiles, isFileAccepted, getInvalidFileTypeErrorMessage, getTooManyFilesErrorMessage } from './utils.js';
|
|
7
|
+
|
|
8
|
+
var calcRootErrors = function calcRootErrors(files, maxFiles) {
|
|
9
|
+
var errors = [];
|
|
10
|
+
if (maxFiles && maxFiles >= 1 && files.length > maxFiles) {
|
|
11
|
+
errors.push(getTooManyFilesErrorMessage(maxFiles));
|
|
12
|
+
}
|
|
13
|
+
return errors;
|
|
14
|
+
};
|
|
15
|
+
var useFileUploader = function useFileUploader(props) {
|
|
16
|
+
var _props$initialFiles = props.initialFiles,
|
|
17
|
+
initialFiles = _props$initialFiles === void 0 ? [] : _props$initialFiles,
|
|
18
|
+
accept = props.accept,
|
|
19
|
+
maxFiles = props.maxFiles,
|
|
20
|
+
disabled = props.disabled;
|
|
21
|
+
var rootRef = useRef(null);
|
|
22
|
+
var inputRef = useRef(null);
|
|
23
|
+
var buttonRef = useRef(null);
|
|
24
|
+
var _useReducer = useReducer(fileUploaderReducer, {
|
|
25
|
+
files: initialFiles.map(function (f) {
|
|
26
|
+
return {
|
|
27
|
+
file: f,
|
|
28
|
+
errors: []
|
|
29
|
+
};
|
|
30
|
+
}),
|
|
31
|
+
isFocused: false,
|
|
32
|
+
isFileDialogActive: false,
|
|
33
|
+
isDragActive: false,
|
|
34
|
+
rootErrors: []
|
|
35
|
+
}),
|
|
36
|
+
_useReducer2 = _slicedToArray(_useReducer, 2),
|
|
37
|
+
state = _useReducer2[0],
|
|
38
|
+
dispatch = _useReducer2[1];
|
|
39
|
+
var stateFiles = state.files;
|
|
40
|
+
var onRootFocus = useCallback(function () {
|
|
41
|
+
return dispatch({
|
|
42
|
+
type: 'focus'
|
|
43
|
+
});
|
|
44
|
+
}, [dispatch]);
|
|
45
|
+
var onRootBlur = useCallback(function () {
|
|
46
|
+
return dispatch({
|
|
47
|
+
type: 'blur'
|
|
48
|
+
});
|
|
49
|
+
}, [dispatch]);
|
|
50
|
+
var onRootDragEnter = useCallback(function (evt) {
|
|
51
|
+
return __awaiter(void 0, void 0, void 0, /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
52
|
+
var files, fileCount;
|
|
53
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
54
|
+
while (1) {
|
|
55
|
+
switch (_context.prev = _context.next) {
|
|
56
|
+
case 0:
|
|
57
|
+
preventDefaults(evt);
|
|
58
|
+
if (!isEventWithFiles(evt)) {
|
|
59
|
+
_context.next = 9;
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
_context.next = 4;
|
|
63
|
+
return fromEvent(evt);
|
|
64
|
+
case 4:
|
|
65
|
+
files = _context.sent;
|
|
66
|
+
fileCount = files.length;
|
|
67
|
+
if (!(fileCount === 0)) {
|
|
68
|
+
_context.next = 8;
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
return _context.abrupt("return");
|
|
72
|
+
case 8:
|
|
73
|
+
dispatch({
|
|
74
|
+
type: 'dragEnter'
|
|
75
|
+
});
|
|
76
|
+
case 9:
|
|
77
|
+
case "end":
|
|
78
|
+
return _context.stop();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}, _callee);
|
|
82
|
+
}));
|
|
83
|
+
}, [dispatch, accept, maxFiles]);
|
|
84
|
+
var onRootDragOver = useCallback(function (evt) {
|
|
85
|
+
preventDefaults(evt);
|
|
86
|
+
var hasFiles = isEventWithFiles(evt);
|
|
87
|
+
if (hasFiles && evt.dataTransfer) {
|
|
88
|
+
try {
|
|
89
|
+
evt.dataTransfer.dropEffect = 'copy';
|
|
90
|
+
} catch (_a) {} /* eslint-disable-line no-empty */
|
|
91
|
+
}
|
|
92
|
+
}, []);
|
|
93
|
+
var onRootDragLeave = useCallback(function (evt) {
|
|
94
|
+
preventDefaults(evt);
|
|
95
|
+
if (evt.currentTarget.contains(evt.relatedTarget)) return;
|
|
96
|
+
dispatch({
|
|
97
|
+
type: 'dragLeave'
|
|
98
|
+
});
|
|
99
|
+
}, [dispatch]);
|
|
100
|
+
var setFiles = useCallback(function (evt) {
|
|
101
|
+
return __awaiter(void 0, void 0, void 0, /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
|
|
102
|
+
var existingFileNames, filesFromEvent, newFiles, rootErrors;
|
|
103
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
104
|
+
while (1) {
|
|
105
|
+
switch (_context2.prev = _context2.next) {
|
|
106
|
+
case 0:
|
|
107
|
+
evt.preventDefault();
|
|
108
|
+
if (!isEventWithFiles(evt)) {
|
|
109
|
+
_context2.next = 9;
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
existingFileNames = stateFiles.map(function (f) {
|
|
113
|
+
return f.file.name;
|
|
114
|
+
}); // TODO: Fiks cast her.
|
|
115
|
+
_context2.next = 5;
|
|
116
|
+
return fromEvent(evt);
|
|
117
|
+
case 5:
|
|
118
|
+
filesFromEvent = _context2.sent;
|
|
119
|
+
newFiles = filesFromEvent.filter(function (f) {
|
|
120
|
+
return !existingFileNames.includes(f.name);
|
|
121
|
+
}).map(function (file) {
|
|
122
|
+
var accepted = isFileAccepted(file, accept);
|
|
123
|
+
return {
|
|
124
|
+
file: file,
|
|
125
|
+
errors: accepted ? [] : [getInvalidFileTypeErrorMessage()]
|
|
126
|
+
};
|
|
127
|
+
}).concat(stateFiles);
|
|
128
|
+
rootErrors = calcRootErrors(newFiles, maxFiles);
|
|
129
|
+
dispatch({
|
|
130
|
+
type: 'onSetFiles',
|
|
131
|
+
payload: {
|
|
132
|
+
rootErrors: rootErrors,
|
|
133
|
+
files: newFiles
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
case 9:
|
|
137
|
+
case "end":
|
|
138
|
+
return _context2.stop();
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}, _callee2);
|
|
142
|
+
}));
|
|
143
|
+
}, [stateFiles, maxFiles, accept, dispatch]);
|
|
144
|
+
var openFileDialog = useCallback(function () {
|
|
145
|
+
if (inputRef.current) {
|
|
146
|
+
inputRef.current.value = '';
|
|
147
|
+
inputRef.current.click();
|
|
148
|
+
}
|
|
149
|
+
}, [inputRef]);
|
|
150
|
+
var removeFile = useCallback(function (file) {
|
|
151
|
+
var newFiles = _toConsumableArray(stateFiles);
|
|
152
|
+
newFiles.splice(stateFiles.indexOf(file), 1);
|
|
153
|
+
var rootErrors = calcRootErrors(newFiles, maxFiles);
|
|
154
|
+
dispatch({
|
|
155
|
+
type: 'onRemoveFile',
|
|
156
|
+
payload: {
|
|
157
|
+
rootErrors: rootErrors,
|
|
158
|
+
files: newFiles
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
}, [stateFiles, maxFiles]);
|
|
162
|
+
var getRootProps = useCallback(function () {
|
|
163
|
+
return {
|
|
164
|
+
onBlur: onRootBlur,
|
|
165
|
+
onFocus: onRootFocus,
|
|
166
|
+
onDragEnter: onRootDragEnter,
|
|
167
|
+
onDragOver: onRootDragOver,
|
|
168
|
+
onDragLeave: onRootDragLeave,
|
|
169
|
+
onDrop: setFiles,
|
|
170
|
+
ref: rootRef
|
|
171
|
+
};
|
|
172
|
+
}, [onRootBlur, onRootFocus, onRootDragEnter, onRootDragOver, setFiles, rootRef, disabled]);
|
|
173
|
+
var getButtonProps = useCallback(function () {
|
|
174
|
+
return {
|
|
175
|
+
onClick: openFileDialog,
|
|
176
|
+
ref: buttonRef
|
|
177
|
+
};
|
|
178
|
+
}, [openFileDialog, buttonRef]);
|
|
179
|
+
var getInputProps = useCallback(function () {
|
|
180
|
+
return Object.assign({
|
|
181
|
+
type: 'file',
|
|
182
|
+
style: {
|
|
183
|
+
display: 'none'
|
|
184
|
+
},
|
|
185
|
+
tabIndex: -1,
|
|
186
|
+
ref: inputRef,
|
|
187
|
+
onChange: setFiles,
|
|
188
|
+
multiple: !maxFiles || maxFiles > 1
|
|
189
|
+
}, accept ? {
|
|
190
|
+
accept: accept.join(',')
|
|
191
|
+
} : {});
|
|
192
|
+
}, [inputRef, setFiles, maxFiles, accept]);
|
|
193
|
+
return {
|
|
194
|
+
state: state,
|
|
195
|
+
getRootProps: getRootProps,
|
|
196
|
+
getInputProps: getInputProps,
|
|
197
|
+
getButtonProps: getButtonProps,
|
|
198
|
+
removeFile: removeFile
|
|
199
|
+
};
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
export { useFileUploader };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import accepted from 'attr-accept';
|
|
2
|
+
|
|
3
|
+
var preventDefaults = function preventDefaults(event) {
|
|
4
|
+
event.preventDefault();
|
|
5
|
+
event.stopPropagation();
|
|
6
|
+
};
|
|
7
|
+
var isDragEvent = function isDragEvent(event) {
|
|
8
|
+
var asDragEvent = event;
|
|
9
|
+
return asDragEvent.dataTransfer !== undefined;
|
|
10
|
+
};
|
|
11
|
+
var isEventWithFiles = function isEventWithFiles(event) {
|
|
12
|
+
if (!isDragEvent(event)) {
|
|
13
|
+
return event.target.files !== null && event.target.files !== undefined;
|
|
14
|
+
}
|
|
15
|
+
return event.dataTransfer.types.includes('Files') || event.dataTransfer.types.includes('application/x-moz-file');
|
|
16
|
+
};
|
|
17
|
+
var isFileAccepted = function isFileAccepted(file, accept) {
|
|
18
|
+
return accept !== undefined ? accepted(file, accept) : true;
|
|
19
|
+
};
|
|
20
|
+
// export const isFileSizeAccepted = (
|
|
21
|
+
// file: File,
|
|
22
|
+
// minSize: number | undefined,
|
|
23
|
+
// maxSize: number | undefined
|
|
24
|
+
// ): boolean => {};
|
|
25
|
+
var getTooManyFilesErrorMessage = function getTooManyFilesErrorMessage(maxFiles) {
|
|
26
|
+
return "For mange filer, maks ".concat(maxFiles, "stk");
|
|
27
|
+
};
|
|
28
|
+
var getInvalidFileTypeErrorMessage = function getInvalidFileTypeErrorMessage() {
|
|
29
|
+
return 'Ugyldig filtype';
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export { getInvalidFileTypeErrorMessage, getTooManyFilesErrorMessage, isDragEvent, isEventWithFiles, isFileAccepted, preventDefaults };
|