@dvrd/dvr-controls 1.0.14 → 1.0.16
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 +8 -1
- package/package.json +29 -23
- package/src/js/button/button.tsx +102 -0
- package/src/js/button/buttonController.tsx +179 -0
- package/src/js/button/closeButton.tsx +29 -0
- package/src/js/button/dvrdButton.tsx +128 -0
- package/src/js/button/outlinedButton.tsx +105 -0
- package/src/js/button/simpleButton.tsx +163 -0
- package/src/js/button/style/button.scss +95 -0
- package/src/js/button/style/closeButton.scss +15 -0
- package/src/js/button/style/dvrdButton.scss +30 -0
- package/src/js/button/style/outlinedButton.scss +84 -0
- package/src/js/button/style/simpleButton.scss +80 -0
- package/src/js/carousel/DVRCarousel.tsx +163 -0
- package/src/js/carousel/DVRCarouselController.tsx +95 -0
- package/src/js/carousel/style/DVRCarousel.scss +38 -0
- package/src/js/checkbox/checkbox.tsx +148 -0
- package/src/js/checkbox/checkboxController.tsx +131 -0
- package/src/js/checkbox/style/checkbox.scss +109 -0
- package/src/js/colorPicker/colorPicker.tsx +118 -0
- package/src/js/colorPicker/style/colorPicker.scss +20 -0
- package/src/js/date/dvrdDatePicker.tsx +357 -0
- package/src/js/date/style/dvrdDatePicker.scss +307 -0
- package/src/js/dialog/dialog.tsx +207 -0
- package/src/js/dialog/dialogController.tsx +70 -0
- package/src/js/dialog/inlineDialog.tsx +127 -0
- package/src/js/dialog/style/dialog.scss +61 -0
- package/src/js/events/withEvents.tsx +40 -0
- package/src/js/head/DVRHead.tsx +49 -0
- package/src/js/header/DVRHeader.tsx +417 -0
- package/src/js/header/style/header.scss +206 -0
- package/src/js/icon/awesomeIcon.tsx +20 -0
- package/src/js/image/imageUpload.tsx +69 -0
- package/src/js/image/style/imageUpload.scss +11 -0
- package/src/js/info/info.tsx +136 -0
- package/src/js/info/style/info.scss +39 -0
- package/src/js/input/animated/animatedTextField.tsx +159 -0
- package/src/js/input/date/dateField.tsx +360 -0
- package/src/js/input/date/dateFieldController.tsx +245 -0
- package/src/js/input/date/datePicker/datePicker.tsx +186 -0
- package/src/js/input/date/datePicker/style/datePicker.scss +102 -0
- package/src/js/input/date/input/dateInput.tsx +214 -0
- package/src/js/input/date/style/dateField.scss +40 -0
- package/src/js/input/date/timePicker/style/timePicker.scss +95 -0
- package/src/js/input/date/timePicker/timePicker.tsx +143 -0
- package/src/js/input/editor/DVREditor.tsx +21 -0
- package/src/js/input/number/numberInput.tsx +157 -0
- package/src/js/input/password/passwordInput.tsx +140 -0
- package/src/js/input/password/passwordRules.tsx +48 -0
- package/src/js/input/password/style/passwordInput.scss +39 -0
- package/src/js/input/password/style/passwordRules.scss +41 -0
- package/src/js/input/simple/style/simpleInput.scss +98 -0
- package/src/js/input/simple/v2/simpleInputV2.tsx +178 -0
- package/src/js/input/style/input.scss +138 -0
- package/src/js/input/v2/inputController_v2.tsx +250 -0
- package/src/js/input/v2/input_v2.tsx +7 -0
- package/src/js/label/label.tsx +196 -0
- package/src/js/label/style/label.scss +4 -0
- package/src/js/link/link.tsx +38 -0
- package/src/js/link/style/link.scss +30 -0
- package/src/js/loader/loader.tsx +79 -0
- package/src/js/loader/loaderController.tsx +61 -0
- package/src/js/loader/style/loader.scss +53 -0
- package/src/js/media/media.tsx +72 -0
- package/src/js/navigator/navigator.tsx +51 -0
- package/src/js/optionsList/dvrdOptionsList.tsx +112 -0
- package/src/js/optionsList/style/dvrdOptionsList.scss +84 -0
- package/src/js/optionsMenu/optionsMenu.tsx +187 -0
- package/src/js/optionsMenu/style/optionsMenu.scss +70 -0
- package/src/js/pdf/element/pdfElement.tsx +315 -0
- package/src/js/pdf/element/style/pdfElement.scss +111 -0
- package/src/js/pdf/history/pdfHistory.ts +57 -0
- package/src/js/pdf/image/pdfImage.tsx +175 -0
- package/src/js/pdf/image/style/pdfImage.scss +34 -0
- package/src/js/pdf/invoiceTable/pdfInvoiceTable.tsx +176 -0
- package/src/js/pdf/invoiceTable/style/pdfInvoiceTable.scss +32 -0
- package/src/js/pdf/pdfTemplateCreator.tsx +279 -0
- package/src/js/pdf/settings/buttons/iconButton.tsx +49 -0
- package/src/js/pdf/settings/buttons/style/iconButton.scss +50 -0
- package/src/js/pdf/settings/image/pdfImageSettings.tsx +82 -0
- package/src/js/pdf/settings/image/style/pdfImageSettings.scss +9 -0
- package/src/js/pdf/settings/invoiceTable/pdfInvoiceTableSettings.tsx +141 -0
- package/src/js/pdf/settings/invoiceTable/style/pdfInvoiceTableSettings.scss +38 -0
- package/src/js/pdf/settings/pdfElementSettings.tsx +86 -0
- package/src/js/pdf/settings/style/pdfElementSettings.scss +56 -0
- package/src/js/pdf/settings/text/pdfTextSettings.tsx +202 -0
- package/src/js/pdf/settings/text/style/pdfTextSettings.scss +94 -0
- package/src/js/pdf/style/pdfTemplateCreator.scss +118 -0
- package/src/js/pdf/text/pdfText.tsx +267 -0
- package/src/js/pdf/text/style/pdfText.scss +22 -0
- package/src/js/pdf/v2/pdfElement/pdfDraggableElement.tsx +193 -0
- package/src/js/pdf/v2/types/pdfTemplateTypes.ts +27 -0
- package/src/js/popup/style/withBackground.scss +29 -0
- package/src/js/popup/withBackground.tsx +92 -0
- package/src/js/select/async/asyncSelect.tsx +46 -0
- package/src/js/select/async/style/asyncSelect.scss +23 -0
- package/src/js/select/dvrdSelect.tsx +214 -0
- package/src/js/select/dvrdSelectController.tsx +81 -0
- package/src/js/select/select.tsx +310 -0
- package/src/js/select/selectController.tsx +341 -0
- package/src/js/select/style/dvrdSelect.scss +140 -0
- package/src/js/select/style/select.scss +199 -0
- package/src/js/sidebarMenu/sidebarMenu.tsx +167 -0
- package/src/js/sidebarMenu/style/sidebarMenu.scss +167 -0
- package/src/js/slider/DVRSlider.tsx +107 -0
- package/src/js/slider/style/DVRSlider.scss +88 -0
- package/src/js/snackbar/snackbar.tsx +72 -0
- package/src/js/snackbar/snackbarController.tsx +104 -0
- package/src/js/snackbar/style/snackbar.scss +46 -0
- package/src/js/switch/dvrdSwitch.tsx +53 -0
- package/src/js/switch/style/dvrdSwitch.scss +47 -0
- package/src/js/switch/style/switch.scss +84 -0
- package/src/js/switch/switch.tsx +115 -0
- package/src/js/switch/switchController.tsx +97 -0
- package/src/js/textField/dvrdInput.tsx +219 -0
- package/src/js/textField/dvrdInputController.tsx +97 -0
- package/src/js/textField/dvrdNumberInput.tsx +141 -0
- package/src/js/textField/dvrdPasswordInput.tsx +40 -0
- package/src/js/textField/style/dvrdInput.scss +114 -0
- package/src/js/textField/style/dvrdPassword.scss +15 -0
- package/src/js/topButton/style/topButton.scss +54 -0
- package/src/js/topButton/topButton.tsx +136 -0
- package/src/js/util/analyticsUtil.ts +41 -0
- package/src/js/util/colorUtil.ts +230 -0
- package/src/js/util/componentUtil.tsx +59 -0
- package/src/js/util/constants.ts +12 -0
- package/src/js/util/controlContext.tsx +46 -0
- package/src/js/util/controlUtil.ts +107 -0
- package/src/js/util/cookieUtil.ts +17 -0
- package/src/js/util/eventUtil.ts +65 -0
- package/src/js/util/googleUtil.ts +88 -0
- package/src/js/util/interfaces.ts +180 -0
- package/src/js/util/jwtUtil.ts +72 -0
- package/src/js/util/miscUtil.ts +170 -0
- package/src/js/util/momentUtil.ts +45 -0
- package/src/js/util/pdfUtil.ts +124 -0
- package/src/js/util/requestUtil.ts +145 -0
- package/src/js/util/responsiveUtil.ts +37 -0
- package/src/js/util/validationUtil.ts +13 -0
- package/src/res/img/lock-handle.png +0 -0
- package/src/res/img/lock-handle.webp +0 -0
- package/src/res/img/lock.png +0 -0
- package/src/res/img/lock.webp +0 -0
- package/src/style/common-icons-variables.scss +140 -0
- package/src/style/common-icons.scss +714 -0
- package/src/style/common-variables.scss +243 -0
- package/src/style/display-breakpoints.scss +141 -0
- package/src/style/fonts/common-icons.eot +0 -0
- package/src/style/fonts/common-icons.svg +150 -0
- package/src/style/fonts/common-icons.ttf +0 -0
- package/src/style/fonts/common-icons.woff +0 -0
- package/src/style/fonts/common-icons.woff2 +0 -0
- package/src/style/fonts/fontAwesome/css/all.css +7003 -0
- package/src/style/fonts/fontAwesome/css/all.min.css +6 -0
- package/src/style/fonts/fontAwesome/css/brands.css +1423 -0
- package/src/style/fonts/fontAwesome/css/brands.min.css +6 -0
- package/src/style/fonts/fontAwesome/css/fontawesome.css +5519 -0
- package/src/style/fonts/fontAwesome/css/fontawesome.min.css +6 -0
- package/src/style/fonts/fontAwesome/css/regular.css +19 -0
- package/src/style/fonts/fontAwesome/css/regular.min.css +6 -0
- package/src/style/fonts/fontAwesome/css/solid.css +19 -0
- package/src/style/fonts/fontAwesome/css/solid.min.css +6 -0
- package/src/style/fonts/fontAwesome/css/svg-with-js.css +634 -0
- package/src/style/fonts/fontAwesome/css/svg-with-js.min.css +6 -0
- package/src/style/fonts/fontAwesome/css/v4-font-face.css +26 -0
- package/src/style/fonts/fontAwesome/css/v4-font-face.min.css +6 -0
- package/src/style/fonts/fontAwesome/css/v4-shims.css +2146 -0
- package/src/style/fonts/fontAwesome/css/v4-shims.min.css +6 -0
- package/src/style/fonts/fontAwesome/css/v5-font-face.css +22 -0
- package/src/style/fonts/fontAwesome/css/v5-font-face.min.css +6 -0
- package/src/style/fonts/fontAwesome/webfonts/fa-brands-400.ttf +0 -0
- package/src/style/fonts/fontAwesome/webfonts/fa-brands-400.woff2 +0 -0
- package/src/style/fonts/fontAwesome/webfonts/fa-regular-400.ttf +0 -0
- package/src/style/fonts/fontAwesome/webfonts/fa-regular-400.woff2 +0 -0
- package/src/style/fonts/fontAwesome/webfonts/fa-solid-900.ttf +0 -0
- package/src/style/fonts/fontAwesome/webfonts/fa-solid-900.woff2 +0 -0
- package/src/style/fonts/fontAwesome/webfonts/fa-v4compatibility.ttf +0 -0
- package/src/style/fonts/fontAwesome/webfonts/fa-v4compatibility.woff2 +0 -0
- package/src/style/variables.scss +11 -0
- package/.gitignore +0 -31
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2021. Dave van Rijn Development
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import './style/switch.scss';
|
|
6
|
+
|
|
7
|
+
import React from 'react';
|
|
8
|
+
import classNames from 'classnames';
|
|
9
|
+
import {isNotNull} from "../util/controlUtil";
|
|
10
|
+
import {convertColor, editColor} from "../util/colorUtil";
|
|
11
|
+
|
|
12
|
+
interface SwitchProps {
|
|
13
|
+
onChange: any,
|
|
14
|
+
onMouseEnter: any,
|
|
15
|
+
onMouseLeave: any,
|
|
16
|
+
value: boolean,
|
|
17
|
+
labels: string[],
|
|
18
|
+
baseColor: string,
|
|
19
|
+
trackColor: string,
|
|
20
|
+
offColor: string,
|
|
21
|
+
id: string,
|
|
22
|
+
containerClass: string,
|
|
23
|
+
labelClass: string,
|
|
24
|
+
switchClass: string,
|
|
25
|
+
trackClass: string,
|
|
26
|
+
mainLabelClass: string;
|
|
27
|
+
padding: 'small' | 'standard' | 'large',
|
|
28
|
+
disabled: boolean,
|
|
29
|
+
isHovered: boolean,
|
|
30
|
+
label: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export class Switch extends React.Component<SwitchProps> {
|
|
34
|
+
getContainerClass = (): string => {
|
|
35
|
+
const {containerClass, disabled, padding} = this.props, classes = ['switchContainer'];
|
|
36
|
+
if (disabled) classes.push('disabled');
|
|
37
|
+
|
|
38
|
+
if (padding === 'small') classes.push('sp');
|
|
39
|
+
else if (padding === 'large') classes.push('lp');
|
|
40
|
+
|
|
41
|
+
classes.push(containerClass);
|
|
42
|
+
return classNames(classes);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
getLabelClass = (position: 'left' | 'right'): string => {
|
|
46
|
+
const {labelClass} = this.props;
|
|
47
|
+
return classNames('label', position, labelClass);
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
getSwitchClass = (): string => {
|
|
51
|
+
const {switchClass, value} = this.props;
|
|
52
|
+
return classNames('switch', value && 'on', switchClass);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
getTrackClass = (): string => {
|
|
56
|
+
const {trackClass} = this.props;
|
|
57
|
+
return classNames('track', trackClass);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
getSwitchStyle = (): object => {
|
|
61
|
+
const {baseColor, isHovered, value, offColor} = this.props;
|
|
62
|
+
let color;
|
|
63
|
+
if (!value && isNotNull(offColor)) color = convertColor(offColor);
|
|
64
|
+
else {
|
|
65
|
+
color = convertColor(baseColor);
|
|
66
|
+
if (isHovered) color = editColor(-.2, color);
|
|
67
|
+
}
|
|
68
|
+
return {backgroundColor: color};
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
getTrackStyle = (): object => {
|
|
72
|
+
const {trackColor} = this.props;
|
|
73
|
+
return {backgroundColor: convertColor(trackColor)};
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
getLabel = (position: 'left' | 'right'): string | null => {
|
|
77
|
+
const {labels} = this.props, positions: { [index: string]: number } = {left: 0, right: 1};
|
|
78
|
+
if (!labels.length || labels.length <= positions[position]) return null;
|
|
79
|
+
return labels[positions[position]];
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
renderMainLabel = () => {
|
|
83
|
+
const {label, mainLabelClass} = this.props;
|
|
84
|
+
if (!label.length) return null;
|
|
85
|
+
return <label className={classNames('main-label', mainLabelClass)}>{label}: </label>
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
renderLabel = (position: 'left' | 'right'): React.ReactNode => {
|
|
89
|
+
const label = this.getLabel(position);
|
|
90
|
+
if (label === null) return null;
|
|
91
|
+
return <label className={this.getLabelClass(position)}>{label}</label>;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
renderSwitch = (): React.ReactNode => {
|
|
95
|
+
const {onChange, onMouseEnter, onMouseLeave} = this.props;
|
|
96
|
+
return (
|
|
97
|
+
<div className='switch-track-container' onClick={onChange} onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave}>
|
|
98
|
+
<div className={this.getTrackClass()} style={this.getTrackStyle()}/>
|
|
99
|
+
<div className={this.getSwitchClass()} style={this.getSwitchStyle()}/>
|
|
100
|
+
</div>
|
|
101
|
+
)
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
render = () => {
|
|
105
|
+
const {id} = this.props;
|
|
106
|
+
return (
|
|
107
|
+
<div className={this.getContainerClass()} id={id}>
|
|
108
|
+
{this.renderMainLabel()}
|
|
109
|
+
{this.renderLabel('left')}
|
|
110
|
+
{this.renderSwitch()}
|
|
111
|
+
{this.renderLabel('right')}
|
|
112
|
+
</div>
|
|
113
|
+
)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2021. Dave van Rijn Development
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import React from 'react';
|
|
6
|
+
import {Switch} from "./switch";
|
|
7
|
+
import {ControlContext} from "../util/controlContext";
|
|
8
|
+
import {generateComponentId} from '../../..';
|
|
9
|
+
|
|
10
|
+
interface SwitchProps {
|
|
11
|
+
onChange: Function,
|
|
12
|
+
onMouseEnter?: Function,
|
|
13
|
+
onMouseLeave?: Function,
|
|
14
|
+
value: boolean,
|
|
15
|
+
labels: string[],
|
|
16
|
+
mainLabel: string;
|
|
17
|
+
baseColor?: string,
|
|
18
|
+
trackColor: string,
|
|
19
|
+
offColor: string,
|
|
20
|
+
containerClass: string,
|
|
21
|
+
labelClass: string,
|
|
22
|
+
switchClass: string,
|
|
23
|
+
trackClass: string,
|
|
24
|
+
mainLabelClass: string;
|
|
25
|
+
id?: string,
|
|
26
|
+
padding: 'small' | 'standard' | 'large',
|
|
27
|
+
disabled: boolean,
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
interface SwitchState {
|
|
31
|
+
isHovered: boolean,
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// noinspection JSUnusedGlobalSymbols
|
|
35
|
+
export default class SwitchController extends React.Component<SwitchProps, SwitchState> {
|
|
36
|
+
declare context: React.ContextType<typeof ControlContext>
|
|
37
|
+
static contextType = ControlContext;
|
|
38
|
+
|
|
39
|
+
static defaultProps = {
|
|
40
|
+
labels: [],
|
|
41
|
+
trackColor: 'color-gray-4',
|
|
42
|
+
offColor: '',
|
|
43
|
+
containerClass: '',
|
|
44
|
+
labelClass: '',
|
|
45
|
+
switchClass: '',
|
|
46
|
+
trackClass: '',
|
|
47
|
+
mainLabelClass: '',
|
|
48
|
+
padding: 'standard',
|
|
49
|
+
disabled: false,
|
|
50
|
+
mainLabel: '',
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
state = {
|
|
54
|
+
isHovered: false,
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
private id: string = generateComponentId(this.props.id);
|
|
58
|
+
|
|
59
|
+
onChange = (evt: any) => {
|
|
60
|
+
const {value, onChange, disabled} = this.props;
|
|
61
|
+
if (!disabled)
|
|
62
|
+
onChange(!value, evt);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
onMouseEnter = (evt: any) => {
|
|
66
|
+
const {onMouseEnter} = this.props;
|
|
67
|
+
this.setState({isHovered: true});
|
|
68
|
+
if (onMouseEnter !== undefined && onMouseEnter !== null)
|
|
69
|
+
onMouseEnter(evt);
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
onMouseLeave = (evt: any) => {
|
|
73
|
+
const {onMouseLeave} = this.props;
|
|
74
|
+
this.setState({isHovered: false});
|
|
75
|
+
if (onMouseLeave !== undefined && onMouseLeave !== null)
|
|
76
|
+
onMouseLeave(evt);
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
getBaseColor = (): string => {
|
|
80
|
+
const {baseColor} = this.props;
|
|
81
|
+
if (baseColor !== undefined && baseColor !== null) return baseColor;
|
|
82
|
+
// noinspection JSDeprecatedSymbols
|
|
83
|
+
return this.context.baseColor;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
render = () => {
|
|
87
|
+
const {
|
|
88
|
+
value, labels, trackColor, offColor, containerClass, labelClass, trackClass, switchClass,
|
|
89
|
+
padding, disabled, mainLabel, mainLabelClass
|
|
90
|
+
} = this.props, {isHovered} = this.state;
|
|
91
|
+
return <Switch onChange={this.onChange} onMouseEnter={this.onMouseEnter} onMouseLeave={this.onMouseLeave}
|
|
92
|
+
value={value} labels={labels} baseColor={this.getBaseColor()} trackColor={trackColor}
|
|
93
|
+
offColor={offColor} id={this.id} containerClass={containerClass} labelClass={labelClass}
|
|
94
|
+
switchClass={switchClass} trackClass={trackClass} padding={padding} disabled={disabled}
|
|
95
|
+
isHovered={isHovered} label={mainLabel} mainLabelClass={mainLabelClass}/>
|
|
96
|
+
}
|
|
97
|
+
}
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2021. Dave van Rijn Development
|
|
3
|
+
*/
|
|
4
|
+
import './style/dvrdInput.scss';
|
|
5
|
+
|
|
6
|
+
import React, {
|
|
7
|
+
ChangeEventHandler, CSSProperties, FocusEventHandler, InputHTMLAttributes, KeyboardEventHandler, MouseEventHandler,
|
|
8
|
+
PureComponent
|
|
9
|
+
} from 'react';
|
|
10
|
+
import classNames from "classnames";
|
|
11
|
+
import {ElementPosition, ErrorType, OrnamentShape} from '../util/interfaces';
|
|
12
|
+
import AwesomeIcon from '../icon/awesomeIcon';
|
|
13
|
+
import {directTimeout} from '../util/componentUtil';
|
|
14
|
+
import {ControlContext} from "../util/controlContext";
|
|
15
|
+
|
|
16
|
+
interface Props {
|
|
17
|
+
onChange: ChangeEventHandler;
|
|
18
|
+
onFocus?: FocusEventHandler;
|
|
19
|
+
onBlur?: FocusEventHandler;
|
|
20
|
+
onKeyDown: KeyboardEventHandler;
|
|
21
|
+
onClearInput?: MouseEventHandler;
|
|
22
|
+
value: string | number;
|
|
23
|
+
disabled?: boolean;
|
|
24
|
+
autoFocus?: boolean;
|
|
25
|
+
autoSelect?: boolean;
|
|
26
|
+
label?: string;
|
|
27
|
+
ornaments?: OrnamentShape | OrnamentShape[];
|
|
28
|
+
inputProps?: InputHTMLAttributes<any>;
|
|
29
|
+
className?: string;
|
|
30
|
+
inputClassName?: string;
|
|
31
|
+
labelClassName?: string;
|
|
32
|
+
ornamentClassName?: string;
|
|
33
|
+
errorClassName?: string;
|
|
34
|
+
error?: ErrorType;
|
|
35
|
+
id: string;
|
|
36
|
+
fullWidth?: boolean;
|
|
37
|
+
area?: boolean;
|
|
38
|
+
noResize?: boolean;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
interface State {
|
|
42
|
+
active: boolean;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export default class DvrdInput extends PureComponent<Props, State> {
|
|
46
|
+
static contextType = ControlContext;
|
|
47
|
+
private input: HTMLInputElement | HTMLTextAreaElement;
|
|
48
|
+
|
|
49
|
+
state: State = {
|
|
50
|
+
active: this.props.autoFocus === true,
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
onClickElement = (evt: React.MouseEvent) => {
|
|
54
|
+
evt.stopPropagation();
|
|
55
|
+
this.focusInput();
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
onFocusInput = (evt: React.FocusEvent) => {
|
|
59
|
+
const {disabled, onFocus, autoSelect, value} = this.props;
|
|
60
|
+
if (!disabled) {
|
|
61
|
+
this.setState({active: true});
|
|
62
|
+
if (autoSelect) {
|
|
63
|
+
this.input.selectionStart = 0;
|
|
64
|
+
this.input.selectionEnd = value.toString().length;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (onFocus)
|
|
68
|
+
onFocus(evt);
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
onBlurInput = (evt: React.FocusEvent) => {
|
|
72
|
+
const {disabled, onBlur} = this.props;
|
|
73
|
+
if (!disabled) this.setState({active: false});
|
|
74
|
+
if (onBlur)
|
|
75
|
+
onBlur(evt);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
hasError = (): boolean => this.props.error !== undefined && this.props.error !== null;
|
|
79
|
+
|
|
80
|
+
getContentStyle = (): CSSProperties => {
|
|
81
|
+
const style: CSSProperties = {gridTemplateColumns: '1fr'}, {ornaments, onClearInput} = this.props;
|
|
82
|
+
if (!ornaments || (Array.isArray(ornaments) && !ornaments.length)) style.gridTemplateColumns = '1fr';
|
|
83
|
+
let hasLeft: boolean = false, hasRight: boolean = onClearInput !== undefined;
|
|
84
|
+
if (Array.isArray(ornaments))
|
|
85
|
+
for (const ornament of ornaments) {
|
|
86
|
+
if (!ornament.placement || ornament.placement === ElementPosition.LEFT) hasLeft = true;
|
|
87
|
+
else if (ornament.placement === ElementPosition.RIGHT) hasRight = true;
|
|
88
|
+
if (hasLeft && hasRight) break;
|
|
89
|
+
}
|
|
90
|
+
else if (ornaments) {
|
|
91
|
+
hasLeft = !ornaments.placement || ornaments.placement === ElementPosition.LEFT;
|
|
92
|
+
hasRight = ornaments.placement === ElementPosition.RIGHT;
|
|
93
|
+
}
|
|
94
|
+
let columns: string = '1fr';
|
|
95
|
+
let paddingLeft: string = '';
|
|
96
|
+
let paddingRight: string = '';
|
|
97
|
+
if (hasRight) {
|
|
98
|
+
columns = `${columns} fit-content(100%)`
|
|
99
|
+
paddingRight = '.75rem';
|
|
100
|
+
}
|
|
101
|
+
if (hasLeft) {
|
|
102
|
+
columns = `fit-content(100%) ${columns}`;
|
|
103
|
+
paddingLeft = '.75rem';
|
|
104
|
+
}
|
|
105
|
+
style.gridTemplateColumns = columns;
|
|
106
|
+
style.paddingRight = paddingRight;
|
|
107
|
+
style.paddingLeft = paddingLeft;
|
|
108
|
+
return style;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
focusInput = (retry: boolean = false) => {
|
|
112
|
+
if (this.input) this.input.focus();
|
|
113
|
+
else if (retry) directTimeout(this.focusInput);
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
renderLabel = () => {
|
|
117
|
+
const {label, labelClassName} = this.props;
|
|
118
|
+
if (!label) return null;
|
|
119
|
+
return <label className={classNames('dvrd-input-label', labelClassName)}
|
|
120
|
+
onClick={this.onClickElement}>{label}</label>
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
renderInput = () => {
|
|
124
|
+
const {value, onChange, onKeyDown, disabled, inputProps, inputClassName, id, area, noResize} = this.props,
|
|
125
|
+
elementProps = {
|
|
126
|
+
...inputProps,
|
|
127
|
+
value,
|
|
128
|
+
onChange,
|
|
129
|
+
onFocus: this.onFocusInput,
|
|
130
|
+
onBlur: this.onBlurInput,
|
|
131
|
+
onKeyDown,
|
|
132
|
+
disabled,
|
|
133
|
+
className: classNames('dvrd-input', inputClassName, noResize && 'no-resize'),
|
|
134
|
+
id: `${id}-input`,
|
|
135
|
+
ref: (ref: HTMLInputElement & HTMLTextAreaElement) => {
|
|
136
|
+
this.input = ref
|
|
137
|
+
},
|
|
138
|
+
};
|
|
139
|
+
if (area) return <textarea {...elementProps}/>;
|
|
140
|
+
return <input {...elementProps}/>
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
renderLeftOrnament = () => {
|
|
144
|
+
const {ornaments} = this.props;
|
|
145
|
+
if (!ornaments || (Array.isArray(ornaments) && !ornaments.length)) return null;
|
|
146
|
+
if (Array.isArray(ornaments)) {
|
|
147
|
+
for (const ornament of ornaments) {
|
|
148
|
+
if (!ornament.placement || ornament.placement === ElementPosition.LEFT)
|
|
149
|
+
return this.renderOrnament(ornament);
|
|
150
|
+
}
|
|
151
|
+
} else if (!ornaments.placement || ornaments.placement === ElementPosition.LEFT)
|
|
152
|
+
return this.renderOrnament(ornaments);
|
|
153
|
+
return null;
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
renderRightOrnament = () => {
|
|
157
|
+
const {ornaments, onClearInput, value} = this.props;
|
|
158
|
+
if (onClearInput && value) return this.renderOrnament({
|
|
159
|
+
element: <AwesomeIcon onClick={onClearInput} name='backspace' className='clear-btn'/>,
|
|
160
|
+
placement: ElementPosition.RIGHT
|
|
161
|
+
});
|
|
162
|
+
if (!ornaments || (Array.isArray(ornaments) && !ornaments.length)) return null;
|
|
163
|
+
if (Array.isArray(ornaments)) {
|
|
164
|
+
for (const ornament of ornaments) {
|
|
165
|
+
if (ornament.placement === ElementPosition.RIGHT)
|
|
166
|
+
return this.renderOrnament(ornament);
|
|
167
|
+
}
|
|
168
|
+
} else if (ornaments.placement === ElementPosition.RIGHT)
|
|
169
|
+
return this.renderOrnament(ornaments);
|
|
170
|
+
return null;
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
renderOrnament = (ornament: OrnamentShape) => {
|
|
174
|
+
const {ornamentClassName} = this.props;
|
|
175
|
+
if (typeof ornament.element === 'string')
|
|
176
|
+
return <span className={classNames('dvrd-input-ornament', ornamentClassName)}
|
|
177
|
+
onClick={this.onClickElement}>{ornament.element}</span>;
|
|
178
|
+
else return React.cloneElement(ornament.element, {
|
|
179
|
+
className: classNames(ornament.element.props.className, ornamentClassName),
|
|
180
|
+
onClick: (evt: React.MouseEvent) => {
|
|
181
|
+
this.onClickElement(evt);
|
|
182
|
+
if (React.isValidElement(ornament.element)){
|
|
183
|
+
const element: React.ReactElement = ornament.element;
|
|
184
|
+
element.props.onClick?.(evt);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
renderError = () => {
|
|
191
|
+
const {error, errorClassName} = this.props;
|
|
192
|
+
return <span className={classNames('dvrd-input-error', errorClassName)}>{error}</span>
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
componentDidUpdate = (prevProps: Props) => {
|
|
196
|
+
if (this.props.autoFocus && !prevProps.autoFocus) this.focusInput(true);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
componentDidMount = () => {
|
|
200
|
+
if (this.props.autoFocus) this.focusInput(true);
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
render = () => {
|
|
204
|
+
const {disabled, className, id, fullWidth} = this.props, {active} = this.state;
|
|
205
|
+
return (
|
|
206
|
+
<div onClick={this.onClickElement} id={id}
|
|
207
|
+
className={classNames('dvrd-input-container', disabled && 'disabled', this.hasError() && 'error',
|
|
208
|
+
active && 'active', fullWidth && 'full', className)}>
|
|
209
|
+
{this.renderLabel()}
|
|
210
|
+
<div className='content-container' style={this.getContentStyle()}>
|
|
211
|
+
{this.renderLeftOrnament()}
|
|
212
|
+
{this.renderInput()}
|
|
213
|
+
{this.renderRightOrnament()}
|
|
214
|
+
</div>
|
|
215
|
+
{this.renderError()}
|
|
216
|
+
</div>
|
|
217
|
+
)
|
|
218
|
+
}
|
|
219
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2021. Dave van Rijn Development
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import React, {
|
|
6
|
+
FocusEventHandler,
|
|
7
|
+
InputHTMLAttributes,
|
|
8
|
+
KeyboardEventHandler,
|
|
9
|
+
MouseEventHandler,
|
|
10
|
+
TextareaHTMLAttributes,
|
|
11
|
+
useEffect,
|
|
12
|
+
useRef,
|
|
13
|
+
useState
|
|
14
|
+
} from 'react';
|
|
15
|
+
import {generateComponentId} from '../util/componentUtil';
|
|
16
|
+
import {enterPressed} from '../util/controlUtil';
|
|
17
|
+
import {ErrorType, OrnamentShape} from '../util/interfaces';
|
|
18
|
+
import DvrdInput from "./dvrdInput";
|
|
19
|
+
|
|
20
|
+
export interface InputControllerProps {
|
|
21
|
+
onChange: (value: string, evt: React.ChangeEvent) => void;
|
|
22
|
+
onFocus?: FocusEventHandler;
|
|
23
|
+
onBlur?: FocusEventHandler;
|
|
24
|
+
onKeyDown?: KeyboardEventHandler;
|
|
25
|
+
onEnter?: KeyboardEventHandler;
|
|
26
|
+
onClearInput?: MouseEventHandler;
|
|
27
|
+
value: string | number;
|
|
28
|
+
label?: string;
|
|
29
|
+
ornaments?: OrnamentShape | OrnamentShape[];
|
|
30
|
+
inputProps?: InputHTMLAttributes<any> & TextareaHTMLAttributes<any>;
|
|
31
|
+
disabled?: boolean;
|
|
32
|
+
autoFocus?: boolean;
|
|
33
|
+
autoSelect?: boolean;
|
|
34
|
+
className?: string;
|
|
35
|
+
inputClassName?: string;
|
|
36
|
+
labelClassName?: string;
|
|
37
|
+
ornamentClassName?: string;
|
|
38
|
+
errorClassName?: string;
|
|
39
|
+
error?: ErrorType;
|
|
40
|
+
id?: string;
|
|
41
|
+
fullWidth?: boolean;
|
|
42
|
+
placeholder?: string;
|
|
43
|
+
type?: string;
|
|
44
|
+
max?: number;
|
|
45
|
+
min?: number;
|
|
46
|
+
step?: number;
|
|
47
|
+
area?: boolean;
|
|
48
|
+
noResize?: boolean;
|
|
49
|
+
unControlled?: boolean;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export default function DvrdInputController(props: InputControllerProps) {
|
|
53
|
+
const {
|
|
54
|
+
disabled, onChange, onEnter, onKeyDown, inputProps, placeholder, type, max, min, step, onFocus, onBlur,
|
|
55
|
+
autoFocus, error, label, ornaments, className, inputClassName, labelClassName, ornamentClassName, fullWidth,
|
|
56
|
+
errorClassName, autoSelect, area, noResize, onClearInput, unControlled
|
|
57
|
+
} = props,
|
|
58
|
+
componentId = useRef(generateComponentId(props.id)),
|
|
59
|
+
[value, setValue] = useState(props.value);
|
|
60
|
+
|
|
61
|
+
function _onChange(evt: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) {
|
|
62
|
+
if (disabled) return;
|
|
63
|
+
const {value} = evt.target;
|
|
64
|
+
setValue(value);
|
|
65
|
+
onChange(value, evt);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function _onKeyDown(evt: React.KeyboardEvent<HTMLInputElement | HTMLTextAreaElement>) {
|
|
69
|
+
if (disabled) return;
|
|
70
|
+
if (onEnter && enterPressed(evt)) onEnter(evt);
|
|
71
|
+
else if (onKeyDown) onKeyDown(evt);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function getInputProps(): InputHTMLAttributes<any> | undefined {
|
|
75
|
+
if (!inputProps && !placeholder && !type) return undefined;
|
|
76
|
+
const props: InputHTMLAttributes<any> = inputProps ?? {};
|
|
77
|
+
if (placeholder !== undefined) props.placeholder = placeholder;
|
|
78
|
+
if (type !== undefined) props.type = type;
|
|
79
|
+
if (max !== undefined) props.max = max;
|
|
80
|
+
if (min !== undefined) props.min = min;
|
|
81
|
+
if (step !== undefined) props.step = step;
|
|
82
|
+
return props;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
useEffect(() => {
|
|
86
|
+
if(!unControlled) setValue(props.value);
|
|
87
|
+
}, [props.value])
|
|
88
|
+
|
|
89
|
+
return (
|
|
90
|
+
<DvrdInput onChange={_onChange} onFocus={onFocus} onKeyDown={_onKeyDown} value={value}
|
|
91
|
+
disabled={disabled} autoFocus={autoFocus} error={error} label={label} ornaments={ornaments}
|
|
92
|
+
inputProps={getInputProps()} className={className} inputClassName={inputClassName}
|
|
93
|
+
labelClassName={labelClassName} ornamentClassName={ornamentClassName} fullWidth={fullWidth}
|
|
94
|
+
errorClassName={errorClassName} id={componentId.current} onBlur={onBlur} autoSelect={autoSelect}
|
|
95
|
+
area={area} noResize={noResize} onClearInput={onClearInput}/>
|
|
96
|
+
);
|
|
97
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2021. Dave van Rijn Development
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import React, {FocusEventHandler, InputHTMLAttributes, KeyboardEventHandler, PureComponent} from 'react';
|
|
6
|
+
import {ChangeFunction, ErrorType, OrnamentShape} from '../util/interfaces';
|
|
7
|
+
import {roundTo} from '../util/miscUtil';
|
|
8
|
+
import DvrdInputController from "./dvrdInputController";
|
|
9
|
+
|
|
10
|
+
interface Props {
|
|
11
|
+
onChange: ChangeFunction<string | number>;
|
|
12
|
+
onFocus?: FocusEventHandler;
|
|
13
|
+
onBlur?: FocusEventHandler;
|
|
14
|
+
onKeyDown?: KeyboardEventHandler;
|
|
15
|
+
onEnter?: KeyboardEventHandler;
|
|
16
|
+
value: string | number;
|
|
17
|
+
label?: string;
|
|
18
|
+
ornaments?: OrnamentShape | OrnamentShape[];
|
|
19
|
+
inputProps?: InputHTMLAttributes<any>;
|
|
20
|
+
disabled?: boolean;
|
|
21
|
+
autoFocus?: boolean;
|
|
22
|
+
className?: string;
|
|
23
|
+
inputClassName?: string;
|
|
24
|
+
labelClassName?: string;
|
|
25
|
+
ornamentClassName?: string;
|
|
26
|
+
errorClassName?: string;
|
|
27
|
+
error?: ErrorType;
|
|
28
|
+
id?: string;
|
|
29
|
+
placeholder?: string;
|
|
30
|
+
multipleSeparators: boolean;
|
|
31
|
+
wholeNumbers: boolean;
|
|
32
|
+
asNumber?: boolean;
|
|
33
|
+
asCurrency: boolean;
|
|
34
|
+
autoSelect?: boolean;
|
|
35
|
+
}
|
|
36
|
+
// TODO Add option to disable symbols (+,-)
|
|
37
|
+
|
|
38
|
+
export default class DvrdNumberInput extends PureComponent<Props> {
|
|
39
|
+
static defaultProps = {
|
|
40
|
+
asCurrency: false,
|
|
41
|
+
wholeNumbers: false,
|
|
42
|
+
multipleSeparators: false,
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
onKeyDown = (evt: React.KeyboardEvent) => {
|
|
46
|
+
const {onKeyDown, asCurrency, wholeNumbers, inputProps, asNumber, value, multipleSeparators} = this.props, {
|
|
47
|
+
key,
|
|
48
|
+
target
|
|
49
|
+
} = evt;
|
|
50
|
+
let numValue = Number(value);
|
|
51
|
+
// Allow only number and simple edit keys
|
|
52
|
+
if (!/\d|Backspace|ArrowRight|ArrowLeft|ArrowUp|ArrowDown|Escape|Esc|Enter|[,.\-+]|Tab/.test(key) &&
|
|
53
|
+
!this.isPasting(evt))
|
|
54
|
+
evt.preventDefault();
|
|
55
|
+
|
|
56
|
+
if (key === 'ArrowUp') {
|
|
57
|
+
evt.preventDefault();
|
|
58
|
+
if (!isNaN(numValue)) {
|
|
59
|
+
const step = Number(inputProps?.step || asCurrency ? .01 : 1);
|
|
60
|
+
numValue += step;
|
|
61
|
+
let stringValue = numValue.toString();
|
|
62
|
+
if (asCurrency) stringValue = roundTo(numValue, 2);
|
|
63
|
+
if (asNumber) this.change(Number(stringValue), evt);
|
|
64
|
+
else this.change(stringValue, evt);
|
|
65
|
+
}
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (key === 'ArrowDown') {
|
|
69
|
+
evt.preventDefault();
|
|
70
|
+
if (!isNaN(numValue)) {
|
|
71
|
+
const step = Number(inputProps?.step || asCurrency ? .01 : 1);
|
|
72
|
+
numValue -= step;
|
|
73
|
+
let stringValue = numValue.toString();
|
|
74
|
+
if (asCurrency) stringValue = roundTo(numValue, 2);
|
|
75
|
+
if (asNumber) this.change(Number(stringValue), evt);
|
|
76
|
+
else this.change(stringValue, evt);
|
|
77
|
+
}
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const textIsSelected = this.textIsSelected(evt), position = (target as HTMLInputElement).selectionStart;
|
|
82
|
+
if (!textIsSelected) {
|
|
83
|
+
if (/[,.]/.test(key) && /[,.]/.test(value.toString()) && !multipleSeparators)
|
|
84
|
+
// Prevent double points or commas
|
|
85
|
+
evt.preventDefault();
|
|
86
|
+
|
|
87
|
+
if (wholeNumbers && /[.,]/.test(key))
|
|
88
|
+
evt.preventDefault();
|
|
89
|
+
else if (asCurrency && /[.,]\d{2}/.test(value.toString()) && /\d/.test(key) &&
|
|
90
|
+
this.positionIsAfterComma(position, value.toString()))
|
|
91
|
+
// Prevent more than 2 decimals when working with currencies
|
|
92
|
+
evt.preventDefault();
|
|
93
|
+
|
|
94
|
+
if ((position && position !== 0) && /[\-+]/.test(key))
|
|
95
|
+
// Prevent - or + sign after first position
|
|
96
|
+
evt.preventDefault();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (onKeyDown !== undefined && onKeyDown !== null)
|
|
100
|
+
onKeyDown(evt);
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
onChange = (value: string, evt: React.ChangeEvent) => {
|
|
104
|
+
const {asNumber} = this.props;
|
|
105
|
+
if (!/^-?[\d.,]*$/.test(value)) evt.preventDefault();
|
|
106
|
+
else {
|
|
107
|
+
let changeValue: string | number = value;
|
|
108
|
+
if (value.startsWith('.') || value.startsWith(',')) value = 0 + value;
|
|
109
|
+
if (asNumber) changeValue = Number(value.replace(',', '.'));
|
|
110
|
+
this.change(changeValue, evt);
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
change = (value: string | number, evt: React.ChangeEvent | React.KeyboardEvent) => {
|
|
115
|
+
this.setState({value}, () => {
|
|
116
|
+
this.props.onChange(value, evt as React.ChangeEvent);
|
|
117
|
+
})
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
isPasting = (evt: React.KeyboardEvent) => {
|
|
121
|
+
return (evt.metaKey || evt.ctrlKey) && evt.key === 'v';
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
positionIsAfterComma = (position: number | null, value: string) => {
|
|
125
|
+
if (position === null) return true;
|
|
126
|
+
if (value.includes('.')) return position > value.indexOf('.');
|
|
127
|
+
if (value.includes(',')) return position > value.indexOf(',');
|
|
128
|
+
return false;
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
textIsSelected = (evt: React.KeyboardEvent) => {
|
|
132
|
+
const target = evt.target as HTMLInputElement, start = target.selectionStart, end = target.selectionEnd;
|
|
133
|
+
return start != end;
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
render = () => {
|
|
137
|
+
return (
|
|
138
|
+
<DvrdInputController {...this.props} onChange={this.onChange} onKeyDown={this.onKeyDown}/>
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
}
|