@dvrd/dvr-controls 1.0.29 → 1.0.30
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/index.ts +4 -0
- package/package.json +1 -1
- package/src/js/checkbox/checkboxController.tsx +6 -0
- package/src/js/fileUpload/fileUpload.tsx +145 -0
- package/src/js/fileUpload/style/fileUpload.scss +64 -0
- package/src/js/header/v2/dvrdHeader.tsx +50 -0
- package/src/js/header/v2/dvrdHeaderController.tsx +38 -0
- package/src/js/util/interfaces.ts +8 -1
- package/src/js/util/jwtUtil.ts +1 -0
- package/src/js/util/requestUtil.ts +2 -0
package/index.ts
CHANGED
|
@@ -53,6 +53,8 @@ import Link from './src/js/link/link';
|
|
|
53
53
|
import DvrdOptionsList from './src/js/optionsList/dvrdOptionsList';
|
|
54
54
|
import DvrdSelectController from './src/js/select/dvrdSelectController';
|
|
55
55
|
import DvrdSwitch from './src/js/switch/dvrdSwitch';
|
|
56
|
+
import DvrdHeaderController from './src/js/header/v2/dvrdHeaderController';
|
|
57
|
+
import FileUpload from './src/js/fileUpload/fileUpload';
|
|
56
58
|
|
|
57
59
|
export {
|
|
58
60
|
// Components
|
|
@@ -98,6 +100,8 @@ export {
|
|
|
98
100
|
DvrdOptionsList,
|
|
99
101
|
DvrdSelectController as DvrdSelect,
|
|
100
102
|
DvrdSwitch,
|
|
103
|
+
DvrdHeaderController as DvrdHeader,
|
|
104
|
+
FileUpload,
|
|
101
105
|
|
|
102
106
|
// Interfaces / Enums
|
|
103
107
|
DialogActionShape,
|
package/package.json
CHANGED
|
@@ -28,6 +28,7 @@ interface Props {
|
|
|
28
28
|
unControlled: boolean;
|
|
29
29
|
title?: string;
|
|
30
30
|
useDarkestColor: boolean;
|
|
31
|
+
ref?: any;
|
|
31
32
|
}
|
|
32
33
|
|
|
33
34
|
interface CheckboxState {
|
|
@@ -112,6 +113,11 @@ export default class CheckboxController extends React.Component<Props, CheckboxS
|
|
|
112
113
|
}
|
|
113
114
|
};
|
|
114
115
|
|
|
116
|
+
componentDidUpdate = (prevProps: Props, prevState: CheckboxState) => {
|
|
117
|
+
if (prevState.internalValue !== this.state.internalValue && this.state.internalValue) this.addRipple();
|
|
118
|
+
else if (prevProps.checked !== this.props.checked && this.props.checked) this.addRipple();
|
|
119
|
+
};
|
|
120
|
+
|
|
115
121
|
componentWillUnmount = () => {
|
|
116
122
|
this.removeRipple();
|
|
117
123
|
};
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Dave van Rijn Development 2023.
|
|
3
|
+
*/
|
|
4
|
+
import './style/fileUpload.scss';
|
|
5
|
+
|
|
6
|
+
import classNames from 'classnames';
|
|
7
|
+
import React, {useMemo, useRef, useState} from 'react';
|
|
8
|
+
import {ChangeFunction} from "../util/interfaces";
|
|
9
|
+
import {preventDefault} from "../util/miscUtil";
|
|
10
|
+
import {stopPropagation, voidFunction} from "../util/controlUtil";
|
|
11
|
+
import DvrdButton from "../button/dvrdButton";
|
|
12
|
+
import AwesomeIcon from "../icon/awesomeIcon";
|
|
13
|
+
import {IconName} from '@fortawesome/fontawesome-svg-core';
|
|
14
|
+
|
|
15
|
+
interface Props {
|
|
16
|
+
onChange: ChangeFunction<FileList>;
|
|
17
|
+
file: File | FileList | null;
|
|
18
|
+
disabled?: boolean;
|
|
19
|
+
className?: string;
|
|
20
|
+
multiple?: boolean;
|
|
21
|
+
accept?: string | Array<string>;
|
|
22
|
+
fileIcon?: IconName;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export default function FileUpload(props: Props) {
|
|
26
|
+
const {file, className, disabled, onChange, multiple, fileIcon} = props;
|
|
27
|
+
const [draggingFile, setDraggingFile] = useState(false);
|
|
28
|
+
const dragCounter = useRef(0);
|
|
29
|
+
const inputRef = useRef<HTMLInputElement>(null);
|
|
30
|
+
|
|
31
|
+
const accept: string | undefined = useMemo(() => {
|
|
32
|
+
if (!props.accept) return undefined;
|
|
33
|
+
if (Array.isArray(props.accept)) return props.accept.join(',');
|
|
34
|
+
return props.accept;
|
|
35
|
+
}, [props.accept]);
|
|
36
|
+
const buttonLabel = useMemo(() => {
|
|
37
|
+
if (!file) {
|
|
38
|
+
if (multiple) return 'Upload bestanden';
|
|
39
|
+
return 'Upload bestand';
|
|
40
|
+
} else {
|
|
41
|
+
if (multiple) return 'Kies andere bestanden';
|
|
42
|
+
return 'Kies ander bestand';
|
|
43
|
+
}
|
|
44
|
+
}, [file]);
|
|
45
|
+
|
|
46
|
+
function _onChange(evt: React.ChangeEvent<HTMLInputElement>) {
|
|
47
|
+
const {files} = evt.target;
|
|
48
|
+
if (!files) return;
|
|
49
|
+
onChange(files, evt);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function onClickUpload() {
|
|
53
|
+
inputRef.current?.click();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function onDrop(evt: React.DragEvent) {
|
|
57
|
+
preventEvent(evt);
|
|
58
|
+
const {files} = evt.dataTransfer;
|
|
59
|
+
onChange(files);
|
|
60
|
+
evt.dataTransfer.clearData();
|
|
61
|
+
setDraggingFile(false);
|
|
62
|
+
dragCounter.current = 0;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function onDragEnter(evt: React.DragEvent) {
|
|
66
|
+
preventEvent(evt);
|
|
67
|
+
dragCounter.current += 1;
|
|
68
|
+
setDraggingFile(true);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function onDragLeave(evt: React.DragEvent) {
|
|
72
|
+
preventEvent(evt);
|
|
73
|
+
if (!draggingFile) return;
|
|
74
|
+
dragCounter.current -= 1;
|
|
75
|
+
if (dragCounter.current <= 0) setDraggingFile(false);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function preventEvent(evt: React.DragEvent) {
|
|
79
|
+
preventDefault(evt);
|
|
80
|
+
stopPropagation(evt);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function renderFile() {
|
|
84
|
+
if (file) return renderSelectedFile();
|
|
85
|
+
return renderSelectFile();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function renderSelectFile() {
|
|
89
|
+
const label = multiple ? 'bestanden' : 'een bestand';
|
|
90
|
+
const icon = fileIcon ?? 'file';
|
|
91
|
+
return (
|
|
92
|
+
<div className='select-file-container'>
|
|
93
|
+
<div className='select-file-label-container'>
|
|
94
|
+
<AwesomeIcon name={icon} className='file-icon'/>
|
|
95
|
+
<label className='selected-file-label'>Klik hier om {label} te selecteren, of
|
|
96
|
+
sleep {label} hierheen.</label>
|
|
97
|
+
</div>
|
|
98
|
+
</div>
|
|
99
|
+
)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function renderSelectedFile() {
|
|
103
|
+
const text = multiple ? 'Geselecteerde bestanden:' : 'Geselecteerd bestand:';
|
|
104
|
+
const icon = fileIcon ?? 'file';
|
|
105
|
+
return (
|
|
106
|
+
<div className='selected-file-container'>
|
|
107
|
+
<AwesomeIcon name={icon} className='file-icon'/>
|
|
108
|
+
<div className='file-labels-container'>
|
|
109
|
+
<label className='selected-file-title'>{text}</label>
|
|
110
|
+
<div className='file-label-container'>
|
|
111
|
+
{renderFileLabels()}
|
|
112
|
+
</div>
|
|
113
|
+
</div>
|
|
114
|
+
</div>
|
|
115
|
+
)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function renderFileLabels() {
|
|
119
|
+
if (!file) return null;
|
|
120
|
+
if (file instanceof FileList) {
|
|
121
|
+
const files = Array.from(file);
|
|
122
|
+
return (
|
|
123
|
+
<>
|
|
124
|
+
{files.map((file: File, index: number) => (
|
|
125
|
+
<label key={index} className='file-label'>{file.name}</label>
|
|
126
|
+
))}
|
|
127
|
+
</>
|
|
128
|
+
)
|
|
129
|
+
}
|
|
130
|
+
return <label className='file-label'>{file.name}</label>;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return (
|
|
134
|
+
<div className={classNames('dvrd-file-upload', className, disabled && 'disabled')} onDrag={preventEvent}
|
|
135
|
+
onDragOver={preventEvent} onDrop={onDrop} onDragEnter={onDragEnter} onDragLeave={onDragLeave}
|
|
136
|
+
onClick={onClickUpload}>
|
|
137
|
+
<input type='file' multiple={multiple} accept={accept} ref={inputRef} style={{display: 'none'}}
|
|
138
|
+
onChange={_onChange}/>
|
|
139
|
+
<div className='file-container'>
|
|
140
|
+
{renderFile()}
|
|
141
|
+
</div>
|
|
142
|
+
<DvrdButton onClick={voidFunction} label={buttonLabel} secondary={!!file}/>
|
|
143
|
+
</div>
|
|
144
|
+
)
|
|
145
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Dave van Rijn Development 2023.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
@import '../../../style/variables';
|
|
6
|
+
|
|
7
|
+
.dvrd-file-upload {
|
|
8
|
+
border-radius: 1rem;
|
|
9
|
+
border: 1px solid $color-gray-4;
|
|
10
|
+
padding: 1rem;
|
|
11
|
+
width: 50rem;
|
|
12
|
+
max-width: 100dvw;
|
|
13
|
+
min-height: min(100dvh, 20rem);
|
|
14
|
+
display: grid;
|
|
15
|
+
grid-template-columns: auto;
|
|
16
|
+
grid-row-gap: 1rem;
|
|
17
|
+
justify-items: center;
|
|
18
|
+
justify-content: center;
|
|
19
|
+
align-content: center;
|
|
20
|
+
cursor: pointer;
|
|
21
|
+
|
|
22
|
+
.file-container {
|
|
23
|
+
|
|
24
|
+
.file-icon {
|
|
25
|
+
font-size: 3rem;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
.select-file-container {
|
|
29
|
+
.select-file-label-container {
|
|
30
|
+
display: grid;
|
|
31
|
+
grid-template-columns: auto;
|
|
32
|
+
justify-items: center;
|
|
33
|
+
grid-row-gap: 1rem;
|
|
34
|
+
|
|
35
|
+
.selected-file-label {
|
|
36
|
+
font-weight: 500;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
.selected-file-container {
|
|
42
|
+
display: grid;
|
|
43
|
+
grid-template-columns: auto;
|
|
44
|
+
grid-row-gap: 1rem;
|
|
45
|
+
justify-items: center;
|
|
46
|
+
|
|
47
|
+
.file-labels-container {
|
|
48
|
+
display: grid;
|
|
49
|
+
grid-template-columns: auto;
|
|
50
|
+
grid-row-gap: 1rem;
|
|
51
|
+
|
|
52
|
+
.file-label-container {
|
|
53
|
+
display: grid;
|
|
54
|
+
grid-template-columns: auto;
|
|
55
|
+
grid-row-gap: .5rem;
|
|
56
|
+
|
|
57
|
+
.file-label {
|
|
58
|
+
font-weight: 500;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import React, {CSSProperties, MouseEventHandler, useContext, useMemo} from 'react';
|
|
2
|
+
import {HeaderItem} from "../../util/interfaces";
|
|
3
|
+
import {ControlContext} from "../../util/controlContext";
|
|
4
|
+
import classNames from 'classnames';
|
|
5
|
+
|
|
6
|
+
interface Props {
|
|
7
|
+
onClickItem: (item: HeaderItem) => MouseEventHandler;
|
|
8
|
+
leftItems?: Array<HeaderItem>;
|
|
9
|
+
rightItems?: Array<HeaderItem>;
|
|
10
|
+
backgroundColor?: string;
|
|
11
|
+
color?: string;
|
|
12
|
+
className?: string;
|
|
13
|
+
itemClassName?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export default function DvrdHeader(props: Props) {
|
|
17
|
+
const context = useContext(ControlContext);
|
|
18
|
+
const {backgroundColor, color, className, itemClassName, rightItems, onClickItem, leftItems} = props;
|
|
19
|
+
const containerStyle: CSSProperties = useMemo(() => {
|
|
20
|
+
const background = backgroundColor ?? context.baseColor;
|
|
21
|
+
return {backgroundColor: background};
|
|
22
|
+
}, [context.baseColor, backgroundColor]);
|
|
23
|
+
const itemStyle: CSSProperties = useMemo(() => {
|
|
24
|
+
const _color = color ?? context.contrastColor;
|
|
25
|
+
return {color: _color};
|
|
26
|
+
}, [context.contrastColor, color]);
|
|
27
|
+
|
|
28
|
+
function renderItem(item: HeaderItem, index: number) {
|
|
29
|
+
const {label} = item;
|
|
30
|
+
if (React.isValidElement(label))
|
|
31
|
+
return React.cloneElement(label, Object.assign(label.props, {onClick: onClickItem(item), key: index}));
|
|
32
|
+
return (
|
|
33
|
+
<div key={index} className={classNames('dvrd-header-item', itemClassName)} onClick={onClickItem(item)}
|
|
34
|
+
style={itemStyle}>
|
|
35
|
+
<label className='dvrd-header-label'>{label}</label>
|
|
36
|
+
</div>
|
|
37
|
+
)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return (
|
|
41
|
+
<div className={classNames('dvrd-header', className)} style={containerStyle}>
|
|
42
|
+
<div className='dvrd-header-items-container'>
|
|
43
|
+
{leftItems?.map(renderItem)}
|
|
44
|
+
</div>
|
|
45
|
+
<div className='dvrd-header-items-container'>
|
|
46
|
+
{rightItems?.map(renderItem)}
|
|
47
|
+
</div>
|
|
48
|
+
</div>
|
|
49
|
+
)
|
|
50
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import React, {MouseEventHandler} from 'react';
|
|
2
|
+
import {useNavigate} from 'react-router-dom';
|
|
3
|
+
import {HeaderItem} from "../../util/interfaces";
|
|
4
|
+
import DvrdHeader from "./dvrdHeader";
|
|
5
|
+
|
|
6
|
+
interface Props {
|
|
7
|
+
onClickItem?: (item: HeaderItem) => MouseEventHandler;
|
|
8
|
+
leftItems?: Array<HeaderItem>;
|
|
9
|
+
rightItems?: Array<HeaderItem>;
|
|
10
|
+
backgroundColor?: string;
|
|
11
|
+
color?: string;
|
|
12
|
+
className?: string;
|
|
13
|
+
itemClassName?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export default function DvrdHeaderController(props: Props) {
|
|
17
|
+
const {leftItems, onClickItem, rightItems, itemClassName, className, backgroundColor, color} = props;
|
|
18
|
+
const navigate = useNavigate();
|
|
19
|
+
|
|
20
|
+
function _onClickItem(item: HeaderItem) {
|
|
21
|
+
return function (evt: React.MouseEvent) {
|
|
22
|
+
if (item.onClick) item.onClick(item)(evt);
|
|
23
|
+
else if (onClickItem) onClickItem(item)(evt);
|
|
24
|
+
else if (item.route) _navigate(item.route, item.target);
|
|
25
|
+
else console.warn('Item clicked without click handler.', {item});
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function _navigate(route: string, target?: '_blank') {
|
|
30
|
+
if (route.startsWith('http')) window.open(route, target);
|
|
31
|
+
else navigate(route);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return (
|
|
35
|
+
<DvrdHeader onClickItem={_onClickItem} backgroundColor={backgroundColor} color={color} className={className}
|
|
36
|
+
itemClassName={itemClassName} leftItems={leftItems} rightItems={rightItems}/>
|
|
37
|
+
);
|
|
38
|
+
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Copyright (c) 2021. Dave van Rijn Development
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import React, {CSSProperties, ReactElement} from 'react';
|
|
5
|
+
import React, {CSSProperties, MouseEventHandler, ReactElement} from 'react';
|
|
6
6
|
|
|
7
7
|
// =========== INTERFACES
|
|
8
8
|
|
|
@@ -67,6 +67,13 @@ export interface PDFOptions {
|
|
|
67
67
|
enableTableHeaderColor?: boolean;
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
+
export interface HeaderItem {
|
|
71
|
+
onClick?: (item: HeaderItem) => MouseEventHandler;
|
|
72
|
+
route?: string;
|
|
73
|
+
label: React.ReactNode;
|
|
74
|
+
target?: '_blank';
|
|
75
|
+
}
|
|
76
|
+
|
|
70
77
|
// =========== ENUMS
|
|
71
78
|
|
|
72
79
|
export enum ModeEnum {DETAIL = 'detail', EDIT = 'edit', NEW = 'new'}
|
package/src/js/util/jwtUtil.ts
CHANGED
|
@@ -20,6 +20,7 @@ export const getJwt = (): string | null => {
|
|
|
20
20
|
};
|
|
21
21
|
|
|
22
22
|
export const setJwt = (token: string | null, preserveOnNull: boolean = false, includeStorage: boolean = true) => {
|
|
23
|
+
console.debug('Settings JWT token', {token, preserveOnNull, includeStorage, allowCookies: window.allowCookies});
|
|
23
24
|
jwtToken = token;
|
|
24
25
|
if (window.allowCookies || !token) {
|
|
25
26
|
if (token) {
|
|
@@ -82,6 +82,8 @@ export function sendFetch(config: FetchOptions, legacySupport: boolean = false):
|
|
|
82
82
|
if (reason.code === DOMException.ABORT_ERR) return; // Request aborted
|
|
83
83
|
else if (config.errorCallback)
|
|
84
84
|
config.errorCallback(reason);
|
|
85
|
+
else if (defaultErrorHandler)
|
|
86
|
+
defaultErrorHandler(reason, config);
|
|
85
87
|
});
|
|
86
88
|
return abortController;
|
|
87
89
|
}
|