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.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;
@@ -13865,21 +13899,8 @@ function Placeholder({ sx, ...other }) {
13865
13899
  }
13866
13900
  );
13867
13901
  }
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
13902
  var AppUploadFile = ({ name, ...rest }) => {
13882
- var _a, _b, _c, _d, _e;
13903
+ var _a, _b, _c;
13883
13904
  const { errors, touched, setFieldValue, values } = formik.useFormikContext();
13884
13905
  const fieldError = _19__default.default.get(errors, name);
13885
13906
  const isTouched = _19__default.default.get(touched, name);
@@ -13889,37 +13910,42 @@ var AppUploadFile = ({ name, ...rest }) => {
13889
13910
  } else if (!((_b = rest.multiple) != null ? _b : false) && _19__default.default.isArray(val)) {
13890
13911
  val = val[0];
13891
13912
  }
13892
- const value = ((_c = rest.multiple) != null ? _c : false) ? (val != null ? val : []).map((__) => (__ == null ? void 0 : __.file) ? __ == null ? void 0 : __.file : __) : val;
13913
+ ((_c = rest.multiple) != null ? _c : false) ? (val != null ? val : []).map((__) => (__ == null ? void 0 : __.file) ? __ == null ? void 0 : __.file : __) : val;
13893
13914
  return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
13894
13915
  /* @__PURE__ */ jsxRuntime.jsx(
13895
13916
  Upload,
13896
13917
  {
13897
13918
  ...rest,
13898
- file: ((_d = rest.multiple) != null ? _d : false) ? void 0 : value,
13899
- files: ((_e = rest.multiple) != null ? _e : false) ? value : void 0,
13919
+ file: rest.multiple ? void 0 : val,
13920
+ files: rest.multiple ? val : void 0,
13900
13921
  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
- })();
13922
+ 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),
13923
+ onDrop: async (fileVal) => {
13924
+ var _a2;
13925
+ const processFiles = await Promise.all(
13926
+ fileVal.map(async (file) => {
13927
+ const preview = await new Promise((resolve) => {
13928
+ const reader = new FileReader();
13929
+ reader.onload = () => resolve(reader.result);
13930
+ reader.readAsDataURL(file);
13931
+ });
13932
+ return {
13933
+ file,
13934
+ preview,
13935
+ name: file.name,
13936
+ size: file.size,
13937
+ type: file.type
13938
+ };
13939
+ })
13940
+ );
13941
+ if (rest.multiple) {
13942
+ const currentValue = Array.isArray(val) ? val : [];
13943
+ if (currentValue.length + processFiles.length > ((_a2 = rest.maxFiles) != null ? _a2 : Infinity))
13944
+ return;
13945
+ setFieldValue(name, [...currentValue, ...processFiles], true);
13946
+ } else if (processFiles.length > 0) {
13947
+ setFieldValue(name, processFiles[0], true);
13948
+ }
13923
13949
  },
13924
13950
  onDelete: () => {
13925
13951
  var _a2;