@ahmadmubarak98/namozaj 1.1.17 → 1.2.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/main.d.ts CHANGED
@@ -89,6 +89,7 @@ export declare type DynamicFormField = TextFieldConfig | NumberFieldConfig | Sli
89
89
 
90
90
  export declare interface DynamicFormProps {
91
91
  fields: DynamicFormField[];
92
+ localization?: NamozajLocalizationOptions;
92
93
  emptyFormMessage?: string;
93
94
  defaultValues?: Record<string, FieldValueType>;
94
95
  defaultSubmitButtonProps?: DefaultSubmitButtonProps;
@@ -228,6 +229,13 @@ export declare type ModalSize = "small" | "medium" | "large" | "full";
228
229
 
229
230
  export declare const Namozaj: default_2.ForwardRefExoticComponent<DynamicFormProps & default_2.RefAttributes<any>>;
230
231
 
232
+ export declare interface NamozajLocalizationOptions {
233
+ locales: string[];
234
+ translatedLabelSuffix?: (label: string, locale: string) => string;
235
+ translatedFieldNameSuffix?: (locale: string) => string;
236
+ translatedFieldGrid?: (locale: string, fieldType: FieldType, fieldMeta: DynamicFormField['meta']) => GridColumnConfig;
237
+ }
238
+
231
239
  export declare type NumberFieldConfig = BaseFieldConfig & {
232
240
  type: "number";
233
241
  meta: BaseFieldConfig["meta"] & {
@@ -347,6 +355,7 @@ export declare type SwitchFieldConfig = BaseFieldConfig & {
347
355
  export declare type TextFieldConfig = BaseFieldConfig & {
348
356
  type: "text";
349
357
  meta: BaseFieldConfig["meta"] & {
358
+ translated?: boolean;
350
359
  variant?: FieldVariant;
351
360
  size?: FieldSize;
352
361
  maxLength?: number;
package/dist/namozaj.js CHANGED
@@ -4,7 +4,7 @@ var Zn = (e, t, a) => tm(e, typeof t != "symbol" ? t + "" : t, a);
4
4
  import { jsx, jsxs, Fragment } from "react/jsx-runtime";
5
5
  import * as React from "react";
6
6
  import React__default, { useState, useEffect, useMemo, createContext, useContext, createElement, useCallback, isValidElement, cloneElement, Children, useRef, forwardRef, useImperativeHandle, useReducer, Fragment as Fragment$1, Component as Component$1, memo, useLayoutEffect } from "react";
7
- import { Stack, Box, TextField as TextField$2, IconButton as IconButton$1, InputLabel as InputLabel$1, Autocomplete, InputAdornment as InputAdornment$1, Select as Select$1, MenuItem as MenuItem$1, Tooltip as Tooltip$1, FormControlLabel, Checkbox, FormGroup, FormControl as FormControl$1, RadioGroup, Radio, Switch, Typography as Typography$1, Avatar, Button as Button$1, Slider, Portal as Portal$1, Dialog as Dialog$1, DialogTitle as DialogTitle$1, DialogContent as DialogContent$1, DialogActions as DialogActions$1, FormHelperText as FormHelperText$1, Grid, useTheme as useTheme$3 } from "@mui/material";
7
+ import { Stack, Box, TextField as TextField$2, IconButton as IconButton$1, InputLabel as InputLabel$1, Autocomplete, InputAdornment as InputAdornment$1, Select as Select$1, MenuItem as MenuItem$1, Tooltip as Tooltip$1, FormControlLabel, Checkbox, FormGroup, FormControl as FormControl$1, RadioGroup, Radio, Switch, Typography as Typography$1, Avatar, Button as Button$1, Slider, Portal as Portal$1, Dialog as Dialog$1, DialogTitle as DialogTitle$1, DialogContent as DialogContent$1, DialogActions as DialogActions$1, FormHelperText as FormHelperText$1, Grid } from "@mui/material";
8
8
  import emStyled from "@emotion/styled";
9
9
  import { Global, ThemeContext, keyframes, css } from "@emotion/react";
10
10
  const defaultContextValue = {
@@ -18,7 +18,7 @@ const defaultContextValue = {
18
18
  }, p = () => {
19
19
  t.fields.forEach((f) => {
20
20
  var m, y, v;
21
- console.log("field", f.name), "optionsFetchFnc" in f.meta && ((m = f.meta) != null && m.optionsFetchFnc) && (!(c != null && c[f.name]) || ((y = c == null ? void 0 : c[f.name]) == null ? void 0 : y.length) === 0) && ((v = f.meta) == null || v.optionsFetchFnc().then((b) => {
21
+ "optionsFetchFnc" in f.meta && ((m = f.meta) != null && m.optionsFetchFnc) && (!(c != null && c[f.name]) || ((y = c == null ? void 0 : c[f.name]) == null ? void 0 : y.length) === 0) && ((v = f.meta) == null || v.optionsFetchFnc().then((b) => {
22
22
  u(f.name, b);
23
23
  }));
24
24
  });
@@ -4574,6 +4574,15 @@ const getDefaultValue = (e) => {
4574
4574
  default:
4575
4575
  return a;
4576
4576
  }
4577
+ }, getTranslatedFieldLabel = (e, t, a) => a ? a(e, t) : `${e} (${t})`, getTranslatedFieldName = (e, t, a) => a ? `${e}${a(t)}` : `${e}_${t}`, getTranslatedFieldGrid = (e, t, a, l) => l ? l(e, t, a) : t === "text" && (a != null && a.multiline) ? {
4578
+ xs: 12,
4579
+ sm: 12,
4580
+ md: 12,
4581
+ lg: 12,
4582
+ xl: 12
4583
+ } : {
4584
+ xs: 12,
4585
+ md: 6
4577
4586
  };
4578
4587
  function chainPropTypes(e, t) {
4579
4588
  return process.env.NODE_ENV === "production" ? () => null : function(...l) {
@@ -82434,35 +82443,45 @@ function o(e, t, a) {
82434
82443
  ModuleRegistry.registerModules([AllCommunityModule]);
82435
82444
  const Form = React__default.forwardRef(
82436
82445
  (e, t) => {
82437
- var h, f;
82446
+ var h, f, m;
82438
82447
  const a = useMemo(
82439
82448
  () => createYupSchema(e.fields),
82440
- [e, createYupSchema]
82449
+ [e, e.fields, (h = e.localization) == null ? void 0 : h.locales, createYupSchema]
82441
82450
  ), l = useForm({
82442
82451
  resolver: o(a),
82443
82452
  defaultValues: e.defaultValues ? e.defaultValues : getDefaultValues(e.fields),
82444
82453
  mode: e.validationMode || "onChange"
82445
- }), c = (m) => {
82446
- var y;
82447
- console.log(m), (y = e.onSubmit) == null || y.call(e, m);
82454
+ });
82455
+ useEffect(() => {
82456
+ const y = Object.keys(l.getValues()), v = e.fields.map((w) => w.name), b = y.filter(
82457
+ (w) => !v.includes(w)
82458
+ );
82459
+ if (b.length > 0) {
82460
+ const w = { ...l.getValues() };
82461
+ b.forEach((S) => {
82462
+ delete w[S];
82463
+ }), l.reset(w);
82464
+ }
82465
+ }, [e.fields]);
82466
+ const c = (y) => {
82467
+ var v;
82468
+ (v = e.onSubmit) == null || v.call(e, y);
82448
82469
  }, d = async () => (await l.trigger()).valueOf();
82449
82470
  useEffect(() => {
82450
- const m = l.watch(
82451
- (y, { name: v, type: b }) => {
82452
- var w;
82453
- if (b === "change" && v) {
82454
- const S = y[v];
82455
- (w = e.onChange) == null || w.call(e, {
82471
+ const y = l.watch(
82472
+ (v, { name: b, type: w }) => {
82473
+ var S;
82474
+ if (w === "change" && b) {
82475
+ const x = v[b];
82476
+ (S = e.onChange) == null || S.call(e, {
82456
82477
  ...l.getValues(),
82457
- [v]: S
82478
+ [b]: x
82458
82479
  });
82459
82480
  }
82460
82481
  }
82461
82482
  );
82462
- return () => m.unsubscribe();
82463
- }, [l.watch]), useEffect(() => {
82464
- console.log("Form State:", l.watch());
82465
- }, [l.watch()]), useImperativeHandle(
82483
+ return () => y.unsubscribe();
82484
+ }, [l.watch]), useImperativeHandle(
82466
82485
  t,
82467
82486
  () => ({
82468
82487
  submit: l.handleSubmit(c),
@@ -82472,38 +82491,65 @@ const Form = React__default.forwardRef(
82472
82491
  );
82473
82492
  const u = e.isNested ? "div" : "form", p = e.isNested ? {} : { onSubmit: l.handleSubmit(c) };
82474
82493
  return useEffect(() => {
82475
- e.error && Object.keys(e.error).forEach((y) => {
82476
- var v;
82477
- l.setError(y, {
82494
+ e.error && Object.keys(e.error).forEach((v) => {
82495
+ var b;
82496
+ l.setError(v, {
82478
82497
  type: "manual",
82479
- message: (v = e.error[y]) == null ? void 0 : v.message
82498
+ message: (b = e.error[v]) == null ? void 0 : b.message
82480
82499
  });
82481
82500
  });
82482
82501
  }, [e.error]), useEffect(() => {
82483
82502
  }, [l.formState.errors]), /* @__PURE__ */ jsx(FormProvider, { ...l, children: /* @__PURE__ */ jsxs(u, { ...p, children: [
82484
- /* @__PURE__ */ jsx(Grid, { container: !0, spacing: 2, children: (e.fields || []).map((m, y) => /* @__PURE__ */ jsx(
82503
+ /* @__PURE__ */ jsx(Grid, { container: !0, spacing: 2, children: (e.fields || []).map((y, v) => /* @__PURE__ */ jsx(
82485
82504
  FieldRenderer,
82486
82505
  {
82487
- ...m,
82506
+ ...y,
82488
82507
  control: l.control
82489
82508
  },
82490
- y
82509
+ y.name || v
82491
82510
  )) }),
82492
- !((h = e.defaultSubmitButtonProps) != null && h.hidden) && /* @__PURE__ */ jsx(Box, { mt: 2, children: /* @__PURE__ */ jsx(Button$1, { variant: "contained", color: "primary", type: "submit", children: ((f = e.defaultSubmitButtonProps) == null ? void 0 : f.label) || "Submit" }) })
82511
+ !((f = e.defaultSubmitButtonProps) != null && f.hidden) && /* @__PURE__ */ jsx(Box, { mt: 2, children: /* @__PURE__ */ jsx(Button$1, { variant: "contained", color: "primary", type: "submit", children: ((m = e.defaultSubmitButtonProps) == null ? void 0 : m.label) || "Submit" }) })
82493
82512
  ] }) });
82494
82513
  }
82495
82514
  ), Namozaj = forwardRef(
82496
82515
  (e, t) => {
82497
- var l;
82498
- const a = useTheme$3();
82499
- return !a || (l = a.palette) != null && l.primary, console.log("theme primary color", a.palette.primary.main), /* @__PURE__ */ jsx(
82516
+ const a = useMemo(() => {
82517
+ const l = [];
82518
+ return e.fields.forEach((c) => {
82519
+ var p, h;
82520
+ const d = c.meta || {};
82521
+ if (!(d.translated || !1)) {
82522
+ l.push(c);
82523
+ return;
82524
+ }
82525
+ (h = (p = e.localization) == null ? void 0 : p.locales) == null || h.forEach((f) => {
82526
+ var v, b, w;
82527
+ const m = { ...c }, y = { ...d };
82528
+ m.name = getTranslatedFieldName(
82529
+ c.name,
82530
+ f,
82531
+ (v = e.localization) == null ? void 0 : v.translatedFieldNameSuffix
82532
+ ), y.label = getTranslatedFieldLabel(
82533
+ d.label,
82534
+ f,
82535
+ (b = e.localization) == null ? void 0 : b.translatedLabelSuffix
82536
+ ), y.grid = getTranslatedFieldGrid(
82537
+ f,
82538
+ c.type,
82539
+ d,
82540
+ (w = e.localization) == null ? void 0 : w.translatedFieldGrid
82541
+ ), m.meta = y, l.push(m);
82542
+ });
82543
+ }), l;
82544
+ }, [e.fields]);
82545
+ return /* @__PURE__ */ jsx(
82500
82546
  DynamicFormLocalProvider,
82501
82547
  {
82502
82548
  value: {
82503
82549
  enableRtl: e.enableRtl,
82504
- fields: e.fields
82550
+ fields: a
82505
82551
  },
82506
- children: /* @__PURE__ */ jsx(Form, { ref: t, ...e })
82552
+ children: /* @__PURE__ */ jsx(Form, { ref: t, ...e, fields: a })
82507
82553
  }
82508
82554
  );
82509
82555
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ahmadmubarak98/namozaj",
3
3
  "private": false,
4
- "version": "1.1.17",
4
+ "version": "1.2.0",
5
5
  "type": "module",
6
6
  "main": "dist/namozaj.js",
7
7
  "types": "dist/main.d.ts",
@@ -84,7 +84,7 @@
84
84
  "url": "git+https://github.com/ahmadmubarak98/namozaj.git"
85
85
  },
86
86
  "author": "Ahmad Mubarak (ahmadmubarak98)",
87
- "license": "ISC",
87
+ "license": "MIT",
88
88
  "bugs": {
89
89
  "url": "https://github.com/ahmadmubarak98/namozaj/issues"
90
90
  },