@torq-north/react-tools 1.2.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.es.js CHANGED
@@ -701,9 +701,9 @@ var CheckboxFormField = function (_a) {
701
701
 
702
702
  var DateFormField = function (_a) {
703
703
  var _b;
704
- var name = _a.name, label = _a.label, required = _a.required, icon = _a.icon, showSpace = _a.showSpace, slotProps = _a.slotProps, disabled = _a.disabled, helperText = _a.helperText, format = _a.format, _c = _a.validators, validators = _c === void 0 ? [] : _c;
705
- var _d = useField(name, __assign({ format: format, validate: required
706
- ? composeValidators.apply(void 0, __spreadArray(__spreadArray([], validators, false), [isRequired], false)) : composeValidators.apply(void 0, validators) }, slotProps === null || slotProps === void 0 ? void 0 : slotProps.field)), input = _d.input, meta = _d.meta;
704
+ var name = _a.name, label = _a.label, required = _a.required, icon = _a.icon, showSpace = _a.showSpace, slotProps = _a.slotProps, disabled = _a.disabled, helperText = _a.helperText, format = _a.format, _c = _a.validators, validators = _c === void 0 ? [] : _c, _d = _a.formatAsDate, formatAsDate = _d === void 0 ? false : _d;
705
+ var _e = useField(name, __assign({ format: format, validate: required
706
+ ? composeValidators.apply(void 0, __spreadArray(__spreadArray([], validators, false), [isRequired], false)) : composeValidators.apply(void 0, validators) }, slotProps === null || slotProps === void 0 ? void 0 : slotProps.field)), input = _e.input, meta = _e.meta;
707
707
  var datePickerProps = deepmerge({
708
708
  label: label,
709
709
  disabled: disabled,
@@ -720,7 +720,9 @@ var DateFormField = function (_a) {
720
720
  },
721
721
  },
722
722
  }, (_b = slotProps === null || slotProps === void 0 ? void 0 : slotProps.datePicker) !== null && _b !== void 0 ? _b : {});
723
- return (jsx(FieldWrapper, { icon: icon, showSpace: showSpace, children: jsx(DatePicker, __assign({ value: input.value ? dayjs(input.value) : null, onChange: function (value) { return input.onChange(value === null || value === void 0 ? void 0 : value.toJSON()); } }, datePickerProps)) }));
723
+ return (jsx(FieldWrapper, { icon: icon, showSpace: showSpace, children: jsx(DatePicker, __assign({ value: input.value ? dayjs(input.value) : null, onChange: function (value) {
724
+ return input.onChange(formatAsDate ? value === null || value === void 0 ? void 0 : value.format("YYYY-MM-DD") : value === null || value === void 0 ? void 0 : value.toJSON());
725
+ } }, datePickerProps)) }));
724
726
  };
725
727
 
726
728
  var DateTimeFormField = function (_a) {
@@ -815,6 +817,63 @@ var DropIndicator = styled(Box, {
815
817
  });
816
818
  });
817
819
 
820
+ function isAsset(file) {
821
+ return "id" in file;
822
+ }
823
+
824
+ var FileBox = styled(Box)(function (_a) {
825
+ var disableInteraction = _a.disableInteraction, theme = _a.theme;
826
+ var alterFunc = theme.palette.mode === "dark" ? lighten : darken;
827
+ return {
828
+ transition: "0.2s",
829
+ cursor: disableInteraction ? undefined : "pointer",
830
+ "&:hover": disableInteraction
831
+ ? undefined
832
+ : {
833
+ backgroundColor: alterFunc(theme.palette.background.paper, 0.05),
834
+ "&:active": {
835
+ transition: "0s",
836
+ backgroundColor: alterFunc(theme.palette.background.paper, 0.1),
837
+ },
838
+ },
839
+ "&:nth-of-type(odd)": {
840
+ backgroundColor: alterFunc(theme.palette.background.paper, 0.05),
841
+ "&:hover": disableInteraction
842
+ ? undefined
843
+ : {
844
+ backgroundColor: alterFunc(theme.palette.background.paper, 0.1),
845
+ "&:active": {
846
+ backgroundColor: alterFunc(theme.palette.background.paper, 0.15),
847
+ },
848
+ },
849
+ },
850
+ };
851
+ });
852
+
853
+ var UnstyledLink = styled("a")({
854
+ color: "unset",
855
+ textDecoration: "none",
856
+ });
857
+ function IndividualFile(_a) {
858
+ var file = _a.file, disableLink = _a.disableLink, allowDelete = _a.allowDelete, onDelete = _a.onDelete;
859
+ var isImage = isAsset(file)
860
+ ? file.mimeType.startsWith("image")
861
+ : ["png", "jpg", "jpeg", "gif"].some(function (ext) { return file.name.endsWith(ext); });
862
+ var content = (jsxs(Stack, { p: 0.5, direction: "row", justifyContent: "space-between", children: [jsxs(Stack, { direction: "row", gap: 2, alignItems: "center", children: [isImage ? jsx(ImageIcon, {}) : jsx(FileIcon, {}), jsx(Typography, { children: isAsset(file) ? file.fileName : file.name }), file instanceof File && (jsx(Tooltip, { title: "New File", placement: "right", children: jsx(SparkleIcon, { color: "info" }) }))] }), allowDelete && (jsx(IconButton, { color: "error", onClick: function (e) {
863
+ e.stopPropagation();
864
+ onDelete && onDelete(file);
865
+ }, children: jsx(DeleteIcon, {}) }))] }));
866
+ return (jsxs(FileBox, { disableInteraction: disableLink, children: [!disableLink && (jsx(UnstyledLink, { href: isAsset(file) ? file.fullPath : "#", target: "_blank", children: content })), disableLink && content] }));
867
+ }
868
+
869
+ function FileDisplay(_a) {
870
+ var files = _a.files, individualFileProps = __rest(_a, ["files"]);
871
+ if (!Array.isArray(files)) {
872
+ return (jsx(Box, { children: jsx(IndividualFile, __assign({ file: files }, individualFileProps)) }));
873
+ }
874
+ return (jsx(Stack, { children: files.map(function (f) { return (jsx(IndividualFile, __assign({ file: f }, individualFileProps), isAsset(f) ? f.id : f.name)); }) }));
875
+ }
876
+
818
877
  var propTypes = {exports: {}};
819
878
 
820
879
  var reactIs = {exports: {}};
@@ -4795,24 +4854,21 @@ function arrayify(value) {
4795
4854
  }
4796
4855
  return Array.isArray(value) ? value : [value];
4797
4856
  }
4798
- function useFileEditorField(name, options) {
4799
- var _a = useField(name, __assign({ validate: function (v) {
4800
- return options.required && v == null ? "This field is required" : undefined;
4801
- } }, options.fieldProps)), input = _a.input, meta = _a.meta;
4802
- var values = useMemo(function () { return arrayify(input.value); }, [input.value]);
4803
- var _b = useState(false), showError = _b[0], setShowError = _b[1];
4804
- var _c = useState(""), errorMessage = _c[0], setErrorMessage = _c[1];
4805
- var _d = useState(false), dragOverDropzone = _d[0], setDragOverDropzone = _d[1];
4857
+ function useFileEditor(value, onChange, options) {
4858
+ var values = useMemo(function () { return arrayify(value); }, [value]);
4859
+ var _a = useState(false), showError = _a[0], setShowError = _a[1];
4860
+ var _b = useState(""), errorMessage = _b[0], setErrorMessage = _b[1];
4861
+ var _c = useState(false), dragOverDropzone = _c[0], setDragOverDropzone = _c[1];
4806
4862
  var multiple = options.multiple, disabled = options.disabled, otherOptions = __rest(options, ["multiple", "disabled"]);
4807
- var _e = useDropzone(__assign({ multiple: multiple, noClick: multiple || values.length == 0, noKeyboard: multiple, disabled: disabled, onDragEnter: function () { return setDragOverDropzone(true); }, onDragOver: function (e) {
4863
+ var _d = useDropzone(__assign({ multiple: multiple, noClick: multiple || values.length == 0, noKeyboard: multiple, disabled: disabled, onDragEnter: function () { return setDragOverDropzone(true); }, onDragOver: function (e) {
4808
4864
  e.preventDefault();
4809
4865
  e.stopPropagation();
4810
4866
  }, onDragLeave: function () { return setDragOverDropzone(false); }, onDrop: function () { return setDragOverDropzone(false); }, onDropAccepted: function (acceptedFiles) {
4811
4867
  if (multiple) {
4812
- input.onChange((values !== null && values !== void 0 ? values : []).concat(acceptedFiles));
4868
+ onChange((values !== null && values !== void 0 ? values : []).concat(acceptedFiles));
4813
4869
  }
4814
4870
  else {
4815
- input.onChange(acceptedFiles[0]);
4871
+ onChange(acceptedFiles[0]);
4816
4872
  }
4817
4873
  }, onDropRejected: function (fileRejections) {
4818
4874
  setShowError(true);
@@ -4820,17 +4876,13 @@ function useFileEditorField(name, options) {
4820
4876
  setErrorMessage("Multiple files cannot be added to this field");
4821
4877
  }
4822
4878
  else {
4823
- //Other than trying to add multiple files to a single input, the only other errors
4824
- // it could be would be size or type violations. Since those are case by case and kind
4825
- // of hard to cram into a single error message, it's easier to just cover all our bases
4826
- // with one message
4827
4879
  setErrorMessage("Some files were rejected because they do not fit the size restriction or are not the right file type");
4828
4880
  }
4829
- } }, otherOptions)), getRootProps = _e.getRootProps, getInputProps = _e.getInputProps, open = _e.open;
4881
+ } }, otherOptions)), getRootProps = _d.getRootProps, getInputProps = _d.getInputProps, open = _d.open;
4830
4882
  var removeFile = useCallback(function (file) {
4831
4883
  var _a, _b;
4832
4884
  if (!multiple) {
4833
- input.onChange(null);
4885
+ onChange(null);
4834
4886
  return;
4835
4887
  }
4836
4888
  var index = (_a = values === null || values === void 0 ? void 0 : values.findIndex(function (f) { return f === file; })) !== null && _a !== void 0 ? _a : -1;
@@ -4839,8 +4891,8 @@ function useFileEditorField(name, options) {
4839
4891
  }
4840
4892
  var copy = (_b = values === null || values === void 0 ? void 0 : values.slice()) !== null && _b !== void 0 ? _b : [];
4841
4893
  copy.splice(index, 1);
4842
- input.onChange(copy);
4843
- }, [multiple, input.onChange, values]);
4894
+ onChange(copy);
4895
+ }, [multiple, onChange, values]);
4844
4896
  return {
4845
4897
  dragOverDropzone: dragOverDropzone,
4846
4898
  getRootProps: getRootProps,
@@ -4851,77 +4903,37 @@ function useFileEditorField(name, options) {
4851
4903
  showError: showError,
4852
4904
  setShowError: setShowError,
4853
4905
  errorMessage: errorMessage,
4854
- meta: meta,
4855
4906
  };
4856
4907
  }
4857
4908
 
4858
- function isAsset(file) {
4859
- return "id" in file;
4860
- }
4861
-
4862
- var FileBox = styled(Box)(function (_a) {
4863
- var disableInteraction = _a.disableInteraction, theme = _a.theme;
4864
- var alterFunc = theme.palette.mode === "dark" ? lighten : darken;
4865
- return {
4866
- transition: "0.2s",
4867
- cursor: disableInteraction ? undefined : "pointer",
4868
- "&:hover": disableInteraction
4869
- ? undefined
4870
- : {
4871
- backgroundColor: alterFunc(theme.palette.background.paper, 0.05),
4872
- "&:active": {
4873
- transition: "0s",
4874
- backgroundColor: alterFunc(theme.palette.background.paper, 0.1),
4875
- },
4876
- },
4877
- "&:nth-of-type(odd)": {
4878
- backgroundColor: alterFunc(theme.palette.background.paper, 0.05),
4879
- "&:hover": disableInteraction
4880
- ? undefined
4881
- : {
4882
- backgroundColor: alterFunc(theme.palette.background.paper, 0.1),
4883
- "&:active": {
4884
- backgroundColor: alterFunc(theme.palette.background.paper, 0.15),
4885
- },
4886
- },
4887
- },
4888
- };
4889
- });
4890
-
4891
- var UnstyledLink = styled("a")({
4892
- color: "unset",
4893
- textDecoration: "none",
4894
- });
4895
- function IndividualFile(_a) {
4896
- var file = _a.file, disableLink = _a.disableLink, allowDelete = _a.allowDelete, onDelete = _a.onDelete;
4897
- var isImage = isAsset(file)
4898
- ? file.mimeType.startsWith("image")
4899
- : ["png", "jpg", "jpeg", "gif"].some(function (ext) { return file.name.endsWith(ext); });
4900
- var content = (jsxs(Stack, { p: 0.5, direction: "row", justifyContent: "space-between", children: [jsxs(Stack, { direction: "row", gap: 2, alignItems: "center", children: [isImage ? jsx(ImageIcon, {}) : jsx(FileIcon, {}), jsx(Typography, { children: isAsset(file) ? file.fileName : file.name }), file instanceof File && (jsx(Tooltip, { title: "New File", placement: "right", children: jsx(SparkleIcon, { color: "info" }) }))] }), allowDelete && (jsx(IconButton, { color: "error", onClick: function (e) {
4901
- e.stopPropagation();
4902
- onDelete && onDelete(file);
4903
- }, children: jsx(DeleteIcon, {}) }))] }));
4904
- return (jsxs(FileBox, { disableInteraction: disableLink, children: [!disableLink && (jsx(UnstyledLink, { href: isAsset(file) ? file.fullPath : "#", target: "_blank", children: content })), disableLink && content] }));
4909
+ function FileEditor(_a) {
4910
+ var value = _a.value, onChange = _a.onChange, multiple = _a.multiple, disabled = _a.disabled, placeholder = _a.placeholder, accept = _a.accept, maxFiles = _a.maxFiles, maxSize = _a.maxSize, minSize = _a.minSize;
4911
+ var _b = useFileEditor(value, onChange, {
4912
+ multiple: multiple,
4913
+ accept: accept,
4914
+ disabled: disabled,
4915
+ maxFiles: maxFiles,
4916
+ maxSize: maxSize,
4917
+ minSize: minSize,
4918
+ }), getInputProps = _b.getInputProps, getRootProps = _b.getRootProps, dragOverDropzone = _b.dragOverDropzone, values = _b.values, removeFile = _b.removeFile, open = _b.open, showError = _b.showError, setShowError = _b.setShowError, errorMessage = _b.errorMessage;
4919
+ return (jsxs(Fragment$1, { children: [jsxs(Box, { sx: { position: "relative" }, children: [jsx("input", __assign({}, getInputProps())), jsx(DropIndicator, __assign({ multiple: multiple, dragOver: dragOverDropzone, disabled: disabled }, getRootProps(), { children: jsxs(Stack, { gap: 1, children: [values.length > 0 && (jsx(Box, { sx: { opacity: disabled ? 0.4 : 1 }, children: jsx(FileDisplay, { files: values !== null && values !== void 0 ? values : [], disableLink: true, allowDelete: !disabled, onDelete: removeFile }) })), (multiple || (values === null || values === void 0 ? void 0 : values.length) == 0) && (jsx(Button, { onClick: open, startIcon: jsx(AddIcon, {}), fullWidth: true, disabled: disabled, children: placeholder !== null && placeholder !== void 0 ? placeholder : "Add File".concat(multiple ? "s" : "") }))] }) })), dragOverDropzone && (jsx(Stack, { position: "absolute", top: 0, bottom: 0, left: 0, right: 0, justifyContent: "center", alignItems: "center", sx: { pointerEvents: "none" }, children: jsx(NoteAddIcon, { sx: { fontSize: 50 } }) }))] }), jsx(ErrorNotification, { open: showError, onClose: function () { return setShowError(false); }, message: errorMessage })] }));
4905
4920
  }
4906
4921
 
4907
- function FileDisplay(_a) {
4908
- var files = _a.files, individualFileProps = __rest(_a, ["files"]);
4909
- if (!Array.isArray(files)) {
4910
- return (jsx(Box, { children: jsx(IndividualFile, __assign({ file: files }, individualFileProps)) }));
4922
+ var isFileRequired = function (value) {
4923
+ if (value == null) {
4924
+ return "This field is required";
4911
4925
  }
4912
- return (jsx(Stack, { children: files.map(function (f) { return (jsx(IndividualFile, __assign({ file: f }, individualFileProps), isAsset(f) ? f.id : f.name)); }) }));
4913
- }
4926
+ if (Array.isArray(value) && value.length === 0) {
4927
+ return "This field is required";
4928
+ }
4929
+ return undefined;
4930
+ };
4914
4931
 
4915
4932
  function FileEditorField(_a) {
4916
- var name = _a.name, label = _a.label, icon = _a.icon, showSpace = _a.showSpace, multiple = _a.multiple, disabled = _a.disabled, helperText = _a.helperText, placeholder = _a.placeholder, required = _a.required, slotProps = _a.slotProps, accept = _a.accept, forceError = _a.forceError;
4917
- var _b = useFileEditorField(name, {
4918
- multiple: multiple,
4919
- accept: accept,
4920
- disabled: disabled,
4921
- required: required,
4922
- fieldProps: slotProps === null || slotProps === void 0 ? void 0 : slotProps.field,
4923
- }), getInputProps = _b.getInputProps, getRootProps = _b.getRootProps, dragOverDropzone = _b.dragOverDropzone, values = _b.values, removeFile = _b.removeFile, open = _b.open, showError = _b.showError, setShowError = _b.setShowError, errorMessage = _b.errorMessage, meta = _b.meta;
4924
- return (jsxs(FieldWrapper, { icon: icon, showSpace: showSpace, children: [jsxs(FormControl, { fullWidth: true, sx: { position: "relative" }, children: [label && (jsx(FormLabel, { disabled: disabled, error: meta.touched && meta.error, children: label })), jsx("input", __assign({}, getInputProps())), jsx(DropIndicator, __assign({ multiple: multiple, dragOver: dragOverDropzone, disabled: disabled }, getRootProps(), { children: jsxs(Stack, { gap: 1, children: [values.length > 0 && (jsx(Box, { sx: { opacity: disabled ? 0.4 : 1 }, children: jsx(FileDisplay, { files: values !== null && values !== void 0 ? values : [], disableLink: true, allowDelete: !disabled, onDelete: removeFile }) })), (multiple || (values === null || values === void 0 ? void 0 : values.length) == 0) && (jsx(Button, { onClick: open, startIcon: jsx(AddIcon, {}), fullWidth: true, disabled: disabled, children: placeholder !== null && placeholder !== void 0 ? placeholder : "Add File".concat(multiple ? "s" : "") }))] }) })), dragOverDropzone && (jsx(Stack, { position: "absolute", top: 0, bottom: 0, left: 0, right: 0, justifyContent: "center", alignItems: "center", sx: { pointerEvents: "none" }, children: jsx(NoteAddIcon, { sx: { fontSize: 50 } }) })), jsx(FormHelperText, { error: (meta.touched && meta.error) || forceError, children: (meta.touched && meta.error) || helperText })] }), jsx(ErrorNotification, { open: showError, onClose: function () { return setShowError(false); }, message: errorMessage })] }));
4933
+ var name = _a.name, label = _a.label, icon = _a.icon, showSpace = _a.showSpace, multiple = _a.multiple, disabled = _a.disabled, helperText = _a.helperText, placeholder = _a.placeholder, required = _a.required, slotProps = _a.slotProps, accept = _a.accept, maxFiles = _a.maxFiles, maxSize = _a.maxSize, minSize = _a.minSize, forceError = _a.forceError, _b = _a.validators, validators = _b === void 0 ? [] : _b;
4934
+ var _c = useField(name, __assign({ validate: required
4935
+ ? composeValidators.apply(void 0, __spreadArray(__spreadArray([], validators, false), [isFileRequired], false)) : composeValidators.apply(void 0, validators) }, slotProps === null || slotProps === void 0 ? void 0 : slotProps.field)), input = _c.input, meta = _c.meta;
4936
+ return (jsx(FieldWrapper, { icon: icon, showSpace: showSpace, children: jsxs(FormControl, { fullWidth: true, children: [label && (jsx(FormLabel, { disabled: disabled, error: meta.touched && meta.error, children: label })), jsx(FileEditor, { value: input.value, onChange: input.onChange, multiple: multiple, disabled: disabled, accept: accept, maxFiles: maxFiles, maxSize: maxSize, minSize: minSize, placeholder: placeholder }), jsx(FormHelperText, { error: (meta.touched && meta.error) || forceError, children: (meta.touched && meta.error) || helperText })] }) }));
4925
4937
  }
4926
4938
 
4927
4939
  function asInteger(value) {
@@ -5202,5 +5214,5 @@ function TabSet(_a) {
5202
5214
  }) }) }), jsx(Panel, __assign({}, actualTab === null || actualTab === void 0 ? void 0 : actualTab.PanelProps, { children: actualTab === null || actualTab === void 0 ? void 0 : actualTab.content }))] }));
5203
5215
  }
5204
5216
 
5205
- export { CheckboxFormField, CloseableDialog, ConfirmCancel, DateFormField, DateTimeFormField, EMAIL_REGEX, EditableBlock, ErrorMessage, ErrorNotification, ErrorOverlay, ExtendedPagination, ExtendedTable, FieldWrapper, FileDisplay, FileDropper, FileEditorField, LabeledValue, LoaderButton, MultiSelectFormField, POSTAL_CODE_REGEX, Panel, RadioFormField, SelectFormField, TabSet, TextFormField, asInteger, asPhoneNumber, enableAuthRedirect, isEmail, isPostalCode, sendDelete, sendFormDataPost, sendGet, sendPost, sendPut, useAsyncData, useAsyncDataCallback, useDebounce, useDebounceEffect, usePartialSetterState };
5217
+ export { CheckboxFormField, CloseableDialog, ConfirmCancel, DateFormField, DateTimeFormField, EMAIL_REGEX, EditableBlock, ErrorMessage, ErrorNotification, ErrorOverlay, ExtendedPagination, ExtendedTable, FieldWrapper, FileDisplay, FileDropper, FileEditor, FileEditorField, LabeledValue, LoaderButton, MultiSelectFormField, POSTAL_CODE_REGEX, Panel, RadioFormField, SelectFormField, TabSet, TextFormField, asInteger, asPhoneNumber, enableAuthRedirect, isEmail, isPostalCode, sendDelete, sendFormDataPost, sendGet, sendPost, sendPut, useAsyncData, useAsyncDataCallback, useDebounce, useDebounceEffect, useFileEditor, usePartialSetterState };
5206
5218
  //# sourceMappingURL=index.es.js.map