@mage-ui/components 1.0.102 → 1.0.104
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/components/forms/Form.d.mts +1 -0
- package/dist/components/forms/controls/FormAutocomplete.d.mts +1 -0
- package/dist/components/forms/controls/FormAutocomplete.d.mts.map +1 -1
- package/dist/components/forms/controls/FormAutocomplete.mjs +1 -1
- package/dist/components/forms/controls/FormAutocomplete.mjs.map +1 -1
- package/dist/components/forms/controls/register-components.mjs +1 -1
- package/dist/components/forms/controls/register-components.mjs.map +1 -1
- package/dist/components/forms/rules/extract-form-rules.mjs +1 -1
- package/dist/components/forms/rules/extract-form-rules.mjs.map +1 -1
- package/package.json +2 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FormAutocomplete.d.mts","names":[],"sources":["../../../../src/components/forms/controls/FormAutocomplete.tsx"],"mappings":";;;KAQY,qBAAA,GAAwB,IAAA,CAClC,iBAAA;EAGA,KAAA;EACA,KAAA;EACA,YAAA;EACA,YAAA,GAAe,aAAA;AAAA"}
|
|
1
|
+
{"version":3,"file":"FormAutocomplete.d.mts","names":[],"sources":["../../../../src/components/forms/controls/FormAutocomplete.tsx"],"mappings":";;;KAQY,qBAAA,GAAwB,IAAA,CAClC,iBAAA;EAGA,KAAA;EACA,KAAA;EACA,YAAA;EACA,YAAA,GAAe,aAAA;EACf,QAAA,IAAY,KAAA,EAAO,aAAA;AAAA"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{Autocomplete as e}from"../../controls/dropdown/autocomplete/Autocomplete.mjs";import{jsx as t}from"react/jsx-runtime";import{Controller as n}from"react-hook-form";const r=({$rule:r,isSubmitting:i,name:a,error:o,defaultValue:s,...
|
|
1
|
+
import{Autocomplete as e}from"../../controls/dropdown/autocomplete/Autocomplete.mjs";import{jsx as t}from"react/jsx-runtime";import{Controller as n}from"react-hook-form";const r=({$rule:r,isSubmitting:i,name:a,error:o,defaultValue:s,onChange:c,...l})=>t(n,{name:a,...s===void 0?{}:{defaultValue:s},render:({field:{onChange:n,value:r}})=>t(e,{name:a,error:o,value:r,...l,onChange:e=>{n(e),c?.(e)}})});r.displayName=`Form.Autocomplete`;export{r as FormAutocomplete};
|
|
2
2
|
//# sourceMappingURL=FormAutocomplete.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FormAutocomplete.mjs","names":[],"sources":["../../../../src/components/forms/controls/FormAutocomplete.tsx"],"sourcesContent":["import { Controller } from 'react-hook-form';\n\nimport {\n Autocomplete,\n type AutocompleteProps,\n} from '@/components/controls/dropdown/autocomplete/Autocomplete';\nimport type { DropdownValue } from '@/components/controls/dropdown/DropdownContextProvider';\n\nexport type FormAutocompleteProps = Omit<\n AutocompleteProps,\n 'onChange' | 'defaultValue' | 'value'\n> & {\n $rule?: string;\n error?: string;\n isSubmitting?: boolean;\n defaultValue?: DropdownValue;\n};\n\nexport const FormAutocomplete = ({\n $rule,\n isSubmitting: _isSubmitting,\n name,\n error,\n defaultValue,\n ...props\n}: FormAutocompleteProps) => {\n const controllerProps = defaultValue === undefined ? {} : { defaultValue };\n\n return (\n <Controller\n name={name}\n {...controllerProps}\n render={({ field: { onChange, value } }) => (\n <Autocomplete\n name={name}\n error={error}\n value={value}\n {...props}\n onChange={onChange}\n />\n )}\n />\n );\n};\n\nFormAutocomplete.displayName = 'Form.Autocomplete';\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"FormAutocomplete.mjs","names":[],"sources":["../../../../src/components/forms/controls/FormAutocomplete.tsx"],"sourcesContent":["import { Controller } from 'react-hook-form';\n\nimport {\n Autocomplete,\n type AutocompleteProps,\n} from '@/components/controls/dropdown/autocomplete/Autocomplete';\nimport type { DropdownValue } from '@/components/controls/dropdown/DropdownContextProvider';\n\nexport type FormAutocompleteProps = Omit<\n AutocompleteProps,\n 'onChange' | 'defaultValue' | 'value'\n> & {\n $rule?: string;\n error?: string;\n isSubmitting?: boolean;\n defaultValue?: DropdownValue;\n onChange?: (value: DropdownValue) => void;\n};\n\nexport const FormAutocomplete = ({\n $rule,\n isSubmitting: _isSubmitting,\n name,\n error,\n defaultValue,\n onChange,\n ...props\n}: FormAutocompleteProps) => {\n const controllerProps = defaultValue === undefined ? {} : { defaultValue };\n\n return (\n <Controller\n name={name}\n {...controllerProps}\n render={({ field: { onChange: fieldOnChange, value } }) => (\n <Autocomplete\n name={name}\n error={error}\n value={value}\n {...props}\n onChange={(val) => {\n fieldOnChange(val);\n onChange?.(val);\n }}\n />\n )}\n />\n );\n};\n\nFormAutocomplete.displayName = 'Form.Autocomplete';\n"],"mappings":"0KAmBA,MAAa,GAAoB,CAC/B,QACA,aAAc,EACd,OACA,QACA,eACA,WACA,GAAG,KAKD,EAAC,EAAD,CACQ,OACN,GALoB,IAAiB,IAAA,GAAY,EAAE,CAAG,CAAE,eAAc,CAMtE,QAAS,CAAE,MAAO,CAAE,SAAU,EAAe,YAC3C,EAAC,EAAD,CACQ,OACC,QACA,QACP,GAAI,EACJ,SAAW,GAAQ,CACjB,EAAc,EAAI,CAClB,IAAW,EAAI,EAEjB,CAAA,CAEJ,CAAA,CAIN,EAAiB,YAAc"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use client";import{Fragment as e,jsx as t}from"react/jsx-runtime";import{Children as n,cloneElement as r,isValidElement as i}from"react";const a=(e,t,n)=>{if(e)try{let r=JSON.parse(e),i={...r.bindings,attribute:t},a=n?n(r.key,i,r.fallback):r.fallback;return String(a).replace(/{attribute}/g,t)}catch{return e.replace(/{attribute}/g,t)}},
|
|
1
|
+
"use client";import{Fragment as e,jsx as t}from"react/jsx-runtime";import{Children as n,cloneElement as r,isValidElement as i}from"react";const a=new Set([`Form.Password`,`Form.Checkbox`,`Form.Select`,`Form.Combobox`,`Form.Autocomplete`]),o=(e,t,n)=>{if(e)try{let r=JSON.parse(e),i={...r.bindings,attribute:t},a=n?n(r.key,i,r.fallback):r.fallback;return String(a).replace(/{attribute}/g,t)}catch{return e.replace(/{attribute}/g,t)}},s=({children:s,register:c,componentTypes:l,errors:u={},t:d})=>{let f=e=>n.map(e,e=>{if(!i(e))return e;let t=e.props;if(l.some(t=>e.type===t)){let n=t.name,i=u[n??``]?.message?.toString(),s=n?o(i,n,d):void 0,l=e.type.displayName;return r(e,{...n!==void 0&&!a.has(l??``)&&n?c(n):{},error:s})}return t.children?r(e,{children:f(t.children)}):e});return t(e,{children:f(s)})};export{s as RegisteredFormChildren};
|
|
2
2
|
//# sourceMappingURL=register-components.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"register-components.mjs","names":[],"sources":["../../../../src/components/forms/controls/register-components.tsx"],"sourcesContent":["'use client';\n\nimport type React from 'react';\nimport { Children, cloneElement, isValidElement } from 'react';\nimport type { FieldErrors, UseFormRegister } from 'react-hook-form';\n\ninterface TranslationMessage {\n key: string;\n bindings: Record<string, string>;\n fallback: string;\n}\n\nconst parseAndTranslate = (\n message: string | undefined,\n fieldName: string,\n t?: (\n key: string,\n bindings?: Record<string, string>,\n fallback?: string,\n ) => string | React.ReactNode,\n): string | undefined => {\n if (!message) return undefined;\n\n try {\n const parsed: TranslationMessage = JSON.parse(message);\n const bindings = { ...parsed.bindings, attribute: fieldName };\n const result = t\n ? t(parsed.key, bindings, parsed.fallback)\n : parsed.fallback;\n return String(result).replace(/{attribute}/g, fieldName);\n } catch {\n return message.replace(/{attribute}/g, fieldName);\n }\n};\n\ninterface RegisteredFormChildrenProps {\n children: React.ReactNode;\n register: UseFormRegister<Record<string, unknown>>;\n componentTypes: React.ComponentType[];\n errors?: FieldErrors<Record<string, unknown>>;\n t?: (\n key: string,\n bindings?: Record<string, string>,\n fallback?: string,\n ) => string | React.ReactNode;\n}\n\nexport const RegisteredFormChildren = ({\n children,\n register,\n componentTypes,\n errors = {},\n t,\n}: RegisteredFormChildrenProps): React.ReactNode => {\n const processChildren = (nodes: React.ReactNode): React.ReactNode => {\n return Children.map(nodes, (child) => {\n if (!isValidElement(child)) {\n return child;\n }\n\n const childProps = child.props as {\n name?: string;\n children?: React.ReactNode;\n };\n\n if (\n componentTypes.some((type: React.ComponentType) => child.type === type)\n ) {\n const
|
|
1
|
+
{"version":3,"file":"register-components.mjs","names":[],"sources":["../../../../src/components/forms/controls/register-components.tsx"],"sourcesContent":["'use client';\n\nimport type React from 'react';\nimport { Children, cloneElement, isValidElement } from 'react';\nimport type { FieldErrors, UseFormRegister } from 'react-hook-form';\n\ninterface TranslationMessage {\n key: string;\n bindings: Record<string, string>;\n fallback: string;\n}\n\nconst controllerDisplayNames = new Set([\n 'Form.Password',\n 'Form.Checkbox',\n 'Form.Select',\n 'Form.Combobox',\n 'Form.Autocomplete',\n]);\n\nconst parseAndTranslate = (\n message: string | undefined,\n fieldName: string,\n t?: (\n key: string,\n bindings?: Record<string, string>,\n fallback?: string,\n ) => string | React.ReactNode,\n): string | undefined => {\n if (!message) return undefined;\n\n try {\n const parsed: TranslationMessage = JSON.parse(message);\n const bindings = { ...parsed.bindings, attribute: fieldName };\n const result = t\n ? t(parsed.key, bindings, parsed.fallback)\n : parsed.fallback;\n return String(result).replace(/{attribute}/g, fieldName);\n } catch {\n return message.replace(/{attribute}/g, fieldName);\n }\n};\n\ninterface RegisteredFormChildrenProps {\n children: React.ReactNode;\n register: UseFormRegister<Record<string, unknown>>;\n componentTypes: React.ComponentType[];\n errors?: FieldErrors<Record<string, unknown>>;\n t?: (\n key: string,\n bindings?: Record<string, string>,\n fallback?: string,\n ) => string | React.ReactNode;\n}\n\nexport const RegisteredFormChildren = ({\n children,\n register,\n componentTypes,\n errors = {},\n t,\n}: RegisteredFormChildrenProps): React.ReactNode => {\n const processChildren = (nodes: React.ReactNode): React.ReactNode => {\n return Children.map(nodes, (child) => {\n if (!isValidElement(child)) {\n return child;\n }\n\n const childProps = child.props as {\n name?: string;\n children?: React.ReactNode;\n };\n\n if (\n componentTypes.some((type: React.ComponentType) => child.type === type)\n ) {\n const fieldName = childProps.name;\n const errorMessage = errors[fieldName ?? '']?.message?.toString();\n const translatedError = fieldName\n ? parseAndTranslate(errorMessage, fieldName, t)\n : undefined;\n const displayName = (\n child.type as React.ComponentType & { displayName?: string }\n ).displayName;\n const shouldRegister =\n fieldName !== undefined &&\n !controllerDisplayNames.has(displayName ?? '');\n const registrationProps =\n shouldRegister && fieldName ? register(fieldName) : {};\n\n return cloneElement(child, {\n ...registrationProps,\n error: translatedError,\n } as Partial<typeof child.props>);\n }\n\n if (childProps.children) {\n return cloneElement(child, {\n children: processChildren(childProps.children),\n } as Partial<typeof child.props>);\n }\n\n return child;\n });\n };\n\n return <>{processChildren(children)}</>;\n};\n"],"mappings":"0IAYA,MAAM,EAAyB,IAAI,IAAI,CACrC,gBACA,gBACA,cACA,gBACA,oBACD,CAAC,CAEI,GACJ,EACA,EACA,IAKuB,CAClB,KAEL,GAAI,CACF,IAAM,EAA6B,KAAK,MAAM,EAAQ,CAChD,EAAW,CAAE,GAAG,EAAO,SAAU,UAAW,EAAW,CACvD,EAAS,EACX,EAAE,EAAO,IAAK,EAAU,EAAO,SAAS,CACxC,EAAO,SACX,OAAO,OAAO,EAAO,CAAC,QAAQ,eAAgB,EAAU,MAClD,CACN,OAAO,EAAQ,QAAQ,eAAgB,EAAU,GAgBxC,GAA0B,CACrC,WACA,WACA,iBACA,SAAS,EAAE,CACX,OACkD,CAClD,IAAM,EAAmB,GAChB,EAAS,IAAI,EAAQ,GAAU,CACpC,GAAI,CAAC,EAAe,EAAM,CACxB,OAAO,EAGT,IAAM,EAAa,EAAM,MAKzB,GACE,EAAe,KAAM,GAA8B,EAAM,OAAS,EAAK,CACvE,CACA,IAAM,EAAY,EAAW,KACvB,EAAe,EAAO,GAAa,KAAK,SAAS,UAAU,CAC3D,EAAkB,EACpB,EAAkB,EAAc,EAAW,EAAE,CAC7C,IAAA,GACE,EACJ,EAAM,KACN,YAOF,OAAO,EAAa,EAAO,CACzB,GANA,IAAc,IAAA,IACd,CAAC,EAAuB,IAAI,GAAe,GAAG,EAE5B,EAAY,EAAS,EAAU,CAAG,EAAE,CAItD,MAAO,EACR,CAAgC,CASnC,OANI,EAAW,SACN,EAAa,EAAO,CACzB,SAAU,EAAgB,EAAW,SAAS,CAC/C,CAAgC,CAG5B,GACP,CAGJ,OAAO,EAAA,EAAA,CAAA,SAAG,EAAgB,EAAS,CAAI,CAAA"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{findFormComponents as e}from"../controls/find-form-components.mjs";import{PASSWORD_RULES as t}from"./password-rules.mjs";import n from"./password.mjs";import{parseRules as r}from"./zod.mjs";import{z as i}from"zod";const a=(e,t)=>{if(!e.name||!e.$rule?.includes(`confirmed`))return;let n=e.$rule.split(`|`).find(e=>e.startsWith(`confirmed`)),r=n?.includes(`:`)?n.split(`:`)[1]:`${e.name}_confirmation`;t.confirmedFields.push({field:e.name,target:r})},o=(e,t)=>{e.name&&(e.$rule?.includes(`required`)?(t.rules[e.name]=i.boolean().optional().or(i.literal(!1)),t.requiredBooleanFields.push(e.name)):t.rules[e.name]=i.boolean().optional())},s=(e,a)=>{if(!e.name)return;let o=i.string(),s=e.$rule?e.$rule.split(`|`).filter(e=>e.trim()!==`password`).join(`|`):``;s&&(o=r(s));let c=e.rules||(e.$rule?.includes(`password`)?t:[]);c.length>0&&(o=n(o,c));let l=!e.$rule||e.$rule.includes(`optional`)||s.includes(`optional`);a.rules[e.name]=l?o.optional():o},c=(e,t)=>{e.name&&(t.rules[e.name]=e.$rule?r(e.$rule):i.string().optional())},l=(e,t)=>{e.name&&(t.rules[e.name]=i.any().optional())},u=(e,t)=>t.confirmedFields.length===0&&t.requiredBooleanFields.length===0?e:e.superRefine((e,n)=>{t.confirmedFields.forEach(({field:t,target:r})=>{e[t]!==e[r]&&n.addIssue({code:i.ZodIssueCode.custom,message:JSON.stringify({key:`confirmed`,fallback:`La confirmación no coincide.`,bindings:{}}),path:[r]})}),t.requiredBooleanFields.forEach(t=>{e[t]!==!0&&n.addIssue({code:i.ZodIssueCode.custom,message:JSON.stringify({key:`required`,fallback:`El campo {attribute} es obligatorio.`,bindings:{}}),path:[t]})})}),d=(t,n)=>{let r={rules:{},confirmedFields:[],requiredBooleanFields:[]};return e(t,n).forEach(e=>{let t=e.props,n=e.type.displayName;t.name&&(a(t,r),n===`Form.Checkbox`?o(t,r):n===`Form.Password`?s(t,r):n===`Form.Select`||n===`Form.Combobox`||n===`Form.Autocomplete`?l(t,r):c(t,r))}),u(i.object(r.rules),r)};export{d as extractFormRules};
|
|
1
|
+
import{findFormComponents as e}from"../controls/find-form-components.mjs";import{PASSWORD_RULES as t}from"./password-rules.mjs";import n from"./password.mjs";import{parseRules as r}from"./zod.mjs";import{z as i}from"zod";const a=(e,t)=>{if(!e.name||!e.$rule?.includes(`confirmed`))return;let n=e.$rule.split(`|`).find(e=>e.startsWith(`confirmed`)),r=n?.includes(`:`)?n.split(`:`)[1]:`${e.name}_confirmation`;t.confirmedFields.push({field:e.name,target:r})},o=(e,t)=>{e.name&&(e.$rule?.includes(`required`)?(t.rules[e.name]=i.boolean().optional().or(i.literal(!1)),t.requiredBooleanFields.push(e.name)):t.rules[e.name]=i.boolean().optional())},s=(e,a)=>{if(!e.name)return;let o=i.string(),s=e.$rule?e.$rule.split(`|`).filter(e=>e.trim()!==`password`).join(`|`):``;s&&(o=r(s));let c=e.rules||(e.$rule?.includes(`password`)?t:[]);c.length>0&&(o=n(o,c));let l=!e.$rule||e.$rule.includes(`optional`)||s.includes(`optional`);a.rules[e.name]=l?o.optional():o},c=(e,t)=>{e.name&&(t.rules[e.name]=e.$rule?r(e.$rule):i.string().optional())},l=(e,t)=>{e.name&&(t.rules[e.name]=i.any().optional())},u=(e,t)=>t.confirmedFields.length===0&&t.requiredBooleanFields.length===0?e:e.superRefine((e,n)=>{t.confirmedFields.forEach(({field:t,target:r})=>{e[t]!==e[r]&&n.addIssue({code:i.ZodIssueCode.custom,message:JSON.stringify({key:`confirmed`,fallback:`La confirmación no coincide.`,bindings:{}}),path:[r]})}),t.requiredBooleanFields.forEach(t=>{e[t]!==!0&&n.addIssue({code:i.ZodIssueCode.custom,message:JSON.stringify({key:`required`,fallback:`El campo {attribute} es obligatorio.`,bindings:{}}),path:[t]})})}),d=(t,n)=>{let r={rules:{},confirmedFields:[],requiredBooleanFields:[]};return e(t,n).forEach(e=>{let t=e.props,n=e.type.displayName;t.name&&(a(t,r),n===`Form.Checkbox`?o(t,r):n===`Form.Password`?s(t,r):n===`Form.Select`||n===`Form.Combobox`||n===`Form.Autocomplete`?l(t,r):c(t,r))}),u(i.object(r.rules).loose(),r)};export{d as extractFormRules};
|
|
2
2
|
//# sourceMappingURL=extract-form-rules.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"extract-form-rules.mjs","names":["passwordRule"],"sources":["../../../../src/components/forms/rules/extract-form-rules.ts"],"sourcesContent":["import type React from 'react';\nimport type { FieldValues } from 'react-hook-form';\nimport { type ZodTypeAny, z } from 'zod';\n\nimport type { PasswordRuleDefinition } from '@/components/controls/password-input/usePasswordRules';\n\nimport { findFormComponents } from '../controls/find-form-components';\nimport passwordRule from './password';\nimport { PASSWORD_RULES } from './password-rules';\nimport { parseRules } from './zod';\n\ntype FormRuleContext = {\n rules: Record<string, ZodTypeAny>;\n confirmedFields: { field: string; target: string }[];\n requiredBooleanFields: string[];\n};\n\ntype ComponentProps = {\n name?: string;\n $rule?: string;\n rules?: PasswordRuleDefinition[];\n};\n\nconst processConfirmedRule = (\n props: ComponentProps,\n context: FormRuleContext,\n) => {\n if (!props.name || !props.$rule?.includes('confirmed')) return;\n\n const rulesList = props.$rule.split('|');\n const confirmedRule = rulesList.find((r) => r.startsWith('confirmed'));\n const target = confirmedRule?.includes(':')\n ? confirmedRule.split(':')[1]\n : `${props.name}_confirmation`;\n\n context.confirmedFields.push({ field: props.name, target });\n};\n\nconst processCheckbox = (props: ComponentProps, context: FormRuleContext) => {\n if (!props.name) return;\n\n if (props.$rule?.includes('required')) {\n context.rules[props.name] = z.boolean().optional().or(z.literal(false));\n context.requiredBooleanFields.push(props.name);\n } else {\n context.rules[props.name] = z.boolean().optional();\n }\n};\n\nconst processPassword = (props: ComponentProps, context: FormRuleContext) => {\n if (!props.name) return;\n\n let baseSchema: ZodTypeAny = z.string();\n const otherRules = props.$rule\n ? props.$rule\n .split('|')\n .filter((r) => r.trim() !== 'password')\n .join('|')\n : '';\n\n if (otherRules) {\n baseSchema = parseRules(otherRules);\n }\n\n const passwordRules =\n props.rules || (props.$rule?.includes('password') ? PASSWORD_RULES : []);\n\n if (passwordRules.length > 0) {\n baseSchema = passwordRule(baseSchema, passwordRules);\n }\n\n const isOptional =\n !props.$rule ||\n props.$rule.includes('optional') ||\n otherRules.includes('optional');\n\n context.rules[props.name] = isOptional ? baseSchema.optional() : baseSchema;\n};\n\nconst processDefault = (props: ComponentProps, context: FormRuleContext) => {\n if (!props.name) return;\n context.rules[props.name] = props.$rule\n ? parseRules(props.$rule)\n : z.string().optional();\n};\n\nconst processDropdown = (props: ComponentProps, context: FormRuleContext) => {\n if (!props.name) return;\n context.rules[props.name] = z.any().optional();\n};\n\nconst applyRefinements = (\n schema: z.ZodObject<FieldValues>,\n context: FormRuleContext,\n) => {\n if (\n context.confirmedFields.length === 0 &&\n context.requiredBooleanFields.length === 0\n ) {\n return schema;\n }\n\n return schema.superRefine((data, ctx) => {\n context.confirmedFields.forEach(({ field, target }) => {\n if (data[field] !== data[target]) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message: JSON.stringify({\n key: 'confirmed',\n fallback: 'La confirmación no coincide.',\n bindings: {},\n }),\n path: [target],\n });\n }\n });\n\n context.requiredBooleanFields.forEach((field) => {\n if (data[field] !== true) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message: JSON.stringify({\n key: 'required',\n fallback: 'El campo {attribute} es obligatorio.',\n bindings: {},\n }),\n path: [field],\n });\n }\n });\n });\n};\n\nexport const extractFormRules = (\n children: React.ReactNode,\n componentTypes: React.ComponentType[],\n): ZodTypeAny => {\n const context: FormRuleContext = {\n rules: {},\n confirmedFields: [],\n requiredBooleanFields: [],\n };\n\n const components = findFormComponents(children, componentTypes);\n\n components.forEach((component) => {\n const props = component.props as ComponentProps;\n const displayName = (component.type as { displayName?: string })\n .displayName;\n\n if (props.name) {\n processConfirmedRule(props, context);\n\n if (displayName === 'Form.Checkbox') {\n processCheckbox(props, context);\n } else if (displayName === 'Form.Password') {\n processPassword(props, context);\n } else if (\n displayName === 'Form.Select' ||\n displayName === 'Form.Combobox' ||\n displayName === 'Form.Autocomplete'\n ) {\n processDropdown(props, context);\n } else {\n processDefault(props, context);\n }\n }\n });\n\n return applyRefinements(z.object(context.rules), context);\n};\n"],"mappings":"6NAuBA,MAAM,GACJ,EACA,IACG,CACH,GAAI,CAAC,EAAM,MAAQ,CAAC,EAAM,OAAO,SAAS,YAAY,CAAE,OAGxD,IAAM,EADY,EAAM,MAAM,MAAM,IAAI,CACR,KAAM,GAAM,EAAE,WAAW,YAAY,CAAC,CAChE,EAAS,GAAe,SAAS,IAAI,CACvC,EAAc,MAAM,IAAI,CAAC,GACzB,GAAG,EAAM,KAAK,eAElB,EAAQ,gBAAgB,KAAK,CAAE,MAAO,EAAM,KAAM,SAAQ,CAAC,EAGvD,GAAmB,EAAuB,IAA6B,CACtE,EAAM,OAEP,EAAM,OAAO,SAAS,WAAW,EACnC,EAAQ,MAAM,EAAM,MAAQ,EAAE,SAAS,CAAC,UAAU,CAAC,GAAG,EAAE,QAAQ,GAAM,CAAC,CACvE,EAAQ,sBAAsB,KAAK,EAAM,KAAK,EAE9C,EAAQ,MAAM,EAAM,MAAQ,EAAE,SAAS,CAAC,UAAU,GAIhD,GAAmB,EAAuB,IAA6B,CAC3E,GAAI,CAAC,EAAM,KAAM,OAEjB,IAAI,EAAyB,EAAE,QAAQ,CACjC,EAAa,EAAM,MACrB,EAAM,MACH,MAAM,IAAI,CACV,OAAQ,GAAM,EAAE,MAAM,GAAK,WAAW,CACtC,KAAK,IAAI,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QAAS,GAAc,CAChC,IAAM,EAAQ,EAAU,MAClB,EAAe,EAAU,KAC5B,YAEC,EAAM,OACR,EAAqB,EAAO,EAAQ,CAEhC,IAAgB,gBAClB,EAAgB,EAAO,EAAQ,CACtB,IAAgB,gBACzB,EAAgB,EAAO,EAAQ,CAE/B,IAAgB,eAChB,IAAgB,iBAChB,IAAgB,oBAEhB,EAAgB,EAAO,EAAQ,CAE/B,EAAe,EAAO,EAAQ,GAGlC,CAEK,EAAiB,EAAE,OAAO,EAAQ,MAAM,CAAE,EAAQ"}
|
|
1
|
+
{"version":3,"file":"extract-form-rules.mjs","names":["passwordRule"],"sources":["../../../../src/components/forms/rules/extract-form-rules.ts"],"sourcesContent":["import type React from 'react';\nimport type { FieldValues } from 'react-hook-form';\nimport { type ZodTypeAny, z } from 'zod';\n\nimport type { PasswordRuleDefinition } from '@/components/controls/password-input/usePasswordRules';\n\nimport { findFormComponents } from '../controls/find-form-components';\nimport passwordRule from './password';\nimport { PASSWORD_RULES } from './password-rules';\nimport { parseRules } from './zod';\n\ntype FormRuleContext = {\n rules: Record<string, ZodTypeAny>;\n confirmedFields: { field: string; target: string }[];\n requiredBooleanFields: string[];\n};\n\ntype ComponentProps = {\n name?: string;\n $rule?: string;\n rules?: PasswordRuleDefinition[];\n};\n\nconst processConfirmedRule = (\n props: ComponentProps,\n context: FormRuleContext,\n) => {\n if (!props.name || !props.$rule?.includes('confirmed')) return;\n\n const rulesList = props.$rule.split('|');\n const confirmedRule = rulesList.find((r) => r.startsWith('confirmed'));\n const target = confirmedRule?.includes(':')\n ? confirmedRule.split(':')[1]\n : `${props.name}_confirmation`;\n\n context.confirmedFields.push({ field: props.name, target });\n};\n\nconst processCheckbox = (props: ComponentProps, context: FormRuleContext) => {\n if (!props.name) return;\n\n if (props.$rule?.includes('required')) {\n context.rules[props.name] = z.boolean().optional().or(z.literal(false));\n context.requiredBooleanFields.push(props.name);\n } else {\n context.rules[props.name] = z.boolean().optional();\n }\n};\n\nconst processPassword = (props: ComponentProps, context: FormRuleContext) => {\n if (!props.name) return;\n\n let baseSchema: ZodTypeAny = z.string();\n const otherRules = props.$rule\n ? props.$rule\n .split('|')\n .filter((r) => r.trim() !== 'password')\n .join('|')\n : '';\n\n if (otherRules) {\n baseSchema = parseRules(otherRules);\n }\n\n const passwordRules =\n props.rules || (props.$rule?.includes('password') ? PASSWORD_RULES : []);\n\n if (passwordRules.length > 0) {\n baseSchema = passwordRule(baseSchema, passwordRules);\n }\n\n const isOptional =\n !props.$rule ||\n props.$rule.includes('optional') ||\n otherRules.includes('optional');\n\n context.rules[props.name] = isOptional ? baseSchema.optional() : baseSchema;\n};\n\nconst processDefault = (props: ComponentProps, context: FormRuleContext) => {\n if (!props.name) return;\n context.rules[props.name] = props.$rule\n ? parseRules(props.$rule)\n : z.string().optional();\n};\n\nconst processDropdown = (props: ComponentProps, context: FormRuleContext) => {\n if (!props.name) return;\n context.rules[props.name] = z.any().optional();\n};\n\nconst applyRefinements = (\n schema: z.ZodObject<FieldValues>,\n context: FormRuleContext,\n) => {\n if (\n context.confirmedFields.length === 0 &&\n context.requiredBooleanFields.length === 0\n ) {\n return schema;\n }\n\n return schema.superRefine((data, ctx) => {\n context.confirmedFields.forEach(({ field, target }) => {\n if (data[field] !== data[target]) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message: JSON.stringify({\n key: 'confirmed',\n fallback: 'La confirmación no coincide.',\n bindings: {},\n }),\n path: [target],\n });\n }\n });\n\n context.requiredBooleanFields.forEach((field) => {\n if (data[field] !== true) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message: JSON.stringify({\n key: 'required',\n fallback: 'El campo {attribute} es obligatorio.',\n bindings: {},\n }),\n path: [field],\n });\n }\n });\n });\n};\n\nexport const extractFormRules = (\n children: React.ReactNode,\n componentTypes: React.ComponentType[],\n): ZodTypeAny => {\n const context: FormRuleContext = {\n rules: {},\n confirmedFields: [],\n requiredBooleanFields: [],\n };\n\n const components = findFormComponents(children, componentTypes);\n\n components.forEach((component) => {\n const props = component.props as ComponentProps;\n const displayName = (component.type as { displayName?: string })\n .displayName;\n\n if (props.name) {\n processConfirmedRule(props, context);\n\n if (displayName === 'Form.Checkbox') {\n processCheckbox(props, context);\n } else if (displayName === 'Form.Password') {\n processPassword(props, context);\n } else if (\n displayName === 'Form.Select' ||\n displayName === 'Form.Combobox' ||\n displayName === 'Form.Autocomplete'\n ) {\n processDropdown(props, context);\n } else {\n processDefault(props, context);\n }\n }\n });\n\n return applyRefinements(z.object(context.rules).loose(), context);\n};\n"],"mappings":"6NAuBA,MAAM,GACJ,EACA,IACG,CACH,GAAI,CAAC,EAAM,MAAQ,CAAC,EAAM,OAAO,SAAS,YAAY,CAAE,OAGxD,IAAM,EADY,EAAM,MAAM,MAAM,IAAI,CACR,KAAM,GAAM,EAAE,WAAW,YAAY,CAAC,CAChE,EAAS,GAAe,SAAS,IAAI,CACvC,EAAc,MAAM,IAAI,CAAC,GACzB,GAAG,EAAM,KAAK,eAElB,EAAQ,gBAAgB,KAAK,CAAE,MAAO,EAAM,KAAM,SAAQ,CAAC,EAGvD,GAAmB,EAAuB,IAA6B,CACtE,EAAM,OAEP,EAAM,OAAO,SAAS,WAAW,EACnC,EAAQ,MAAM,EAAM,MAAQ,EAAE,SAAS,CAAC,UAAU,CAAC,GAAG,EAAE,QAAQ,GAAM,CAAC,CACvE,EAAQ,sBAAsB,KAAK,EAAM,KAAK,EAE9C,EAAQ,MAAM,EAAM,MAAQ,EAAE,SAAS,CAAC,UAAU,GAIhD,GAAmB,EAAuB,IAA6B,CAC3E,GAAI,CAAC,EAAM,KAAM,OAEjB,IAAI,EAAyB,EAAE,QAAQ,CACjC,EAAa,EAAM,MACrB,EAAM,MACH,MAAM,IAAI,CACV,OAAQ,GAAM,EAAE,MAAM,GAAK,WAAW,CACtC,KAAK,IAAI,CACZ,GAEA,IACF,EAAa,EAAW,EAAW,EAGrC,IAAM,EACJ,EAAM,QAAU,EAAM,OAAO,SAAS,WAAW,C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,EAAQ,CAEhC,IAAgB,gBAClB,EAAgB,EAAO,EAAQ,CACtB,IAAgB,gBACzB,EAAgB,EAAO,EAAQ,CAE/B,IAAgB,eAChB,IAAgB,iBAChB,IAAgB,oBAEhB,EAAgB,EAAO,EAAQ,CAE/B,EAAe,EAAO,EAAQ,GAGlC,CAEK,EAAiB,EAAE,OAAO,EAAQ,MAAM,CAAC,OAAO,CAAE,EAAQ"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mage-ui/components",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.104",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "",
|
|
6
6
|
"keywords": [],
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"rimraf": "^6.1.3",
|
|
26
26
|
"tsdown": "^0.21.4",
|
|
27
27
|
"typescript": "^5.9.3",
|
|
28
|
-
"@mage-ui/preset": "1.0.
|
|
28
|
+
"@mage-ui/preset": "1.0.104",
|
|
29
29
|
"@mage-ui/styled-system": "1.0.8"
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|