ish-ui 1.0.2

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 (126) hide show
  1. package/.eslintrc.json +39 -0
  2. package/.gitattributes +3 -0
  3. package/.github/workflows/release-package.yml +33 -0
  4. package/.prettierrc +5 -0
  5. package/README.md +80 -0
  6. package/assets/fonts/inter/Inter-Italic.var.woff2 +0 -0
  7. package/assets/fonts/inter/Inter-Roman.var.woff2 +0 -0
  8. package/assets/fonts/inter/LICENSE.txt +92 -0
  9. package/assets/fonts/inter/index.d.ts +10 -0
  10. package/assets/fonts/inter/inter.scss +17 -0
  11. package/package.json +108 -0
  12. package/src/actions/FieldProcessing.ts +21 -0
  13. package/src/actions/index.ts +10 -0
  14. package/src/appBar/AppBarHelpMenu.tsx +139 -0
  15. package/src/appBar/index.ts +9 -0
  16. package/src/buttons/AddButton.tsx +30 -0
  17. package/src/buttons/TimetableButton.tsx +19 -0
  18. package/src/buttons/index.ts +10 -0
  19. package/src/colorPicker/ColorPicker.tsx +223 -0
  20. package/src/colorPicker/index.ts +9 -0
  21. package/src/dialog/BrowserWarning.tsx +24 -0
  22. package/src/dialog/confirm/ConfirmBase.tsx +59 -0
  23. package/src/dialog/index.ts +11 -0
  24. package/src/dialog/message/Message.tsx +140 -0
  25. package/src/documents/DocumentIconsChooser.tsx +69 -0
  26. package/src/documents/index.ts +10 -0
  27. package/src/documents/utils/index.tsx +71 -0
  28. package/src/dynamicSizeList/DynamicSizeList.tsx +208 -0
  29. package/src/dynamicSizeList/index.ts +9 -0
  30. package/src/fieldMessage/ErrorMessage.tsx +28 -0
  31. package/src/fieldMessage/WarningMessage.tsx +28 -0
  32. package/src/fieldMessage/index.ts +11 -0
  33. package/src/fieldMessage/styles.ts +21 -0
  34. package/src/fileUploader/FileUploaderDialog.tsx +158 -0
  35. package/src/fileUploader/index.ts +9 -0
  36. package/src/formFields/CheckboxField.tsx +90 -0
  37. package/src/formFields/CodeEditorField.tsx +103 -0
  38. package/src/formFields/ColoredCheckBox.tsx +74 -0
  39. package/src/formFields/DateTimeField.tsx +61 -0
  40. package/src/formFields/EditInPlaceDateTimeField.tsx +285 -0
  41. package/src/formFields/EditInPlaceDurationField.tsx +79 -0
  42. package/src/formFields/EditInPlaceField.tsx +131 -0
  43. package/src/formFields/EditInPlaceFieldBase.tsx +241 -0
  44. package/src/formFields/EditInPlaceFileField.tsx +67 -0
  45. package/src/formFields/EditInPlaceMoneyField.tsx +64 -0
  46. package/src/formFields/EditInPlacePhoneField.tsx +97 -0
  47. package/src/formFields/EditInPlaceSearchSelect.tsx +562 -0
  48. package/src/formFields/FieldAdornments.tsx +70 -0
  49. package/src/formFields/FilePreview.tsx +149 -0
  50. package/src/formFields/FormRadioButtons.tsx +62 -0
  51. package/src/formFields/SelectCustomComponents.tsx +228 -0
  52. package/src/formFields/Switch.tsx +146 -0
  53. package/src/formFields/TextField.tsx +60 -0
  54. package/src/formFields/UneditableBase.tsx +101 -0
  55. package/src/formFields/index.ts +27 -0
  56. package/src/index.ts +26 -0
  57. package/src/layout/InfoPill.tsx +36 -0
  58. package/src/layout/index.ts +10 -0
  59. package/src/layout/resizable/ResizableWrapper.tsx +87 -0
  60. package/src/markdownEditor/HtmlEditor.tsx +44 -0
  61. package/src/markdownEditor/WysiwygEditor.tsx +137 -0
  62. package/src/markdownEditor/index.ts +12 -0
  63. package/src/markdownEditor/style.ts +188 -0
  64. package/src/markdownEditor/utils/index.ts +48 -0
  65. package/src/model/AppBar.ts +20 -0
  66. package/src/model/CommomObjects.ts +8 -0
  67. package/src/model/CommonFunctions.ts +20 -0
  68. package/src/model/Confirm.ts +27 -0
  69. package/src/model/Fields.ts +254 -0
  70. package/src/model/NestedEntity.ts +14 -0
  71. package/src/model/Theme.ts +42 -0
  72. package/src/model/index.ts +16 -0
  73. package/src/styles/GlobalStylesProvider.tsx +520 -0
  74. package/src/styles/StylesProviderCustom.tsx +8 -0
  75. package/src/styles/animateStyles.ts +94 -0
  76. package/src/styles/bootstrap.ts +503 -0
  77. package/src/styles/hooks.ts +44 -0
  78. package/src/styles/index.ts +18 -0
  79. package/src/styles/makeStyles.ts +21 -0
  80. package/src/styles/mixins/common.ts +40 -0
  81. package/src/styles/mixins/keyframes.ts +16 -0
  82. package/src/styles/mixins/prefixer.ts +21 -0
  83. package/src/tagsInput/AddTagMenu.tsx +74 -0
  84. package/src/tagsInput/AddTagMenuItem.tsx +50 -0
  85. package/src/tagsInput/TagInputList.tsx +428 -0
  86. package/src/tagsInput/index.ts +4 -0
  87. package/src/themes/appTheme.ts +324 -0
  88. package/src/themes/index.ts +10 -0
  89. package/src/themes/ishTheme.ts +401 -0
  90. package/src/timetable/datePeriodFilter/DayPeriodIcons.tsx +95 -0
  91. package/src/timetable/datePeriodFilter/index.ts +10 -0
  92. package/src/timetable/index.ts +11 -0
  93. package/src/timetable/miniCalendar/CalendarDay.tsx +109 -0
  94. package/src/timetable/miniCalendar/CalendarHeader.tsx +37 -0
  95. package/src/timetable/miniCalendar/CalendarWeekPanel.tsx +35 -0
  96. package/src/timetable/miniCalendar/WeekDay.tsx +49 -0
  97. package/src/timetable/miniCalendar/index.ts +13 -0
  98. package/src/utils/DOM/Links.ts +17 -0
  99. package/src/utils/DOM/getCaretCoordinates.ts +122 -0
  100. package/src/utils/DOM/getOS.ts +32 -0
  101. package/src/utils/DOM/index.ts +34 -0
  102. package/src/utils/dates/datesNormalizing.ts +4 -0
  103. package/src/utils/dates/format.ts +52 -0
  104. package/src/utils/dates/formatRelative.ts +22 -0
  105. package/src/utils/dates/formatString.ts +233 -0
  106. package/src/utils/dates/formatTimezone.ts +37 -0
  107. package/src/utils/dates/index.ts +15 -0
  108. package/src/utils/dates/timetable.ts +46 -0
  109. package/src/utils/events/index.ts +8 -0
  110. package/src/utils/fields/index.ts +23 -0
  111. package/src/utils/formatting/PhoneMasks.ts +320 -0
  112. package/src/utils/formatting/index.tsx +20 -0
  113. package/src/utils/hooks/index.ts +19 -0
  114. package/src/utils/index.ts +15 -0
  115. package/src/utils/numbers/decimalCalculation.ts +26 -0
  116. package/src/utils/numbers/index.ts +11 -0
  117. package/src/utils/numbers/numbersNormalizing.ts +52 -0
  118. package/src/utils/stubs/index.tsx +12 -0
  119. package/src/utils/tags/index.ts +53 -0
  120. package/tools/webpack/webpack.aliases.js +13 -0
  121. package/tools/webpack/webpack.config.dev.js +29 -0
  122. package/tools/webpack/webpack.config.prod.js +36 -0
  123. package/tools/webpack/webpack.helpers.js +26 -0
  124. package/tools/webpack/webpack.plugins.js +15 -0
  125. package/tools/webpack/webpack.rules.js +51 -0
  126. package/tsconfig.json +24 -0
@@ -0,0 +1,74 @@
1
+ /*
2
+ * Copyright ish group pty ltd 2023.
3
+ *
4
+ * This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License version 3 as published by the Free Software Foundation.
5
+ *
6
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
7
+ */
8
+
9
+ import React, { useCallback, useRef } from 'react';
10
+ import { ButtonBase, Typography } from '@mui/material';
11
+ import clsx from 'clsx';
12
+ import { alpha } from '@mui/material/styles';
13
+ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
14
+ import { useAppTheme } from '@src/themes';
15
+ import { rgbToHex } from '@mui/system/colorManipulator';
16
+ import { ColoredCheckboxFieldProps, FieldInputProps, FieldMetaProps } from '@src/model';
17
+ import { getCheckboxValue } from '@src/utils';
18
+
19
+ export function ColoredCheckBox<IP extends FieldInputProps, MP extends FieldMetaProps>({
20
+ input, label, color, className, disabled
21
+ }: ColoredCheckboxFieldProps<IP, MP>) {
22
+ const theme = useAppTheme();
23
+
24
+ const inputRef = useRef<HTMLInputElement>();
25
+
26
+ const onClick = useCallback(() => {
27
+ inputRef.current.click();
28
+ }, [inputRef.current]);
29
+
30
+ if (disabled) {
31
+ color = rgbToHex(theme.palette.divider);
32
+ }
33
+
34
+ return (
35
+ <div
36
+ className={clsx('centeredFlex cursor-pointer', className)}
37
+ onClick={onClick}
38
+ >
39
+ <input
40
+ ref={inputRef}
41
+ type="checkbox"
42
+ {...input}
43
+ checked={getCheckboxValue(input.value, false)}
44
+ hidden
45
+ />
46
+ <ButtonBase
47
+ component="div"
48
+ disabled={disabled}
49
+ sx={{
50
+ width: theme => theme.spacing(3),
51
+ height: theme => theme.spacing(3),
52
+ border: `2px solid ${color}`,
53
+ borderRadius: '50%',
54
+ fontSize: '10px',
55
+ backgroundColor: input.value ? `${color}` : alpha(`${color}`, 0.1),
56
+ color: theme => theme.palette.getContrastText(`${color}`)
57
+ }}
58
+ >
59
+ {input.value && <FontAwesomeIcon fixedWidth icon="check" />}
60
+ </ButtonBase>
61
+ {label && <Typography
62
+ variant="caption"
63
+ fontWeight="400"
64
+ sx={{
65
+ marginLeft: 1,
66
+ textDecoration: input.value ? 'line-through' : 'unset',
67
+ color: theme => (input.value ? theme.palette.text.disabled : 'unset'),
68
+ }}
69
+ >
70
+ {label}
71
+ </Typography>}
72
+ </div>
73
+ );
74
+ }
@@ -0,0 +1,61 @@
1
+ /*
2
+ * Copyright ish group pty ltd 2023.
3
+ *
4
+ * This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License version 3 as published by the Free Software Foundation.
5
+ *
6
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
7
+ */
8
+
9
+ import * as React from 'react';
10
+ import TextField from '@mui/material/TextField';
11
+ import { MobileDatePicker, MobileDateTimePicker, MobileTimePicker } from '@mui/x-date-pickers';
12
+ import { DD_MM_YYYY_HH_MM_SPECIAL, DD_MMM_YYYY_MINUSED, HH_MM_COLONED } from '@src/utils';
13
+
14
+ export const DateTimeField = props => {
15
+ const {
16
+ onChange, type, ampm, value, setPickerRef, formatDate, maxDate, minDate, ...rest
17
+ } = props;
18
+
19
+ const picker = type === 'date' ? (
20
+ <MobileDatePicker
21
+ ref={setPickerRef}
22
+ onChange={onChange}
23
+ ampm={ampm || false}
24
+ value={value}
25
+ format={formatDate || DD_MMM_YYYY_MINUSED}
26
+ maxDate={maxDate || undefined}
27
+ minDate={minDate || undefined}
28
+ renderInput={props => <TextField {...props} />}
29
+ variant="standard"
30
+ {...rest}
31
+ />
32
+ ) : type === 'time' ? (
33
+ <MobileTimePicker
34
+ ref={setPickerRef}
35
+ onChange={onChange}
36
+ ampm={ampm || false}
37
+ value={value}
38
+ format={formatDate || HH_MM_COLONED}
39
+ maxDate={maxDate || undefined}
40
+ minDate={minDate || undefined}
41
+ renderInput={props => <TextField {...props} />}
42
+ variant="standard"
43
+ {...rest}
44
+ />
45
+ ) : type === 'datetime' ? (
46
+ <MobileDateTimePicker
47
+ ref={setPickerRef}
48
+ onChange={onChange}
49
+ ampm={ampm || false}
50
+ value={value}
51
+ format={formatDate || DD_MM_YYYY_HH_MM_SPECIAL}
52
+ maxDate={maxDate || undefined}
53
+ minDate={minDate || undefined}
54
+ renderInput={props => <TextField {...props} />}
55
+ variant="standard"
56
+ {...rest}
57
+ />
58
+ ) : null;
59
+
60
+ return picker || null;
61
+ };
@@ -0,0 +1,285 @@
1
+ /*
2
+ * Copyright ish group pty ltd 2023.
3
+ *
4
+ * This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License version 3 as published by the Free Software Foundation.
5
+ *
6
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
7
+ */
8
+
9
+ /**
10
+ * Wrapper component for DateTimeField with edit in place functional
11
+ * */
12
+
13
+ import React, {
14
+ useEffect, useMemo, useRef, useState
15
+ } from 'react';
16
+ import clsx from 'clsx';
17
+ import DateRange from '@mui/icons-material/DateRange';
18
+ import QueryBuilder from '@mui/icons-material/QueryBuilder';
19
+ import { format, isValid } from 'date-fns';
20
+ import IconButton from '@mui/material/IconButton';
21
+ import { DateTimeField } from './DateTimeField';
22
+ import { makeAppStyles } from '@src/styles';
23
+ import EditInPlaceFieldBase from './EditInPlaceFieldBase';
24
+ import { EditInPlaceDateTimeFieldProps, FieldInputProps, FieldMetaProps } from '@src/model';
25
+ import {
26
+ appendTimezone,
27
+ formatStringDate, HH_MM_COLONED, III_DD_MMM_YYYY,
28
+ III_DD_MMM_YYYY_HH_MM,
29
+ prependTimezone,
30
+ YYYY_MM_DD_MINUSED
31
+ } from '@src/utils';
32
+ import { endFieldProcessingAction, startFieldProcessingAction } from '@src/actions/FieldProcessing';
33
+
34
+ const useStyles = makeAppStyles(theme => ({
35
+ inlinePickerButton: {
36
+ padding: '2px',
37
+ marginBottom: '-4px',
38
+ fontSize: '1.3em',
39
+ '&:hover': {
40
+ color: theme.palette.primary.main,
41
+ }
42
+ },
43
+ pickerButton: {
44
+ width: theme.spacing(4),
45
+ height: theme.spacing(4),
46
+ bottom: theme.spacing(-0.5)
47
+ },
48
+ inlineContainer: {
49
+ display: 'inline-block',
50
+ }
51
+ }));
52
+
53
+ interface InputTypes {
54
+ type?: 'date' | 'time' | 'datetime'
55
+ }
56
+
57
+ function EditInPlaceDateTimeField<IP extends FieldInputProps, MP extends FieldMetaProps>(
58
+ {
59
+ type,
60
+ formatDate,
61
+ formatTime,
62
+ formatDateTime,
63
+ timezone,
64
+ input,
65
+ fieldClasses = {},
66
+ inline,
67
+ meta: { error, invalid, active, dispatch },
68
+ labelAdornment,
69
+ label,
70
+ disabled,
71
+ formatValue,
72
+ className,
73
+ onKeyPress,
74
+ placeholder = 'No value',
75
+ persistValue,
76
+ warning,
77
+ defaultValue,
78
+ rightAligned,
79
+ processActionId
80
+ }: EditInPlaceDateTimeFieldProps<IP, MP> & InputTypes
81
+ ) {
82
+ const [textValue, setTextValue] = useState(defaultValue || '');
83
+ const [pickerOpened, setPickerOpened] = useState(false);
84
+
85
+ const inputNode = useRef<any>(null);
86
+
87
+ const classes = useStyles();
88
+
89
+ const formatDateInner = dateObj => {
90
+ if (!dateObj) {
91
+ return '';
92
+ }
93
+
94
+ if (dateObj.toString() === 'Invalid Date') return '';
95
+
96
+ switch (type) {
97
+ case 'date':
98
+ return format(dateObj, formatDate || III_DD_MMM_YYYY);
99
+ case 'time':
100
+ return format(dateObj, formatTime || HH_MM_COLONED);
101
+ case 'datetime':
102
+ return format(dateObj, formatDateTime || III_DD_MMM_YYYY_HH_MM);
103
+ default:
104
+ return dateObj.toString();
105
+ }
106
+ };
107
+
108
+ const dateValue = useMemo(() => {
109
+ let base = input.value ? new Date(input.value) : null;
110
+
111
+ if (base && timezone) {
112
+ base = appendTimezone(base, timezone);
113
+ }
114
+
115
+ return base;
116
+ }, [input.value, timezone]);
117
+
118
+ useEffect(() => {
119
+ setTextValue(formatDateInner(dateValue));
120
+ }, [dateValue]);
121
+
122
+ useEffect(() => {
123
+ if (!active && processActionId) {
124
+ dispatch(endFieldProcessingAction(input.name));
125
+ }
126
+ }, [input.value, input.name, active, processActionId]);
127
+
128
+ const onInputChange = e => {
129
+ if (e) setTextValue(e.target.value);
130
+ };
131
+
132
+ const openPicker = e => {
133
+ e.stopPropagation();
134
+ setPickerOpened(true);
135
+ };
136
+
137
+ const onChange = (v: Date) => {
138
+ if (v) {
139
+ let formatted;
140
+ if (formatValue) {
141
+ formatted = format(v, formatValue);
142
+ } else if (type === 'date') {
143
+ if (isValid(v)) {
144
+ formatted = format(v, YYYY_MM_DD_MINUSED);
145
+ } else {
146
+ formatted = null;
147
+ }
148
+ } else {
149
+ try {
150
+ formatted = v.toISOString();
151
+ } catch {
152
+ formatted = null;
153
+ }
154
+ }
155
+
156
+ if (timezone && formatted) {
157
+ formatted = prependTimezone(formatted, timezone)?.toISOString();
158
+ }
159
+
160
+ input.onChange(formatted);
161
+ input.onBlur(formatted);
162
+ return;
163
+ }
164
+ setTextValue('');
165
+ input.onChange(null);
166
+ input.onBlur(null);
167
+ };
168
+
169
+ const onBlur = () => {
170
+ dispatch(startFieldProcessingAction(input.name));
171
+
172
+ if (persistValue && !textValue) {
173
+ input.onChange(input.value);
174
+ input.onBlur(input.value);
175
+ setTextValue(formatDateInner(dateValue));
176
+ return;
177
+ }
178
+
179
+ const parsed = textValue
180
+ ? formatStringDate(textValue, type, dateValue || new Date(), formatDate || formatDateTime || formatTime)
181
+ : null;
182
+
183
+ if (parsed) {
184
+ let formatted;
185
+ if (formatValue) {
186
+ formatted = format(parsed, formatValue);
187
+ } else if (type === 'date' && isValid(parsed)) {
188
+ formatted = format(parsed, YYYY_MM_DD_MINUSED);
189
+ } else {
190
+ try {
191
+ formatted = parsed.toISOString();
192
+ } catch {
193
+ formatted = null;
194
+ }
195
+ }
196
+ setTextValue(formatDateInner(parsed));
197
+
198
+ if (timezone && formatted) {
199
+ formatted = prependTimezone(formatted, timezone).toISOString();
200
+ }
201
+
202
+ input.onChange(formatted);
203
+ input.onBlur(formatted);
204
+ } else {
205
+ setTextValue(null);
206
+ input.onChange(null);
207
+ input.onBlur(null);
208
+ }
209
+ };
210
+
211
+ const onClose = () => {
212
+ setPickerOpened(false);
213
+ };
214
+
215
+ const onEnterPress = e => {
216
+ if (e.keyCode === 13 && inputNode.current) {
217
+ inputNode.current.blur();
218
+ }
219
+ };
220
+
221
+ return (
222
+ <div
223
+ className={clsx(className, 'outline-none', {
224
+ [classes.inlineContainer]: inline,
225
+ 'pointer-events-none': disabled
226
+ })}
227
+ >
228
+ <div
229
+ id={input.name}
230
+ className="w-100"
231
+ >
232
+ <DateTimeField
233
+ type={type}
234
+ toolbarTitle={label}
235
+ open={pickerOpened}
236
+ value={dateValue}
237
+ onChange={onChange}
238
+ onClose={onClose}
239
+ renderInput={() => (
240
+ <EditInPlaceFieldBase
241
+ name={input.name}
242
+ value={textValue}
243
+ error={error}
244
+ invalid={invalid}
245
+ inline={inline}
246
+ label={label}
247
+ warning={warning}
248
+ fieldClasses={fieldClasses}
249
+ rightAligned={rightAligned}
250
+ shrink={Boolean(label || input.value)}
251
+ disabled={disabled}
252
+ labelAdornment={labelAdornment}
253
+ placeholder={placeholder}
254
+ editIcon={
255
+ <IconButton
256
+ tabIndex={-1}
257
+ onClick={openPicker}
258
+ classes={{
259
+ root: clsx(fieldClasses.text, inline ? classes.inlinePickerButton : classes.pickerButton)
260
+ }}
261
+ >
262
+ {type === 'time'
263
+ ? <QueryBuilder fontSize="inherit" color="inherit" />
264
+ : <DateRange color="inherit" fontSize="inherit" />}
265
+ </IconButton>}
266
+ InputProps={{
267
+ type: 'text',
268
+ onFocus: input.onFocus,
269
+ inputRef: inputNode,
270
+ value: textValue,
271
+ onKeyPress,
272
+ disabled,
273
+ onBlur,
274
+ onKeyDown: onEnterPress,
275
+ onChange: onInputChange
276
+ }}
277
+ />
278
+ )}
279
+ />
280
+ </div>
281
+ </div>
282
+ );
283
+ }
284
+
285
+ export default EditInPlaceDateTimeField;
@@ -0,0 +1,79 @@
1
+ /*
2
+ * Copyright ish group pty ltd 2023.
3
+ *
4
+ * This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License version 3 as published by the Free Software Foundation.
5
+ *
6
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
7
+ */
8
+
9
+ import React, { useCallback, useEffect, useState } from 'react';
10
+ import EditInPlaceField from './EditInPlaceField';
11
+ import { EditInPlaceFieldProps, FieldInputProps, FieldMetaProps } from '@src/model';
12
+ import { formatDurationMinutes } from '@src/utils/dates/formatString';
13
+
14
+ const parseDurationString = (duration: string): number => {
15
+ if (duration.indexOf('.') > -1) {
16
+ return Math.round(parseFloat(duration) * 60);
17
+ }
18
+
19
+ let min = 0;
20
+ const hoursMatch = duration.match(/(\d+)h/);
21
+ const minutesMatch = duration.match(/(\d+)m/);
22
+ const plainHours = parseFloat(duration);
23
+
24
+ if (hoursMatch) {
25
+ min += Number(hoursMatch[1]) * 60;
26
+ }
27
+
28
+ if (minutesMatch) {
29
+ min += Number(minutesMatch[1]);
30
+ }
31
+
32
+ if (!hoursMatch && !minutesMatch && !isNaN(plainHours)) {
33
+ min += Math.round(plainHours) * 60;
34
+ }
35
+
36
+ return min;
37
+ };
38
+
39
+ function EditInPlaceDurationField<FP extends FieldInputProps, MP extends FieldMetaProps>(
40
+ {
41
+ input, ...restProps
42
+ }: EditInPlaceFieldProps<FP, MP>) {
43
+ const [textValue, setTextValue] = useState(formatDurationMinutes(input.value || 0));
44
+
45
+ const onChange = useCallback(
46
+ e => {
47
+ setTextValue(e.target.value);
48
+ },
49
+ [input.onChange]
50
+ );
51
+
52
+ const onBlur = useCallback(
53
+ value => {
54
+ const parsed = parseDurationString(value);
55
+ input.onBlur(parsed);
56
+ setTextValue(formatDurationMinutes(parsed || 0));
57
+ },
58
+ [input.onBlur]
59
+ );
60
+
61
+ useEffect(() => {
62
+ setTextValue(formatDurationMinutes(input.value || 0));
63
+ }, [input.value]);
64
+
65
+ return (
66
+ <EditInPlaceField
67
+ {...restProps}
68
+ input={{
69
+ value: textValue,
70
+ onBlur,
71
+ onChange,
72
+ onFocus: input.onFocus,
73
+ name: input.name
74
+ }}
75
+ />
76
+ );
77
+ }
78
+
79
+ export default EditInPlaceDurationField;
@@ -0,0 +1,131 @@
1
+ /*
2
+ * Copyright ish group pty ltd 2023.
3
+ *
4
+ * This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License version 3 as published by the Free Software Foundation.
5
+ *
6
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
7
+ */
8
+
9
+ /**
10
+ * Wrapper component for Material Select and Text Field with edit in plaxce functional
11
+ * */
12
+
13
+ import React, { useMemo } from 'react';
14
+ import clsx from 'clsx';
15
+ import { Edit } from '@mui/icons-material';
16
+ import EditInPlaceFieldBase from './EditInPlaceFieldBase';
17
+ import { makeAppStyles } from '@src/styles';
18
+ import { EditInPlaceFieldProps, FieldInputProps, FieldMetaProps } from '@src/model';
19
+
20
+ const useStyles = makeAppStyles(theme => ({
21
+ rightAligned: {
22
+ display: 'flex',
23
+ alignItems: 'center',
24
+ justifyContent: 'flex-end'
25
+ },
26
+ inlineContainer: {
27
+ display: 'inline-block',
28
+ },
29
+ editIcon: {
30
+ fontSize: '24px',
31
+ color: theme.palette.divider,
32
+ verticalAlign: 'middle',
33
+ },
34
+ endAdornment: {
35
+ opacity: 0.5
36
+ }
37
+ }));
38
+
39
+ interface InputTypes {
40
+ type?: 'password' | 'number' | 'text'
41
+ }
42
+
43
+ function EditInPlaceField<IP extends FieldInputProps, MP extends FieldMetaProps>(
44
+ {
45
+ inputRef,
46
+ input,
47
+ label,
48
+ maxLength,
49
+ disabled,
50
+ min,
51
+ max,
52
+ step,
53
+ className,
54
+ onKeyPress,
55
+ labelAdornment,
56
+ truncateLines,
57
+ rightAligned,
58
+ defaultValue,
59
+ onKeyDown,
60
+ multiline,
61
+ inline,
62
+ warning,
63
+ preformatDisplayValue,
64
+ type = 'text',
65
+ meta: { error, invalid },
66
+ placeholder = 'No value',
67
+ fieldClasses = {},
68
+ InputProps = {}
69
+ }: EditInPlaceFieldProps<IP, MP> & InputTypes) {
70
+
71
+ const classes = useStyles();
72
+
73
+ const onBlur = () => {
74
+ input?.onBlur && input.onBlur(input.value);
75
+ };
76
+
77
+ const inputValue = useMemo(() => preformatDisplayValue
78
+ ? preformatDisplayValue(input.value === 0 ? input.value : input.value || defaultValue)
79
+ : (input.value === 0 ? String(input.value) : input.value) || defaultValue || '',
80
+ [preformatDisplayValue, input.value, defaultValue]);
81
+
82
+ return (
83
+ <div
84
+ id={input.name}
85
+ className={clsx(className, 'outline-none', {
86
+ [classes.rightAligned]: rightAligned,
87
+ [classes.inlineContainer]: inline
88
+ })}
89
+ >
90
+ <EditInPlaceFieldBase
91
+ name={input.name}
92
+ value={inputValue}
93
+ error={error}
94
+ invalid={invalid}
95
+ inline={inline}
96
+ label={label}
97
+ warning={warning}
98
+ fieldClasses={fieldClasses}
99
+ endAdornmentClass={classes.endAdornment}
100
+ rightAligned={rightAligned}
101
+ shrink={Boolean(label || input.value)}
102
+ disabled={disabled}
103
+ labelAdornment={labelAdornment}
104
+ placeholder={placeholder}
105
+ hideArrows={['percentage', 'number'].includes(type)}
106
+ editIcon={<Edit fontSize="inherit" />}
107
+ InputProps={{
108
+ multiline,
109
+ onKeyPress,
110
+ onBlur,
111
+ onFocus: input.onFocus,
112
+ maxRows: truncateLines,
113
+ inputProps: {
114
+ ref: inputRef,
115
+ maxLength,
116
+ step,
117
+ min,
118
+ max,
119
+ onKeyDown,
120
+ type,
121
+ },
122
+ value: inputValue,
123
+ onChange: v => (type === 'number' && max && Number(v) > Number(max) ? null : input.onChange(v)),
124
+ ...InputProps
125
+ }}
126
+ />
127
+ </div>
128
+ );
129
+ }
130
+
131
+ export default EditInPlaceField;