formik-form-components 0.2.15 → 0.2.16

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;
@@ -13838,21 +13872,8 @@ function Placeholder({ sx, ...other }) {
13838
13872
  }
13839
13873
  );
13840
13874
  }
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
13875
  var AppUploadFile = ({ name, ...rest }) => {
13855
- var _a, _b, _c, _d, _e;
13876
+ var _a, _b, _c;
13856
13877
  const { errors, touched, setFieldValue, values } = useFormikContext();
13857
13878
  const fieldError = _19.get(errors, name);
13858
13879
  const isTouched = _19.get(touched, name);
@@ -13862,37 +13883,42 @@ var AppUploadFile = ({ name, ...rest }) => {
13862
13883
  } else if (!((_b = rest.multiple) != null ? _b : false) && _19.isArray(val)) {
13863
13884
  val = val[0];
13864
13885
  }
13865
- const value = ((_c = rest.multiple) != null ? _c : false) ? (val != null ? val : []).map((__) => (__ == null ? void 0 : __.file) ? __ == null ? void 0 : __.file : __) : val;
13886
+ ((_c = rest.multiple) != null ? _c : false) ? (val != null ? val : []).map((__) => (__ == null ? void 0 : __.file) ? __ == null ? void 0 : __.file : __) : val;
13866
13887
  return /* @__PURE__ */ jsxs(Fragment, { children: [
13867
13888
  /* @__PURE__ */ jsx(
13868
13889
  Upload,
13869
13890
  {
13870
13891
  ...rest,
13871
- file: ((_d = rest.multiple) != null ? _d : false) ? void 0 : value,
13872
- files: ((_e = rest.multiple) != null ? _e : false) ? value : void 0,
13892
+ file: rest.multiple ? void 0 : val,
13893
+ files: rest.multiple ? val : void 0,
13873
13894
  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
- })();
13895
+ 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),
13896
+ onDrop: async (fileVal) => {
13897
+ var _a2;
13898
+ const processFiles = await Promise.all(
13899
+ fileVal.map(async (file) => {
13900
+ const preview = await new Promise((resolve) => {
13901
+ const reader = new FileReader();
13902
+ reader.onload = () => resolve(reader.result);
13903
+ reader.readAsDataURL(file);
13904
+ });
13905
+ return {
13906
+ file,
13907
+ preview,
13908
+ name: file.name,
13909
+ size: file.size,
13910
+ type: file.type
13911
+ };
13912
+ })
13913
+ );
13914
+ if (rest.multiple) {
13915
+ const currentValue = Array.isArray(val) ? val : [];
13916
+ if (currentValue.length + processFiles.length > ((_a2 = rest.maxFiles) != null ? _a2 : Infinity))
13917
+ return;
13918
+ setFieldValue(name, [...currentValue, ...processFiles], true);
13919
+ } else if (processFiles.length > 0) {
13920
+ setFieldValue(name, processFiles[0], true);
13921
+ }
13896
13922
  },
13897
13923
  onDelete: () => {
13898
13924
  var _a2;