formik-form-components 0.2.15 → 0.2.17

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.js CHANGED
@@ -13071,50 +13071,84 @@ function MultiFilePreview({
13071
13071
  function SingleFilePreview({
13072
13072
  file
13073
13073
  }) {
13074
+ var _a;
13074
13075
  if (!file) {
13075
13076
  return null;
13076
13077
  }
13077
- const imgUrl = (typeof file === "string" ? file : file.preview) || "";
13078
- return /* @__PURE__ */ jsxRuntime.jsx(
13078
+ const imgUrl = typeof file === "string" ? file : file.preview;
13079
+ const isImage = typeof file === "string" ? /\\.(jpg|jpeg|png|gif|bmp|webp)$/i.test(file) : (_a = file.type) == null ? void 0 : _a.startsWith("image/");
13080
+ const fileName = typeof file === "string" ? file.split("/").pop() : file.name || "file";
13081
+ const fileSize = typeof file === "string" ? null : file.size ? formatFileSize(file.size) : null;
13082
+ if (isImage && imgUrl) {
13083
+ return /* @__PURE__ */ jsxRuntime.jsx(
13084
+ material.Box,
13085
+ {
13086
+ sx: {
13087
+ position: "relative",
13088
+ width: "100%",
13089
+ height: "100%",
13090
+ "& .wrapper": {
13091
+ width: "100%",
13092
+ height: "100%",
13093
+ position: "relative"
13094
+ }
13095
+ },
13096
+ children: /* @__PURE__ */ jsxRuntime.jsx(
13097
+ Image_default,
13098
+ {
13099
+ src: imgUrl,
13100
+ effect: "opacity",
13101
+ wrapperProps: {
13102
+ style: {
13103
+ width: "100%",
13104
+ height: "100%",
13105
+ position: "relative",
13106
+ display: "flex",
13107
+ alignItems: "center",
13108
+ justifyContent: "center",
13109
+ backgroundColor: "background.paper"
13110
+ }
13111
+ },
13112
+ style: {
13113
+ maxWidth: "100%",
13114
+ maxHeight: "100%",
13115
+ objectFit: "contain",
13116
+ borderRadius: 1
13117
+ }
13118
+ }
13119
+ )
13120
+ }
13121
+ );
13122
+ }
13123
+ return /* @__PURE__ */ jsxRuntime.jsxs(
13079
13124
  material.Box,
13080
13125
  {
13081
13126
  sx: {
13082
- position: "relative",
13127
+ p: 2,
13083
13128
  width: "100%",
13084
13129
  height: "100%",
13085
- "& .wrapper": {
13086
- width: "100%",
13087
- height: "100%",
13088
- position: "relative"
13089
- }
13130
+ display: "flex",
13131
+ flexDirection: "column",
13132
+ alignItems: "center",
13133
+ justifyContent: "center",
13134
+ textAlign: "center"
13090
13135
  },
13091
- children: /* @__PURE__ */ jsxRuntime.jsx(
13092
- Image_default,
13093
- {
13094
- src: imgUrl,
13095
- effect: "opacity",
13096
- wrapperProps: {
13097
- style: {
13098
- width: "100%",
13099
- height: "100%",
13100
- position: "relative"
13101
- }
13102
- },
13103
- style: {
13104
- top: 8,
13105
- left: 8,
13106
- zIndex: 8,
13107
- borderRadius: 1,
13108
- position: "absolute",
13109
- width: "calc(100% - 16px)",
13110
- height: "calc(100% - 16px)",
13111
- objectFit: "cover"
13112
- }
13113
- }
13114
- )
13136
+ children: [
13137
+ /* @__PURE__ */ jsxRuntime.jsx(Iconify_default, { icon: "eva:file-text-outline", width: 40, sx: { mb: 1 } }),
13138
+ /* @__PURE__ */ jsxRuntime.jsx(material.Typography, { variant: "subtitle2", noWrap: true, children: fileName }),
13139
+ fileSize && /* @__PURE__ */ jsxRuntime.jsx(material.Typography, { variant: "caption", color: "text.secondary", children: fileSize })
13140
+ ]
13115
13141
  }
13116
13142
  );
13117
13143
  }
13144
+ function formatFileSize(bytes) {
13145
+ if (bytes === 0)
13146
+ return "0 Bytes";
13147
+ const k = 1024;
13148
+ const sizes = ["Bytes", "KB", "MB", "GB"];
13149
+ const i = Math.floor(Math.log(bytes) / Math.log(k));
13150
+ return `${parseFloat((bytes / Math.pow(k, i)).toFixed(2))} ${sizes[i]}`;
13151
+ }
13118
13152
  function BackgroundIllustration() {
13119
13153
  const theme = styles.useTheme();
13120
13154
  const PRIMARY_MAIN = theme.palette.primary.main;
@@ -13759,16 +13793,7 @@ function Upload({
13759
13793
  },
13760
13794
  children: [
13761
13795
  /* @__PURE__ */ jsxRuntime.jsx("input", { ...getInputProps() }),
13762
- /* @__PURE__ */ jsxRuntime.jsx(
13763
- Placeholder,
13764
- {
13765
- sx: {
13766
- ...hasFile && {
13767
- opacity: 0
13768
- }
13769
- }
13770
- }
13771
- ),
13796
+ hasFile ? null : /* @__PURE__ */ jsxRuntime.jsx(Placeholder, {}),
13772
13797
  hasFile && /* @__PURE__ */ jsxRuntime.jsx(SingleFilePreview, { file })
13773
13798
  ]
13774
13799
  }
@@ -13865,21 +13890,8 @@ function Placeholder({ sx, ...other }) {
13865
13890
  }
13866
13891
  );
13867
13892
  }
13868
- async function blobToBase64(file) {
13869
- return new Promise((resolve, reject) => {
13870
- const reader = new FileReader();
13871
- reader.readAsDataURL(file);
13872
- reader.onloadend = function() {
13873
- const base64data = reader.result;
13874
- resolve(base64data);
13875
- };
13876
- reader.onerror = function(err) {
13877
- reject(err);
13878
- };
13879
- });
13880
- }
13881
13893
  var AppUploadFile = ({ name, ...rest }) => {
13882
- var _a, _b, _c, _d, _e;
13894
+ var _a, _b, _c;
13883
13895
  const { errors, touched, setFieldValue, values } = formik.useFormikContext();
13884
13896
  const fieldError = _19__default.default.get(errors, name);
13885
13897
  const isTouched = _19__default.default.get(touched, name);
@@ -13889,37 +13901,42 @@ var AppUploadFile = ({ name, ...rest }) => {
13889
13901
  } else if (!((_b = rest.multiple) != null ? _b : false) && _19__default.default.isArray(val)) {
13890
13902
  val = val[0];
13891
13903
  }
13892
- const value = ((_c = rest.multiple) != null ? _c : false) ? (val != null ? val : []).map((__) => (__ == null ? void 0 : __.file) ? __ == null ? void 0 : __.file : __) : val;
13904
+ ((_c = rest.multiple) != null ? _c : false) ? (val != null ? val : []).map((__) => (__ == null ? void 0 : __.file) ? __ == null ? void 0 : __.file : __) : val;
13893
13905
  return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
13894
13906
  /* @__PURE__ */ jsxRuntime.jsx(
13895
13907
  Upload,
13896
13908
  {
13897
13909
  ...rest,
13898
- file: ((_d = rest.multiple) != null ? _d : false) ? void 0 : value,
13899
- files: ((_e = rest.multiple) != null ? _e : false) ? value : void 0,
13910
+ file: rest.multiple ? void 0 : val,
13911
+ files: rest.multiple ? val : void 0,
13900
13912
  error: Boolean(fieldError) && isTouched,
13901
- disabled: (rest == null ? void 0 : rest.maxFiles) !== null && (rest == null ? void 0 : rest.maxFiles) !== void 0 && (value == null ? void 0 : value.length) >= (rest == null ? void 0 : rest.maxFiles),
13902
- onDrop: (fileVal) => {
13903
- (async () => {
13904
- var _a2;
13905
- const images = await Promise.all(
13906
- fileVal.map(async (single) => {
13907
- var _a3;
13908
- return {
13909
- preview: ((_a3 = single == null ? void 0 : single.type) == null ? void 0 : _a3.includes("image")) ? await blobToBase64(single) : void 0,
13910
- file: single
13911
- };
13912
- })
13913
- );
13914
- const currentValue = Array.isArray(value) ? value : [];
13915
- if (rest.multiple === true) {
13916
- if (currentValue.length >= ((_a2 = rest.maxFiles) != null ? _a2 : Infinity))
13917
- return;
13918
- setFieldValue(name, [...currentValue, ...images], true);
13919
- } else {
13920
- setFieldValue(name, images[0], true);
13921
- }
13922
- })();
13913
+ disabled: (rest == null ? void 0 : rest.maxFiles) !== null && (rest == null ? void 0 : rest.maxFiles) !== void 0 && (Array.isArray(val) ? val.length : val ? 1 : 0) >= (rest == null ? void 0 : rest.maxFiles),
13914
+ onDrop: async (fileVal) => {
13915
+ var _a2;
13916
+ const processFiles = await Promise.all(
13917
+ fileVal.map(async (file) => {
13918
+ const preview = await new Promise((resolve) => {
13919
+ const reader = new FileReader();
13920
+ reader.onload = () => resolve(reader.result);
13921
+ reader.readAsDataURL(file);
13922
+ });
13923
+ return {
13924
+ file,
13925
+ preview,
13926
+ name: file.name,
13927
+ size: file.size,
13928
+ type: file.type
13929
+ };
13930
+ })
13931
+ );
13932
+ if (rest.multiple) {
13933
+ const currentValue = Array.isArray(val) ? val : [];
13934
+ if (currentValue.length + processFiles.length > ((_a2 = rest.maxFiles) != null ? _a2 : Infinity))
13935
+ return;
13936
+ setFieldValue(name, [...currentValue, ...processFiles], true);
13937
+ } else if (processFiles.length > 0) {
13938
+ setFieldValue(name, processFiles[0], true);
13939
+ }
13923
13940
  },
13924
13941
  onDelete: () => {
13925
13942
  var _a2;
@@ -13929,15 +13946,22 @@ var AppUploadFile = ({ name, ...rest }) => {
13929
13946
  setFieldValue(name, "", true);
13930
13947
  }
13931
13948
  },
13932
- onRemove: (e) => {
13933
- const afterFilter = val.filter((file) => {
13934
- var _a2;
13935
- if ((_a2 = file.file) != null ? _a2 : false) {
13936
- return !_19__default.default.isEqual(e, file.file);
13937
- }
13938
- return !_19__default.default.isEqual(e, file);
13939
- });
13940
- setFieldValue(name, afterFilter, true);
13949
+ onRemove: (fileToRemove) => {
13950
+ if (rest.multiple) {
13951
+ const currentFiles = Array.isArray(val) ? val : [];
13952
+ const updatedFiles = currentFiles.filter((f) => {
13953
+ if (f.file && f.file === fileToRemove)
13954
+ return false;
13955
+ if (f === fileToRemove)
13956
+ return false;
13957
+ if (f.preview === fileToRemove)
13958
+ return false;
13959
+ return true;
13960
+ });
13961
+ setFieldValue(name, updatedFiles, true);
13962
+ } else {
13963
+ setFieldValue(name, null, true);
13964
+ }
13941
13965
  }
13942
13966
  }
13943
13967
  ),