@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.
Files changed (180) hide show
  1. package/index.ts +8 -1
  2. package/package.json +29 -23
  3. package/src/js/button/button.tsx +102 -0
  4. package/src/js/button/buttonController.tsx +179 -0
  5. package/src/js/button/closeButton.tsx +29 -0
  6. package/src/js/button/dvrdButton.tsx +128 -0
  7. package/src/js/button/outlinedButton.tsx +105 -0
  8. package/src/js/button/simpleButton.tsx +163 -0
  9. package/src/js/button/style/button.scss +95 -0
  10. package/src/js/button/style/closeButton.scss +15 -0
  11. package/src/js/button/style/dvrdButton.scss +30 -0
  12. package/src/js/button/style/outlinedButton.scss +84 -0
  13. package/src/js/button/style/simpleButton.scss +80 -0
  14. package/src/js/carousel/DVRCarousel.tsx +163 -0
  15. package/src/js/carousel/DVRCarouselController.tsx +95 -0
  16. package/src/js/carousel/style/DVRCarousel.scss +38 -0
  17. package/src/js/checkbox/checkbox.tsx +148 -0
  18. package/src/js/checkbox/checkboxController.tsx +131 -0
  19. package/src/js/checkbox/style/checkbox.scss +109 -0
  20. package/src/js/colorPicker/colorPicker.tsx +118 -0
  21. package/src/js/colorPicker/style/colorPicker.scss +20 -0
  22. package/src/js/date/dvrdDatePicker.tsx +357 -0
  23. package/src/js/date/style/dvrdDatePicker.scss +307 -0
  24. package/src/js/dialog/dialog.tsx +207 -0
  25. package/src/js/dialog/dialogController.tsx +70 -0
  26. package/src/js/dialog/inlineDialog.tsx +127 -0
  27. package/src/js/dialog/style/dialog.scss +61 -0
  28. package/src/js/events/withEvents.tsx +40 -0
  29. package/src/js/head/DVRHead.tsx +49 -0
  30. package/src/js/header/DVRHeader.tsx +417 -0
  31. package/src/js/header/style/header.scss +206 -0
  32. package/src/js/icon/awesomeIcon.tsx +20 -0
  33. package/src/js/image/imageUpload.tsx +69 -0
  34. package/src/js/image/style/imageUpload.scss +11 -0
  35. package/src/js/info/info.tsx +136 -0
  36. package/src/js/info/style/info.scss +39 -0
  37. package/src/js/input/animated/animatedTextField.tsx +159 -0
  38. package/src/js/input/date/dateField.tsx +360 -0
  39. package/src/js/input/date/dateFieldController.tsx +245 -0
  40. package/src/js/input/date/datePicker/datePicker.tsx +186 -0
  41. package/src/js/input/date/datePicker/style/datePicker.scss +102 -0
  42. package/src/js/input/date/input/dateInput.tsx +214 -0
  43. package/src/js/input/date/style/dateField.scss +40 -0
  44. package/src/js/input/date/timePicker/style/timePicker.scss +95 -0
  45. package/src/js/input/date/timePicker/timePicker.tsx +143 -0
  46. package/src/js/input/editor/DVREditor.tsx +21 -0
  47. package/src/js/input/number/numberInput.tsx +157 -0
  48. package/src/js/input/password/passwordInput.tsx +140 -0
  49. package/src/js/input/password/passwordRules.tsx +48 -0
  50. package/src/js/input/password/style/passwordInput.scss +39 -0
  51. package/src/js/input/password/style/passwordRules.scss +41 -0
  52. package/src/js/input/simple/style/simpleInput.scss +98 -0
  53. package/src/js/input/simple/v2/simpleInputV2.tsx +178 -0
  54. package/src/js/input/style/input.scss +138 -0
  55. package/src/js/input/v2/inputController_v2.tsx +250 -0
  56. package/src/js/input/v2/input_v2.tsx +7 -0
  57. package/src/js/label/label.tsx +196 -0
  58. package/src/js/label/style/label.scss +4 -0
  59. package/src/js/link/link.tsx +38 -0
  60. package/src/js/link/style/link.scss +30 -0
  61. package/src/js/loader/loader.tsx +79 -0
  62. package/src/js/loader/loaderController.tsx +61 -0
  63. package/src/js/loader/style/loader.scss +53 -0
  64. package/src/js/media/media.tsx +72 -0
  65. package/src/js/navigator/navigator.tsx +51 -0
  66. package/src/js/optionsList/dvrdOptionsList.tsx +112 -0
  67. package/src/js/optionsList/style/dvrdOptionsList.scss +84 -0
  68. package/src/js/optionsMenu/optionsMenu.tsx +187 -0
  69. package/src/js/optionsMenu/style/optionsMenu.scss +70 -0
  70. package/src/js/pdf/element/pdfElement.tsx +315 -0
  71. package/src/js/pdf/element/style/pdfElement.scss +111 -0
  72. package/src/js/pdf/history/pdfHistory.ts +57 -0
  73. package/src/js/pdf/image/pdfImage.tsx +175 -0
  74. package/src/js/pdf/image/style/pdfImage.scss +34 -0
  75. package/src/js/pdf/invoiceTable/pdfInvoiceTable.tsx +176 -0
  76. package/src/js/pdf/invoiceTable/style/pdfInvoiceTable.scss +32 -0
  77. package/src/js/pdf/pdfTemplateCreator.tsx +279 -0
  78. package/src/js/pdf/settings/buttons/iconButton.tsx +49 -0
  79. package/src/js/pdf/settings/buttons/style/iconButton.scss +50 -0
  80. package/src/js/pdf/settings/image/pdfImageSettings.tsx +82 -0
  81. package/src/js/pdf/settings/image/style/pdfImageSettings.scss +9 -0
  82. package/src/js/pdf/settings/invoiceTable/pdfInvoiceTableSettings.tsx +141 -0
  83. package/src/js/pdf/settings/invoiceTable/style/pdfInvoiceTableSettings.scss +38 -0
  84. package/src/js/pdf/settings/pdfElementSettings.tsx +86 -0
  85. package/src/js/pdf/settings/style/pdfElementSettings.scss +56 -0
  86. package/src/js/pdf/settings/text/pdfTextSettings.tsx +202 -0
  87. package/src/js/pdf/settings/text/style/pdfTextSettings.scss +94 -0
  88. package/src/js/pdf/style/pdfTemplateCreator.scss +118 -0
  89. package/src/js/pdf/text/pdfText.tsx +267 -0
  90. package/src/js/pdf/text/style/pdfText.scss +22 -0
  91. package/src/js/pdf/v2/pdfElement/pdfDraggableElement.tsx +193 -0
  92. package/src/js/pdf/v2/types/pdfTemplateTypes.ts +27 -0
  93. package/src/js/popup/style/withBackground.scss +29 -0
  94. package/src/js/popup/withBackground.tsx +92 -0
  95. package/src/js/select/async/asyncSelect.tsx +46 -0
  96. package/src/js/select/async/style/asyncSelect.scss +23 -0
  97. package/src/js/select/dvrdSelect.tsx +214 -0
  98. package/src/js/select/dvrdSelectController.tsx +81 -0
  99. package/src/js/select/select.tsx +310 -0
  100. package/src/js/select/selectController.tsx +341 -0
  101. package/src/js/select/style/dvrdSelect.scss +140 -0
  102. package/src/js/select/style/select.scss +199 -0
  103. package/src/js/sidebarMenu/sidebarMenu.tsx +167 -0
  104. package/src/js/sidebarMenu/style/sidebarMenu.scss +167 -0
  105. package/src/js/slider/DVRSlider.tsx +107 -0
  106. package/src/js/slider/style/DVRSlider.scss +88 -0
  107. package/src/js/snackbar/snackbar.tsx +72 -0
  108. package/src/js/snackbar/snackbarController.tsx +104 -0
  109. package/src/js/snackbar/style/snackbar.scss +46 -0
  110. package/src/js/switch/dvrdSwitch.tsx +53 -0
  111. package/src/js/switch/style/dvrdSwitch.scss +47 -0
  112. package/src/js/switch/style/switch.scss +84 -0
  113. package/src/js/switch/switch.tsx +115 -0
  114. package/src/js/switch/switchController.tsx +97 -0
  115. package/src/js/textField/dvrdInput.tsx +219 -0
  116. package/src/js/textField/dvrdInputController.tsx +97 -0
  117. package/src/js/textField/dvrdNumberInput.tsx +141 -0
  118. package/src/js/textField/dvrdPasswordInput.tsx +40 -0
  119. package/src/js/textField/style/dvrdInput.scss +114 -0
  120. package/src/js/textField/style/dvrdPassword.scss +15 -0
  121. package/src/js/topButton/style/topButton.scss +54 -0
  122. package/src/js/topButton/topButton.tsx +136 -0
  123. package/src/js/util/analyticsUtil.ts +41 -0
  124. package/src/js/util/colorUtil.ts +230 -0
  125. package/src/js/util/componentUtil.tsx +59 -0
  126. package/src/js/util/constants.ts +12 -0
  127. package/src/js/util/controlContext.tsx +46 -0
  128. package/src/js/util/controlUtil.ts +107 -0
  129. package/src/js/util/cookieUtil.ts +17 -0
  130. package/src/js/util/eventUtil.ts +65 -0
  131. package/src/js/util/googleUtil.ts +88 -0
  132. package/src/js/util/interfaces.ts +180 -0
  133. package/src/js/util/jwtUtil.ts +72 -0
  134. package/src/js/util/miscUtil.ts +170 -0
  135. package/src/js/util/momentUtil.ts +45 -0
  136. package/src/js/util/pdfUtil.ts +124 -0
  137. package/src/js/util/requestUtil.ts +145 -0
  138. package/src/js/util/responsiveUtil.ts +37 -0
  139. package/src/js/util/validationUtil.ts +13 -0
  140. package/src/res/img/lock-handle.png +0 -0
  141. package/src/res/img/lock-handle.webp +0 -0
  142. package/src/res/img/lock.png +0 -0
  143. package/src/res/img/lock.webp +0 -0
  144. package/src/style/common-icons-variables.scss +140 -0
  145. package/src/style/common-icons.scss +714 -0
  146. package/src/style/common-variables.scss +243 -0
  147. package/src/style/display-breakpoints.scss +141 -0
  148. package/src/style/fonts/common-icons.eot +0 -0
  149. package/src/style/fonts/common-icons.svg +150 -0
  150. package/src/style/fonts/common-icons.ttf +0 -0
  151. package/src/style/fonts/common-icons.woff +0 -0
  152. package/src/style/fonts/common-icons.woff2 +0 -0
  153. package/src/style/fonts/fontAwesome/css/all.css +7003 -0
  154. package/src/style/fonts/fontAwesome/css/all.min.css +6 -0
  155. package/src/style/fonts/fontAwesome/css/brands.css +1423 -0
  156. package/src/style/fonts/fontAwesome/css/brands.min.css +6 -0
  157. package/src/style/fonts/fontAwesome/css/fontawesome.css +5519 -0
  158. package/src/style/fonts/fontAwesome/css/fontawesome.min.css +6 -0
  159. package/src/style/fonts/fontAwesome/css/regular.css +19 -0
  160. package/src/style/fonts/fontAwesome/css/regular.min.css +6 -0
  161. package/src/style/fonts/fontAwesome/css/solid.css +19 -0
  162. package/src/style/fonts/fontAwesome/css/solid.min.css +6 -0
  163. package/src/style/fonts/fontAwesome/css/svg-with-js.css +634 -0
  164. package/src/style/fonts/fontAwesome/css/svg-with-js.min.css +6 -0
  165. package/src/style/fonts/fontAwesome/css/v4-font-face.css +26 -0
  166. package/src/style/fonts/fontAwesome/css/v4-font-face.min.css +6 -0
  167. package/src/style/fonts/fontAwesome/css/v4-shims.css +2146 -0
  168. package/src/style/fonts/fontAwesome/css/v4-shims.min.css +6 -0
  169. package/src/style/fonts/fontAwesome/css/v5-font-face.css +22 -0
  170. package/src/style/fonts/fontAwesome/css/v5-font-face.min.css +6 -0
  171. package/src/style/fonts/fontAwesome/webfonts/fa-brands-400.ttf +0 -0
  172. package/src/style/fonts/fontAwesome/webfonts/fa-brands-400.woff2 +0 -0
  173. package/src/style/fonts/fontAwesome/webfonts/fa-regular-400.ttf +0 -0
  174. package/src/style/fonts/fontAwesome/webfonts/fa-regular-400.woff2 +0 -0
  175. package/src/style/fonts/fontAwesome/webfonts/fa-solid-900.ttf +0 -0
  176. package/src/style/fonts/fontAwesome/webfonts/fa-solid-900.woff2 +0 -0
  177. package/src/style/fonts/fontAwesome/webfonts/fa-v4compatibility.ttf +0 -0
  178. package/src/style/fonts/fontAwesome/webfonts/fa-v4compatibility.woff2 +0 -0
  179. package/src/style/variables.scss +11 -0
  180. package/.gitignore +0 -31
@@ -0,0 +1,178 @@
1
+ /*
2
+ * Copyright (c) 2021. Dave van Rijn Development
3
+ */
4
+
5
+ import '../style/simpleInput.scss';
6
+
7
+ import React, {CSSProperties, PureComponent} from 'react';
8
+ import classNames from "classnames";
9
+ import {ControlContext} from "../../../util/controlContext";
10
+ import {colorIsWhite, convertColor, editColor} from "../../../util/colorUtil";
11
+ import {ElementPosition, OrnamentShape} from "../../../util/interfaces";
12
+ import AwesomeIcon from '../../../icon/awesomeIcon';
13
+ import {stopPropagation} from "../../../util/controlUtil";
14
+ import {DVRInputProps} from "../../v2/inputController_v2";
15
+
16
+ export default class SimpleInput extends PureComponent<DVRInputProps> {
17
+ declare context: React.ContextType<typeof ControlContext>;
18
+ static contextType = ControlContext;
19
+
20
+ private input: HTMLInputElement | HTMLTextAreaElement | null = null;
21
+
22
+ private onFocus = (evt: React.FocusEvent) => {
23
+ const {onKeyDown, autoSelect, value, onFocus} = this.props;
24
+ if (this.input) {
25
+ if (onKeyDown)
26
+ this.input.addEventListener('keydown', onKeyDown as any);
27
+ if (autoSelect && value.toString().length) {
28
+ this.input.setSelectionRange(0, value.toString().length);
29
+ }
30
+ }
31
+ onFocus(evt);
32
+ };
33
+
34
+ private onBlur = (evt: React.FocusEvent) => {
35
+ const {onKeyDown, onBlur} = this.props;
36
+ if (this.input) this.input.removeEventListener('keydown', onKeyDown as any);
37
+ onBlur(evt);
38
+ };
39
+
40
+ private getBaseColor = (trueBase: boolean = false): string => {
41
+ const themeColor = this.context.baseColor, {baseColor} = this.props;
42
+ let color: string;
43
+ if (baseColor) color = baseColor;
44
+ else color = themeColor;
45
+ if (colorIsWhite(color) && !trueBase) return this.getContrastColor(false);
46
+ return convertColor(color);
47
+ };
48
+
49
+ private getContrastColor = (allowWhite: boolean = true): string => {
50
+ const base = this.getBaseColor(true);
51
+ if (colorIsWhite(base) && allowWhite) return base;
52
+
53
+ const themeColor = this.context.contrastColor, {contrastColor} = this.props;
54
+ let color: string;
55
+ if (contrastColor && contrastColor.length) color = contrastColor;
56
+ else color = themeColor;
57
+ return convertColor(color);
58
+ };
59
+
60
+ private getBorderColor = (): string => {
61
+ const {isHovered, isFocused, disabled, error, borderColor} = this.props;
62
+ let color: string;
63
+ if (error) color = 'red';
64
+ else if (disabled) color = 'color-gray-4';
65
+ else if (isFocused) color = this.getBaseColor();
66
+ else if (isHovered) color = editColor(.3, this.getBaseColor());
67
+ else color = borderColor || this.context.borderColor || 'color-gray-3';
68
+ return convertColor(color);
69
+ };
70
+
71
+ private getCurrentColor = (): string => {
72
+ const {isHovered, isFocused, disabled, error} = this.props;
73
+ let color: string;
74
+ if (error) color = 'red';
75
+ else if (disabled) color = 'color-gray-4';
76
+ else if (isFocused) color = this.getBaseColor();
77
+ else if (isHovered) color = editColor(.3, this.getBaseColor());
78
+ else color = 'color-gray-3';
79
+ return convertColor(color);
80
+ };
81
+
82
+ private getContainerStyle = (): CSSProperties => ({
83
+ borderColor: this.getBorderColor(),
84
+ ...this.props.containerStyle,
85
+ });
86
+
87
+ private getLabelStyle = (): CSSProperties => ({
88
+ color: this.getCurrentColor(),
89
+ ...this.props.labelStyle,
90
+ });
91
+
92
+ private getInputStyle = (): CSSProperties => ({
93
+ ...this.props.inputStyle,
94
+ });
95
+
96
+ private getInputProps = () => {
97
+ const {placeholder, inputProps, onChange, value, id, disabled, readOnly} = this.props;
98
+ return {
99
+ ...inputProps,
100
+ className: classNames('dvr-simple-input', inputProps?.className),
101
+ ref: this.attachRef,
102
+ onBlur: this.onBlur,
103
+ onFocus: this.onFocus,
104
+ style: this.getInputStyle(),
105
+ placeholder,
106
+ onChange,
107
+ value,
108
+ id: `${id}-input`,
109
+ // type,
110
+ disabled,
111
+ readOnly,
112
+ };
113
+ };
114
+
115
+ private attachRef = (ref: HTMLInputElement | HTMLTextAreaElement | null) => {
116
+ this.input = ref;
117
+ };
118
+
119
+ private getOrnament = (placement: ElementPosition): OrnamentShape | null => {
120
+ const {ornaments, children} = this.props;
121
+ if (ornaments !== undefined) {
122
+ if (Array.isArray(ornaments)) {
123
+ for (const orn of ornaments)
124
+ if (orn.placement === placement) return orn;
125
+ } else if (ornaments.placement === placement) return ornaments;
126
+
127
+ if (Array.isArray(children)) {
128
+ for (const child of children)
129
+ if (child && child.hasOwnProperty('placement') && (child as OrnamentShape).placement ===
130
+ placement) return child as OrnamentShape;
131
+ } else if (children && children.hasOwnProperty('placement') && (children as OrnamentShape).placement ===
132
+ placement) return children as OrnamentShape;
133
+ }
134
+ return null;
135
+ };
136
+
137
+ private renderOrnament = (placement: ElementPosition) => {
138
+ const {onClick, clearAble, value, onClear, disabled} = this.props;
139
+ let toRender: OrnamentShape | null = this.getOrnament(placement);
140
+ if (!toRender && placement === ElementPosition.RIGHT && clearAble && !disabled)
141
+ toRender = {
142
+ allowPropagation: false,
143
+ element: <AwesomeIcon name='eraser' onClick={onClear} className={classNames('dvr-simple-input-clear',
144
+ !value.toString().length && 'invis')}/>,
145
+ placement
146
+ };
147
+ if (!toRender) return null;
148
+ else
149
+ return (
150
+ <div className={classNames('dvr-simple-input-ornament', onClick !== undefined && 'clickable')}
151
+ onClick={toRender.allowPropagation ? onClick : stopPropagation}>
152
+ {toRender.element}
153
+ </div>
154
+ )
155
+ };
156
+
157
+ render = () => {
158
+ const {
159
+ className, label, labelClassName, onMouseEnter, onMouseLeave, area, onClick, fullWidth, error, errorClassName,
160
+ errorStyle, disabled, id
161
+ } = this.props;
162
+ return (
163
+ <div className={classNames('dvr-simple-input-container', fullWidth && 'full-width',
164
+ error !== null && 'error', disabled && 'disabled', className)}
165
+ style={this.getContainerStyle()} onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave}
166
+ onClick={onClick} id={id}>
167
+ <label className={classNames('dvr-simple-input-label', labelClassName)}
168
+ style={this.getLabelStyle()}>{label}</label>
169
+ {this.renderOrnament(ElementPosition.LEFT)}
170
+ {area ? <textarea {...this.getInputProps()}/> : <input {...this.getInputProps()}/>}
171
+ {this.renderOrnament(ElementPosition.RIGHT)}
172
+ {error !== null &&
173
+ <label className={classNames('dvr-simple-input-error', errorClassName)}
174
+ style={errorStyle}>{error}</label>}
175
+ </div>
176
+ );
177
+ }
178
+ }
@@ -0,0 +1,138 @@
1
+ /*!
2
+ * Copyright (c) 2021. Dave van Rijn Development
3
+ */
4
+
5
+ @import '../../../style/common-variables';
6
+
7
+ .inputContainer {
8
+ position: relative;
9
+ width: 20rem;
10
+ max-width: 100%;
11
+ display: flex;
12
+ flex-direction: column;
13
+ background-color: white;
14
+ box-sizing: border-box;
15
+
16
+ &.fullWidth {
17
+ width: 100%;
18
+ .inputFieldContainer{
19
+ width: 100%;
20
+ }
21
+ }
22
+
23
+ .inputFieldContainer {
24
+ display: flex;
25
+ align-items: center;
26
+
27
+ .inputOrnament {
28
+ max-height: 100%;
29
+ padding: 0 .5rem;
30
+ display: flex;
31
+ align-items: center;
32
+
33
+ &.clickable {
34
+ cursor: pointer;
35
+ }
36
+ }
37
+
38
+ .input {
39
+ width: 100%;
40
+ flex: 1;
41
+ box-sizing: border-box;
42
+ border: none !important;
43
+ outline: none !important;
44
+ padding: .5rem;
45
+ font-size: 1rem;
46
+
47
+ &:disabled {
48
+ color: $color-gray-4 !important;
49
+ }
50
+ }
51
+
52
+ .clearButton {
53
+ font-size: .8rem;
54
+ color: $color-gray-5;
55
+ transition: color .2s ease-in-out;
56
+ cursor: pointer;
57
+ margin-right: .3rem;
58
+ padding: 0 .5rem;
59
+
60
+ &:hover {
61
+ color: red;
62
+ }
63
+ }
64
+ }
65
+
66
+ &.outlined {
67
+ @include borderRadius(.2rem);
68
+ border-width: 2px;
69
+ border-style: solid;
70
+ padding: .2rem;
71
+ position: relative;
72
+ transition: border-color .2s ease-in-out, margin-bottom .2s ease-in-out;
73
+ margin-top: .8rem;
74
+ margin-bottom: 0;
75
+
76
+ .label {
77
+ position: absolute;
78
+ background-color: white;
79
+ padding: .3rem .2rem;
80
+ left: .2rem;
81
+ top: -.9rem;
82
+ font-size: .7rem;
83
+ transition: top .2s ease-in-out, left .2s ease-in-out, font-size .2s ease-in-out, transform .2s ease-in-out, color .2s ease-in-out;
84
+ cursor: text;
85
+ user-select: none;
86
+ }
87
+
88
+ .input {
89
+ padding-left: .3rem;
90
+ }
91
+
92
+ .error {
93
+ position: absolute;
94
+ left: .4rem;
95
+ bottom: -1.4rem;
96
+ font-size: .9rem;
97
+ color: red;
98
+ pointer-events: none;
99
+ visibility: hidden;
100
+ opacity: 0;
101
+ transition: visibility .2s ease-in-out, opacity .2s ease-in-out;
102
+ }
103
+
104
+ &:not(.focused) {
105
+ .label {
106
+ transition-delay: .2s;
107
+ }
108
+
109
+ &:not(.filled) {
110
+ .label {
111
+ @include centerY;
112
+
113
+ &.multiLine {
114
+ top: calc(.2rem + 2px);
115
+ transform: none;
116
+ }
117
+
118
+ font-size: .9rem;
119
+ }
120
+ }
121
+ }
122
+
123
+ &.error {
124
+ margin-bottom: 2.4rem;
125
+
126
+ .error {
127
+ visibility: visible;
128
+ opacity: 1;
129
+ }
130
+ }
131
+ }
132
+
133
+ &.disabled {
134
+ &, & .label, & .inputFieldContainer .input {
135
+ cursor: not-allowed;
136
+ }
137
+ }
138
+ }
@@ -0,0 +1,250 @@
1
+ /*
2
+ * Copyright (c) 2021. Dave van Rijn Development
3
+ */
4
+ import React, {
5
+ ChangeEventHandler, CSSProperties, EventHandler, FocusEventHandler, InputHTMLAttributes, KeyboardEventHandler,
6
+ MouseEventHandler, PureComponent, ReactNode, TextareaHTMLAttributes
7
+ } from 'react';
8
+ import {assert, enterPressed} from "../../util/controlUtil";
9
+ import SimpleInput from "../simple/v2/simpleInputV2";
10
+ import {ControlVariant, ErrorType, OrnamentShape} from "../../util/interfaces";
11
+ import { generateComponentId } from '../../../..';
12
+
13
+ type HTMLProps = Omit<InputHTMLAttributes<any>, 'onChange' | 'step' | 'children'>;
14
+
15
+ export interface DVRInputControllerProps extends HTMLProps {
16
+ // Events
17
+ onChange: (value: string | number, evt: React.ChangeEvent) => void;
18
+ onEnter?: KeyboardEventHandler;
19
+ onFocus?: FocusEventHandler;
20
+ // Theme
21
+ variant: ControlVariant;
22
+ baseColor?: string;
23
+ contrastColor?: string;
24
+ // Classes
25
+ errorClassName: string;
26
+ inputContainerClassName: string;
27
+ labelClassName: string;
28
+ // Styles
29
+ containerStyle?: CSSProperties;
30
+ labelStyle?: CSSProperties;
31
+ errorStyle?: CSSProperties;
32
+ inputStyle?: CSSProperties;
33
+ // Other
34
+ error: ErrorType;
35
+ inputProps?: InputHTMLAttributes<any> & TextareaHTMLAttributes<any>;
36
+ ornaments?: OrnamentShape | OrnamentShape[];
37
+ area: boolean;
38
+ autoSelect: boolean;
39
+ autoFocus: boolean;
40
+ clearAble: boolean;
41
+ fullWidth: boolean;
42
+ defaultValue?: string | number;
43
+ label?: string | number;
44
+ value?: string | number;
45
+ step?: number;
46
+ unControlled?: boolean;
47
+ children?: ReactNode | ReactNode[] | OrnamentShape | OrnamentShape[];
48
+ }
49
+
50
+ export interface DVRInputProps extends Omit<DVRInputControllerProps, 'value' | 'onChange'> {
51
+ // Events
52
+ onChange: ChangeEventHandler;
53
+ onFocus: FocusEventHandler;
54
+ onBlur: FocusEventHandler;
55
+ onClear: EventHandler<any>;
56
+ onMouseEnter: MouseEventHandler;
57
+ onMouseLeave: MouseEventHandler;
58
+ onKeyDown: KeyboardEventHandler;
59
+ onClick: MouseEventHandler;
60
+ // State
61
+ isFocused: boolean;
62
+ isHovered: boolean;
63
+ // Theme
64
+ variant: ControlVariant;
65
+ baseColor?: string;
66
+ contrastColor?: string;
67
+ borderColor?: string;
68
+ // Classes
69
+ errorClassName: string;
70
+ inputContainerClassName: string;
71
+ labelClassName: string;
72
+ // Styles
73
+ containerStyle?: CSSProperties;
74
+ labelStyle?: CSSProperties;
75
+ errorStyle?: CSSProperties;
76
+ inputStyle?: CSSProperties;
77
+ // Other
78
+ error: ErrorType;
79
+ inputProps?: InputHTMLAttributes<any>;
80
+ ornaments?: OrnamentShape | OrnamentShape[];
81
+ clearAble: boolean;
82
+ fullWidth: boolean;
83
+ label?: string | number;
84
+ value: string | number;
85
+ step?: number;
86
+ unControlled?: boolean;
87
+ }
88
+
89
+ export const DefaultInputProps: Partial<DVRInputProps> = {
90
+ area: false,
91
+ clearAble: true,
92
+ disabled: false,
93
+ autoFocus: false,
94
+ autoSelect: false,
95
+ error: null,
96
+ errorClassName: '',
97
+ fullWidth: true,
98
+ className: '',
99
+ inputContainerClassName: '',
100
+ labelClassName: '',
101
+ readOnly: false,
102
+ type: 'text',
103
+ variant: ControlVariant.SIMPLE,
104
+ }
105
+
106
+ interface State {
107
+ isFocused: boolean;
108
+ isHovered: boolean;
109
+ value: string | number;
110
+ }
111
+
112
+ export default class InputControllerV2 extends PureComponent<DVRInputControllerProps, State> {
113
+ static defaultProps: Partial<DVRInputProps> = DefaultInputProps;
114
+ private readonly id: string;
115
+
116
+ private getValue = (props: DVRInputControllerProps = this.props): string | number => {
117
+ if (props.value !== undefined) return props.value;
118
+ if (props.defaultValue !== undefined) return props.defaultValue;
119
+ return '';
120
+ }
121
+
122
+ private validateProps = (props: DVRInputControllerProps) => {
123
+ if (props.value === undefined && props.defaultValue === undefined)
124
+ return 'value and defaultValue cannot both be undefined.';
125
+ return null;
126
+ };
127
+
128
+ constructor(props: DVRInputControllerProps) {
129
+ super(props);
130
+ const error = this.validateProps(props);
131
+ assert(error === null, error);
132
+ this.id = generateComponentId(props.id);
133
+ this.state = {
134
+ isFocused: false,
135
+ isHovered: false,
136
+ value: this.getValue(props),
137
+ }
138
+ }
139
+
140
+ onChange = (evt: React.ChangeEvent<any>) => {
141
+ const {onChange, type, step} = this.props, {value} = evt.target;
142
+ let finalValue: string | number = value;
143
+ if (type === 'number') {
144
+ if (step !== undefined) {
145
+ if (Number.isInteger(step)) finalValue = parseInt(finalValue as string);
146
+ else finalValue = parseFloat(finalValue as string);
147
+ }
148
+ }
149
+ evt.persist();
150
+ this.setState({value: finalValue.toString()}, () => {
151
+ onChange(finalValue, evt);
152
+ });
153
+ };
154
+
155
+ onClear = (evt: any) => {
156
+ const {clearAble, onChange} = this.props;
157
+ if (clearAble) {
158
+ this.setState({value: ''}, () => {
159
+ onChange('', evt);
160
+ this.setInputFocus();
161
+ });
162
+ }
163
+ }
164
+
165
+ onFocus = (evt: React.FocusEvent<any>) => {
166
+ const {onFocus} = this.props;
167
+ this.setState({isFocused: true});
168
+ if (onFocus) onFocus(evt);
169
+ }
170
+
171
+ onBlur = (evt: React.FocusEvent<any>) => {
172
+ const {onBlur} = this.props;
173
+ this.setState({isFocused: false});
174
+ if (onBlur) onBlur(evt);
175
+ }
176
+
177
+ onClick = (evt: React.MouseEvent<any>) => {
178
+ const {onClick} = this.props, {isFocused} = this.state;
179
+ if (!isFocused) this.setInputFocus();
180
+ if (onClick) onClick(evt);
181
+ }
182
+
183
+ onKeyDown = (evt: React.KeyboardEvent<any>) => {
184
+ const {onKeyDown, onEnter} = this.props;
185
+ if (onEnter && enterPressed(evt)) onEnter(evt);
186
+ else if (onKeyDown) onKeyDown(evt);
187
+ }
188
+
189
+ onMouseEnter = (evt: React.MouseEvent<any>) => {
190
+ const {onMouseEnter} = this.props;
191
+ this.setState({isHovered: true});
192
+ if (onMouseEnter) onMouseEnter(evt);
193
+ };
194
+
195
+ onMouseLeave = (evt: React.MouseEvent<any>) => {
196
+ const {onMouseLeave} = this.props;
197
+ this.setState({isHovered: false});
198
+ if (onMouseLeave) onMouseLeave(evt);
199
+ };
200
+
201
+ getInput = (): HTMLInputElement | HTMLTextAreaElement | null => {
202
+ const input = document.getElementById(`${this.id}-input`);
203
+ if (input !== null) return (input as HTMLInputElement | HTMLTextAreaElement);
204
+ return null;
205
+ };
206
+
207
+ getViewValue = (): string | number => {
208
+ const {value, unControlled} = this.props;
209
+ if(unControlled || value === undefined) return this.state.value;
210
+ return value;
211
+ };
212
+
213
+ setInputFocus = (retryOnNull: boolean = true) => {
214
+ const input = this.getInput();
215
+ if (input === null && retryOnNull)
216
+ window.setTimeout(() => {
217
+ this.setInputFocus(false);
218
+ });
219
+ if (input !== null) {
220
+ input.focus();
221
+ this.setState({isFocused: true});
222
+ }
223
+ };
224
+
225
+ componentDidMount = () => {
226
+ if (this.props.autoFocus) this.setInputFocus();
227
+ };
228
+
229
+ render = () => {
230
+ const {variant, unControlled} = this.props, {isFocused, isHovered} = this.state;
231
+ const props = {
232
+ ...this.props,
233
+ onChange: this.onChange,
234
+ onClear: this.onClear,
235
+ onFocus: this.onFocus,
236
+ onBlur: this.onBlur,
237
+ onClick: this.onClick,
238
+ onMouseEnter: this.onMouseEnter,
239
+ onMouseLeave: this.onMouseLeave,
240
+ onKeyDown: this.onKeyDown,
241
+ isFocused,
242
+ isHovered,
243
+ value: this.getViewValue(),
244
+ id: this.id,
245
+ unControlled,
246
+ }
247
+ if (variant === ControlVariant.SIMPLE)
248
+ return <SimpleInput {...props}>{this.props.children}</SimpleInput>
249
+ };
250
+ }
@@ -0,0 +1,7 @@
1
+ /*
2
+ * Copyright (c) 2021. Dave van Rijn Development
3
+ */
4
+
5
+ interface Props {
6
+
7
+ }