@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.cjs CHANGED
@@ -703,9 +703,9 @@ var CheckboxFormField = function (_a) {
703
703
 
704
704
  var DateFormField = function (_a) {
705
705
  var _b;
706
- 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;
707
- var _d = reactFinalForm.useField(name, __assign({ format: format, validate: required
708
- ? 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;
706
+ 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;
707
+ var _e = reactFinalForm.useField(name, __assign({ format: format, validate: required
708
+ ? 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;
709
709
  var datePickerProps = deepmerge({
710
710
  label: label,
711
711
  disabled: disabled,
@@ -722,7 +722,9 @@ var DateFormField = function (_a) {
722
722
  },
723
723
  },
724
724
  }, (_b = slotProps === null || slotProps === void 0 ? void 0 : slotProps.datePicker) !== null && _b !== void 0 ? _b : {});
725
- return (jsxRuntime.jsx(FieldWrapper, { icon: icon, showSpace: showSpace, children: jsxRuntime.jsx(xDatePickers.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)) }));
725
+ return (jsxRuntime.jsx(FieldWrapper, { icon: icon, showSpace: showSpace, children: jsxRuntime.jsx(xDatePickers.DatePicker, __assign({ value: input.value ? dayjs(input.value) : null, onChange: function (value) {
726
+ 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());
727
+ } }, datePickerProps)) }));
726
728
  };
727
729
 
728
730
  var DateTimeFormField = function (_a) {
@@ -817,6 +819,63 @@ var DropIndicator = material.styled(material.Box, {
817
819
  });
818
820
  });
819
821
 
822
+ function isAsset(file) {
823
+ return "id" in file;
824
+ }
825
+
826
+ var FileBox = material.styled(material.Box)(function (_a) {
827
+ var disableInteraction = _a.disableInteraction, theme = _a.theme;
828
+ var alterFunc = theme.palette.mode === "dark" ? material.lighten : material.darken;
829
+ return {
830
+ transition: "0.2s",
831
+ cursor: disableInteraction ? undefined : "pointer",
832
+ "&:hover": disableInteraction
833
+ ? undefined
834
+ : {
835
+ backgroundColor: alterFunc(theme.palette.background.paper, 0.05),
836
+ "&:active": {
837
+ transition: "0s",
838
+ backgroundColor: alterFunc(theme.palette.background.paper, 0.1),
839
+ },
840
+ },
841
+ "&:nth-of-type(odd)": {
842
+ backgroundColor: alterFunc(theme.palette.background.paper, 0.05),
843
+ "&:hover": disableInteraction
844
+ ? undefined
845
+ : {
846
+ backgroundColor: alterFunc(theme.palette.background.paper, 0.1),
847
+ "&:active": {
848
+ backgroundColor: alterFunc(theme.palette.background.paper, 0.15),
849
+ },
850
+ },
851
+ },
852
+ };
853
+ });
854
+
855
+ var UnstyledLink = material.styled("a")({
856
+ color: "unset",
857
+ textDecoration: "none",
858
+ });
859
+ function IndividualFile(_a) {
860
+ var file = _a.file, disableLink = _a.disableLink, allowDelete = _a.allowDelete, onDelete = _a.onDelete;
861
+ var isImage = isAsset(file)
862
+ ? file.mimeType.startsWith("image")
863
+ : ["png", "jpg", "jpeg", "gif"].some(function (ext) { return file.name.endsWith(ext); });
864
+ var content = (jsxRuntime.jsxs(material.Stack, { p: 0.5, direction: "row", justifyContent: "space-between", children: [jsxRuntime.jsxs(material.Stack, { direction: "row", gap: 2, alignItems: "center", children: [isImage ? jsxRuntime.jsx(ImageIcon, {}) : jsxRuntime.jsx(FileIcon, {}), jsxRuntime.jsx(material.Typography, { children: isAsset(file) ? file.fileName : file.name }), file instanceof File && (jsxRuntime.jsx(material.Tooltip, { title: "New File", placement: "right", children: jsxRuntime.jsx(SparkleIcon, { color: "info" }) }))] }), allowDelete && (jsxRuntime.jsx(material.IconButton, { color: "error", onClick: function (e) {
865
+ e.stopPropagation();
866
+ onDelete && onDelete(file);
867
+ }, children: jsxRuntime.jsx(DeleteIcon, {}) }))] }));
868
+ return (jsxRuntime.jsxs(FileBox, { disableInteraction: disableLink, children: [!disableLink && (jsxRuntime.jsx(UnstyledLink, { href: isAsset(file) ? file.fullPath : "#", target: "_blank", children: content })), disableLink && content] }));
869
+ }
870
+
871
+ function FileDisplay(_a) {
872
+ var files = _a.files, individualFileProps = __rest(_a, ["files"]);
873
+ if (!Array.isArray(files)) {
874
+ return (jsxRuntime.jsx(material.Box, { children: jsxRuntime.jsx(IndividualFile, __assign({ file: files }, individualFileProps)) }));
875
+ }
876
+ return (jsxRuntime.jsx(material.Stack, { children: files.map(function (f) { return (jsxRuntime.jsx(IndividualFile, __assign({ file: f }, individualFileProps), isAsset(f) ? f.id : f.name)); }) }));
877
+ }
878
+
820
879
  var propTypes = {exports: {}};
821
880
 
822
881
  var reactIs = {exports: {}};
@@ -4797,24 +4856,21 @@ function arrayify(value) {
4797
4856
  }
4798
4857
  return Array.isArray(value) ? value : [value];
4799
4858
  }
4800
- function useFileEditorField(name, options) {
4801
- var _a = reactFinalForm.useField(name, __assign({ validate: function (v) {
4802
- return options.required && v == null ? "This field is required" : undefined;
4803
- } }, options.fieldProps)), input = _a.input, meta = _a.meta;
4804
- var values = React.useMemo(function () { return arrayify(input.value); }, [input.value]);
4805
- var _b = React.useState(false), showError = _b[0], setShowError = _b[1];
4806
- var _c = React.useState(""), errorMessage = _c[0], setErrorMessage = _c[1];
4807
- var _d = React.useState(false), dragOverDropzone = _d[0], setDragOverDropzone = _d[1];
4859
+ function useFileEditor(value, onChange, options) {
4860
+ var values = React.useMemo(function () { return arrayify(value); }, [value]);
4861
+ var _a = React.useState(false), showError = _a[0], setShowError = _a[1];
4862
+ var _b = React.useState(""), errorMessage = _b[0], setErrorMessage = _b[1];
4863
+ var _c = React.useState(false), dragOverDropzone = _c[0], setDragOverDropzone = _c[1];
4808
4864
  var multiple = options.multiple, disabled = options.disabled, otherOptions = __rest(options, ["multiple", "disabled"]);
4809
- var _e = useDropzone(__assign({ multiple: multiple, noClick: multiple || values.length == 0, noKeyboard: multiple, disabled: disabled, onDragEnter: function () { return setDragOverDropzone(true); }, onDragOver: function (e) {
4865
+ var _d = useDropzone(__assign({ multiple: multiple, noClick: multiple || values.length == 0, noKeyboard: multiple, disabled: disabled, onDragEnter: function () { return setDragOverDropzone(true); }, onDragOver: function (e) {
4810
4866
  e.preventDefault();
4811
4867
  e.stopPropagation();
4812
4868
  }, onDragLeave: function () { return setDragOverDropzone(false); }, onDrop: function () { return setDragOverDropzone(false); }, onDropAccepted: function (acceptedFiles) {
4813
4869
  if (multiple) {
4814
- input.onChange((values !== null && values !== void 0 ? values : []).concat(acceptedFiles));
4870
+ onChange((values !== null && values !== void 0 ? values : []).concat(acceptedFiles));
4815
4871
  }
4816
4872
  else {
4817
- input.onChange(acceptedFiles[0]);
4873
+ onChange(acceptedFiles[0]);
4818
4874
  }
4819
4875
  }, onDropRejected: function (fileRejections) {
4820
4876
  setShowError(true);
@@ -4822,17 +4878,13 @@ function useFileEditorField(name, options) {
4822
4878
  setErrorMessage("Multiple files cannot be added to this field");
4823
4879
  }
4824
4880
  else {
4825
- //Other than trying to add multiple files to a single input, the only other errors
4826
- // it could be would be size or type violations. Since those are case by case and kind
4827
- // of hard to cram into a single error message, it's easier to just cover all our bases
4828
- // with one message
4829
4881
  setErrorMessage("Some files were rejected because they do not fit the size restriction or are not the right file type");
4830
4882
  }
4831
- } }, otherOptions)), getRootProps = _e.getRootProps, getInputProps = _e.getInputProps, open = _e.open;
4883
+ } }, otherOptions)), getRootProps = _d.getRootProps, getInputProps = _d.getInputProps, open = _d.open;
4832
4884
  var removeFile = React.useCallback(function (file) {
4833
4885
  var _a, _b;
4834
4886
  if (!multiple) {
4835
- input.onChange(null);
4887
+ onChange(null);
4836
4888
  return;
4837
4889
  }
4838
4890
  var index = (_a = values === null || values === void 0 ? void 0 : values.findIndex(function (f) { return f === file; })) !== null && _a !== void 0 ? _a : -1;
@@ -4841,8 +4893,8 @@ function useFileEditorField(name, options) {
4841
4893
  }
4842
4894
  var copy = (_b = values === null || values === void 0 ? void 0 : values.slice()) !== null && _b !== void 0 ? _b : [];
4843
4895
  copy.splice(index, 1);
4844
- input.onChange(copy);
4845
- }, [multiple, input.onChange, values]);
4896
+ onChange(copy);
4897
+ }, [multiple, onChange, values]);
4846
4898
  return {
4847
4899
  dragOverDropzone: dragOverDropzone,
4848
4900
  getRootProps: getRootProps,
@@ -4853,77 +4905,37 @@ function useFileEditorField(name, options) {
4853
4905
  showError: showError,
4854
4906
  setShowError: setShowError,
4855
4907
  errorMessage: errorMessage,
4856
- meta: meta,
4857
4908
  };
4858
4909
  }
4859
4910
 
4860
- function isAsset(file) {
4861
- return "id" in file;
4862
- }
4863
-
4864
- var FileBox = material.styled(material.Box)(function (_a) {
4865
- var disableInteraction = _a.disableInteraction, theme = _a.theme;
4866
- var alterFunc = theme.palette.mode === "dark" ? material.lighten : material.darken;
4867
- return {
4868
- transition: "0.2s",
4869
- cursor: disableInteraction ? undefined : "pointer",
4870
- "&:hover": disableInteraction
4871
- ? undefined
4872
- : {
4873
- backgroundColor: alterFunc(theme.palette.background.paper, 0.05),
4874
- "&:active": {
4875
- transition: "0s",
4876
- backgroundColor: alterFunc(theme.palette.background.paper, 0.1),
4877
- },
4878
- },
4879
- "&:nth-of-type(odd)": {
4880
- backgroundColor: alterFunc(theme.palette.background.paper, 0.05),
4881
- "&:hover": disableInteraction
4882
- ? undefined
4883
- : {
4884
- backgroundColor: alterFunc(theme.palette.background.paper, 0.1),
4885
- "&:active": {
4886
- backgroundColor: alterFunc(theme.palette.background.paper, 0.15),
4887
- },
4888
- },
4889
- },
4890
- };
4891
- });
4892
-
4893
- var UnstyledLink = material.styled("a")({
4894
- color: "unset",
4895
- textDecoration: "none",
4896
- });
4897
- function IndividualFile(_a) {
4898
- var file = _a.file, disableLink = _a.disableLink, allowDelete = _a.allowDelete, onDelete = _a.onDelete;
4899
- var isImage = isAsset(file)
4900
- ? file.mimeType.startsWith("image")
4901
- : ["png", "jpg", "jpeg", "gif"].some(function (ext) { return file.name.endsWith(ext); });
4902
- var content = (jsxRuntime.jsxs(material.Stack, { p: 0.5, direction: "row", justifyContent: "space-between", children: [jsxRuntime.jsxs(material.Stack, { direction: "row", gap: 2, alignItems: "center", children: [isImage ? jsxRuntime.jsx(ImageIcon, {}) : jsxRuntime.jsx(FileIcon, {}), jsxRuntime.jsx(material.Typography, { children: isAsset(file) ? file.fileName : file.name }), file instanceof File && (jsxRuntime.jsx(material.Tooltip, { title: "New File", placement: "right", children: jsxRuntime.jsx(SparkleIcon, { color: "info" }) }))] }), allowDelete && (jsxRuntime.jsx(material.IconButton, { color: "error", onClick: function (e) {
4903
- e.stopPropagation();
4904
- onDelete && onDelete(file);
4905
- }, children: jsxRuntime.jsx(DeleteIcon, {}) }))] }));
4906
- return (jsxRuntime.jsxs(FileBox, { disableInteraction: disableLink, children: [!disableLink && (jsxRuntime.jsx(UnstyledLink, { href: isAsset(file) ? file.fullPath : "#", target: "_blank", children: content })), disableLink && content] }));
4911
+ function FileEditor(_a) {
4912
+ 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;
4913
+ var _b = useFileEditor(value, onChange, {
4914
+ multiple: multiple,
4915
+ accept: accept,
4916
+ disabled: disabled,
4917
+ maxFiles: maxFiles,
4918
+ maxSize: maxSize,
4919
+ minSize: minSize,
4920
+ }), 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;
4921
+ return (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsxs(material.Box, { sx: { position: "relative" }, children: [jsxRuntime.jsx("input", __assign({}, getInputProps())), jsxRuntime.jsx(DropIndicator, __assign({ multiple: multiple, dragOver: dragOverDropzone, disabled: disabled }, getRootProps(), { children: jsxRuntime.jsxs(material.Stack, { gap: 1, children: [values.length > 0 && (jsxRuntime.jsx(material.Box, { sx: { opacity: disabled ? 0.4 : 1 }, children: jsxRuntime.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) && (jsxRuntime.jsx(material.Button, { onClick: open, startIcon: jsxRuntime.jsx(AddIcon, {}), fullWidth: true, disabled: disabled, children: placeholder !== null && placeholder !== void 0 ? placeholder : "Add File".concat(multiple ? "s" : "") }))] }) })), dragOverDropzone && (jsxRuntime.jsx(material.Stack, { position: "absolute", top: 0, bottom: 0, left: 0, right: 0, justifyContent: "center", alignItems: "center", sx: { pointerEvents: "none" }, children: jsxRuntime.jsx(NoteAddIcon, { sx: { fontSize: 50 } }) }))] }), jsxRuntime.jsx(ErrorNotification, { open: showError, onClose: function () { return setShowError(false); }, message: errorMessage })] }));
4907
4922
  }
4908
4923
 
4909
- function FileDisplay(_a) {
4910
- var files = _a.files, individualFileProps = __rest(_a, ["files"]);
4911
- if (!Array.isArray(files)) {
4912
- return (jsxRuntime.jsx(material.Box, { children: jsxRuntime.jsx(IndividualFile, __assign({ file: files }, individualFileProps)) }));
4924
+ var isFileRequired = function (value) {
4925
+ if (value == null) {
4926
+ return "This field is required";
4913
4927
  }
4914
- return (jsxRuntime.jsx(material.Stack, { children: files.map(function (f) { return (jsxRuntime.jsx(IndividualFile, __assign({ file: f }, individualFileProps), isAsset(f) ? f.id : f.name)); }) }));
4915
- }
4928
+ if (Array.isArray(value) && value.length === 0) {
4929
+ return "This field is required";
4930
+ }
4931
+ return undefined;
4932
+ };
4916
4933
 
4917
4934
  function FileEditorField(_a) {
4918
- 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;
4919
- var _b = useFileEditorField(name, {
4920
- multiple: multiple,
4921
- accept: accept,
4922
- disabled: disabled,
4923
- required: required,
4924
- fieldProps: slotProps === null || slotProps === void 0 ? void 0 : slotProps.field,
4925
- }), 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;
4926
- return (jsxRuntime.jsxs(FieldWrapper, { icon: icon, showSpace: showSpace, children: [jsxRuntime.jsxs(material.FormControl, { fullWidth: true, sx: { position: "relative" }, children: [label && (jsxRuntime.jsx(material.FormLabel, { disabled: disabled, error: meta.touched && meta.error, children: label })), jsxRuntime.jsx("input", __assign({}, getInputProps())), jsxRuntime.jsx(DropIndicator, __assign({ multiple: multiple, dragOver: dragOverDropzone, disabled: disabled }, getRootProps(), { children: jsxRuntime.jsxs(material.Stack, { gap: 1, children: [values.length > 0 && (jsxRuntime.jsx(material.Box, { sx: { opacity: disabled ? 0.4 : 1 }, children: jsxRuntime.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) && (jsxRuntime.jsx(material.Button, { onClick: open, startIcon: jsxRuntime.jsx(AddIcon, {}), fullWidth: true, disabled: disabled, children: placeholder !== null && placeholder !== void 0 ? placeholder : "Add File".concat(multiple ? "s" : "") }))] }) })), dragOverDropzone && (jsxRuntime.jsx(material.Stack, { position: "absolute", top: 0, bottom: 0, left: 0, right: 0, justifyContent: "center", alignItems: "center", sx: { pointerEvents: "none" }, children: jsxRuntime.jsx(NoteAddIcon, { sx: { fontSize: 50 } }) })), jsxRuntime.jsx(material.FormHelperText, { error: (meta.touched && meta.error) || forceError, children: (meta.touched && meta.error) || helperText })] }), jsxRuntime.jsx(ErrorNotification, { open: showError, onClose: function () { return setShowError(false); }, message: errorMessage })] }));
4935
+ 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;
4936
+ var _c = reactFinalForm.useField(name, __assign({ validate: required
4937
+ ? 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;
4938
+ return (jsxRuntime.jsx(FieldWrapper, { icon: icon, showSpace: showSpace, children: jsxRuntime.jsxs(material.FormControl, { fullWidth: true, children: [label && (jsxRuntime.jsx(material.FormLabel, { disabled: disabled, error: meta.touched && meta.error, children: label })), jsxRuntime.jsx(FileEditor, { value: input.value, onChange: input.onChange, multiple: multiple, disabled: disabled, accept: accept, maxFiles: maxFiles, maxSize: maxSize, minSize: minSize, placeholder: placeholder }), jsxRuntime.jsx(material.FormHelperText, { error: (meta.touched && meta.error) || forceError, children: (meta.touched && meta.error) || helperText })] }) }));
4927
4939
  }
4928
4940
 
4929
4941
  function asInteger(value) {
@@ -5219,6 +5231,7 @@ exports.ExtendedTable = ExtendedTable;
5219
5231
  exports.FieldWrapper = FieldWrapper;
5220
5232
  exports.FileDisplay = FileDisplay;
5221
5233
  exports.FileDropper = FileDropper;
5234
+ exports.FileEditor = FileEditor;
5222
5235
  exports.FileEditorField = FileEditorField;
5223
5236
  exports.LabeledValue = LabeledValue;
5224
5237
  exports.LoaderButton = LoaderButton;
@@ -5243,5 +5256,6 @@ exports.useAsyncData = useAsyncData;
5243
5256
  exports.useAsyncDataCallback = useAsyncDataCallback;
5244
5257
  exports.useDebounce = useDebounce;
5245
5258
  exports.useDebounceEffect = useDebounceEffect;
5259
+ exports.useFileEditor = useFileEditor;
5246
5260
  exports.usePartialSetterState = usePartialSetterState;
5247
5261
  //# sourceMappingURL=index.cjs.map