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.mjs CHANGED
@@ -13044,50 +13044,84 @@ function MultiFilePreview({
13044
13044
  function SingleFilePreview({
13045
13045
  file
13046
13046
  }) {
13047
+ var _a;
13047
13048
  if (!file) {
13048
13049
  return null;
13049
13050
  }
13050
- const imgUrl = (typeof file === "string" ? file : file.preview) || "";
13051
- return /* @__PURE__ */ jsx(
13051
+ const imgUrl = typeof file === "string" ? file : file.preview;
13052
+ const isImage = typeof file === "string" ? /\\.(jpg|jpeg|png|gif|bmp|webp)$/i.test(file) : (_a = file.type) == null ? void 0 : _a.startsWith("image/");
13053
+ const fileName = typeof file === "string" ? file.split("/").pop() : file.name || "file";
13054
+ const fileSize = typeof file === "string" ? null : file.size ? formatFileSize(file.size) : null;
13055
+ if (isImage && imgUrl) {
13056
+ return /* @__PURE__ */ jsx(
13057
+ Box,
13058
+ {
13059
+ sx: {
13060
+ position: "relative",
13061
+ width: "100%",
13062
+ height: "100%",
13063
+ "& .wrapper": {
13064
+ width: "100%",
13065
+ height: "100%",
13066
+ position: "relative"
13067
+ }
13068
+ },
13069
+ children: /* @__PURE__ */ jsx(
13070
+ Image_default,
13071
+ {
13072
+ src: imgUrl,
13073
+ effect: "opacity",
13074
+ wrapperProps: {
13075
+ style: {
13076
+ width: "100%",
13077
+ height: "100%",
13078
+ position: "relative",
13079
+ display: "flex",
13080
+ alignItems: "center",
13081
+ justifyContent: "center",
13082
+ backgroundColor: "background.paper"
13083
+ }
13084
+ },
13085
+ style: {
13086
+ maxWidth: "100%",
13087
+ maxHeight: "100%",
13088
+ objectFit: "contain",
13089
+ borderRadius: 1
13090
+ }
13091
+ }
13092
+ )
13093
+ }
13094
+ );
13095
+ }
13096
+ return /* @__PURE__ */ jsxs(
13052
13097
  Box,
13053
13098
  {
13054
13099
  sx: {
13055
- position: "relative",
13100
+ p: 2,
13056
13101
  width: "100%",
13057
13102
  height: "100%",
13058
- "& .wrapper": {
13059
- width: "100%",
13060
- height: "100%",
13061
- position: "relative"
13062
- }
13103
+ display: "flex",
13104
+ flexDirection: "column",
13105
+ alignItems: "center",
13106
+ justifyContent: "center",
13107
+ textAlign: "center"
13063
13108
  },
13064
- children: /* @__PURE__ */ jsx(
13065
- Image_default,
13066
- {
13067
- src: imgUrl,
13068
- effect: "opacity",
13069
- wrapperProps: {
13070
- style: {
13071
- width: "100%",
13072
- height: "100%",
13073
- position: "relative"
13074
- }
13075
- },
13076
- style: {
13077
- top: 8,
13078
- left: 8,
13079
- zIndex: 8,
13080
- borderRadius: 1,
13081
- position: "absolute",
13082
- width: "calc(100% - 16px)",
13083
- height: "calc(100% - 16px)",
13084
- objectFit: "cover"
13085
- }
13086
- }
13087
- )
13109
+ children: [
13110
+ /* @__PURE__ */ jsx(Iconify_default, { icon: "eva:file-text-outline", width: 40, sx: { mb: 1 } }),
13111
+ /* @__PURE__ */ jsx(Typography, { variant: "subtitle2", noWrap: true, children: fileName }),
13112
+ fileSize && /* @__PURE__ */ jsx(Typography, { variant: "caption", color: "text.secondary", children: fileSize })
13113
+ ]
13088
13114
  }
13089
13115
  );
13090
13116
  }
13117
+ function formatFileSize(bytes) {
13118
+ if (bytes === 0)
13119
+ return "0 Bytes";
13120
+ const k = 1024;
13121
+ const sizes = ["Bytes", "KB", "MB", "GB"];
13122
+ const i = Math.floor(Math.log(bytes) / Math.log(k));
13123
+ return `${parseFloat((bytes / Math.pow(k, i)).toFixed(2))} ${sizes[i]}`;
13124
+ }
13091
13125
  function BackgroundIllustration() {
13092
13126
  const theme = useTheme$1();
13093
13127
  const PRIMARY_MAIN = theme.palette.primary.main;
@@ -13732,16 +13766,7 @@ function Upload({
13732
13766
  },
13733
13767
  children: [
13734
13768
  /* @__PURE__ */ jsx("input", { ...getInputProps() }),
13735
- /* @__PURE__ */ jsx(
13736
- Placeholder,
13737
- {
13738
- sx: {
13739
- ...hasFile && {
13740
- opacity: 0
13741
- }
13742
- }
13743
- }
13744
- ),
13769
+ hasFile ? null : /* @__PURE__ */ jsx(Placeholder, {}),
13745
13770
  hasFile && /* @__PURE__ */ jsx(SingleFilePreview, { file })
13746
13771
  ]
13747
13772
  }
@@ -13838,21 +13863,8 @@ function Placeholder({ sx, ...other }) {
13838
13863
  }
13839
13864
  );
13840
13865
  }
13841
- async function blobToBase64(file) {
13842
- return new Promise((resolve, reject) => {
13843
- const reader = new FileReader();
13844
- reader.readAsDataURL(file);
13845
- reader.onloadend = function() {
13846
- const base64data = reader.result;
13847
- resolve(base64data);
13848
- };
13849
- reader.onerror = function(err) {
13850
- reject(err);
13851
- };
13852
- });
13853
- }
13854
13866
  var AppUploadFile = ({ name, ...rest }) => {
13855
- var _a, _b, _c, _d, _e;
13867
+ var _a, _b, _c;
13856
13868
  const { errors, touched, setFieldValue, values } = useFormikContext();
13857
13869
  const fieldError = _19.get(errors, name);
13858
13870
  const isTouched = _19.get(touched, name);
@@ -13862,37 +13874,42 @@ var AppUploadFile = ({ name, ...rest }) => {
13862
13874
  } else if (!((_b = rest.multiple) != null ? _b : false) && _19.isArray(val)) {
13863
13875
  val = val[0];
13864
13876
  }
13865
- const value = ((_c = rest.multiple) != null ? _c : false) ? (val != null ? val : []).map((__) => (__ == null ? void 0 : __.file) ? __ == null ? void 0 : __.file : __) : val;
13877
+ ((_c = rest.multiple) != null ? _c : false) ? (val != null ? val : []).map((__) => (__ == null ? void 0 : __.file) ? __ == null ? void 0 : __.file : __) : val;
13866
13878
  return /* @__PURE__ */ jsxs(Fragment, { children: [
13867
13879
  /* @__PURE__ */ jsx(
13868
13880
  Upload,
13869
13881
  {
13870
13882
  ...rest,
13871
- file: ((_d = rest.multiple) != null ? _d : false) ? void 0 : value,
13872
- files: ((_e = rest.multiple) != null ? _e : false) ? value : void 0,
13883
+ file: rest.multiple ? void 0 : val,
13884
+ files: rest.multiple ? val : void 0,
13873
13885
  error: Boolean(fieldError) && isTouched,
13874
- 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),
13875
- onDrop: (fileVal) => {
13876
- (async () => {
13877
- var _a2;
13878
- const images = await Promise.all(
13879
- fileVal.map(async (single) => {
13880
- var _a3;
13881
- return {
13882
- preview: ((_a3 = single == null ? void 0 : single.type) == null ? void 0 : _a3.includes("image")) ? await blobToBase64(single) : void 0,
13883
- file: single
13884
- };
13885
- })
13886
- );
13887
- const currentValue = Array.isArray(value) ? value : [];
13888
- if (rest.multiple === true) {
13889
- if (currentValue.length >= ((_a2 = rest.maxFiles) != null ? _a2 : Infinity))
13890
- return;
13891
- setFieldValue(name, [...currentValue, ...images], true);
13892
- } else {
13893
- setFieldValue(name, images[0], true);
13894
- }
13895
- })();
13886
+ 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),
13887
+ onDrop: async (fileVal) => {
13888
+ var _a2;
13889
+ const processFiles = await Promise.all(
13890
+ fileVal.map(async (file) => {
13891
+ const preview = await new Promise((resolve) => {
13892
+ const reader = new FileReader();
13893
+ reader.onload = () => resolve(reader.result);
13894
+ reader.readAsDataURL(file);
13895
+ });
13896
+ return {
13897
+ file,
13898
+ preview,
13899
+ name: file.name,
13900
+ size: file.size,
13901
+ type: file.type
13902
+ };
13903
+ })
13904
+ );
13905
+ if (rest.multiple) {
13906
+ const currentValue = Array.isArray(val) ? val : [];
13907
+ if (currentValue.length + processFiles.length > ((_a2 = rest.maxFiles) != null ? _a2 : Infinity))
13908
+ return;
13909
+ setFieldValue(name, [...currentValue, ...processFiles], true);
13910
+ } else if (processFiles.length > 0) {
13911
+ setFieldValue(name, processFiles[0], true);
13912
+ }
13896
13913
  },
13897
13914
  onDelete: () => {
13898
13915
  var _a2;
@@ -13902,15 +13919,22 @@ var AppUploadFile = ({ name, ...rest }) => {
13902
13919
  setFieldValue(name, "", true);
13903
13920
  }
13904
13921
  },
13905
- onRemove: (e) => {
13906
- const afterFilter = val.filter((file) => {
13907
- var _a2;
13908
- if ((_a2 = file.file) != null ? _a2 : false) {
13909
- return !_19.isEqual(e, file.file);
13910
- }
13911
- return !_19.isEqual(e, file);
13912
- });
13913
- setFieldValue(name, afterFilter, true);
13922
+ onRemove: (fileToRemove) => {
13923
+ if (rest.multiple) {
13924
+ const currentFiles = Array.isArray(val) ? val : [];
13925
+ const updatedFiles = currentFiles.filter((f) => {
13926
+ if (f.file && f.file === fileToRemove)
13927
+ return false;
13928
+ if (f === fileToRemove)
13929
+ return false;
13930
+ if (f.preview === fileToRemove)
13931
+ return false;
13932
+ return true;
13933
+ });
13934
+ setFieldValue(name, updatedFiles, true);
13935
+ } else {
13936
+ setFieldValue(name, null, true);
13937
+ }
13914
13938
  }
13915
13939
  }
13916
13940
  ),