react-form-dto 0.0.8 → 0.0.9

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/README.md CHANGED
@@ -214,6 +214,143 @@ const profileForm: FormDTO = {
214
214
 
215
215
  ---
216
216
 
217
+ ## 🌍 Internationalization (I18n)
218
+
219
+ React Form DTO has built-in support for multi-language forms through `I18String` and `I18nOption` types.
220
+
221
+ ### Using I18String
222
+
223
+ Any text property (`label`, `placeholder`, `title`, `description`, validation messages) can be either a plain string or a locale map:
224
+
225
+ ```tsx
226
+ // Simple string (single language)
227
+ {
228
+ label: "First Name"
229
+ }
230
+
231
+ // Multi-language support
232
+ {
233
+ label: {
234
+ en: "First Name",
235
+ fr: "Prénom",
236
+ es: "Nombre",
237
+ de: "Vorname"
238
+ }
239
+ }
240
+ ```
241
+
242
+ ### Using I18nOption for Selections
243
+
244
+ For select, autocomplete, and multi-autocomplete fields, you can use `I18nOption` objects to provide translatable labels while maintaining stable values:
245
+
246
+ ```tsx
247
+ {
248
+ id: "country",
249
+ type: "select",
250
+ label: { en: "Country", fr: "Pays" },
251
+ options: [
252
+ {
253
+ value: "us",
254
+ label: { en: "United States", fr: "États-Unis" }
255
+ },
256
+ {
257
+ value: "fr",
258
+ label: { en: "France", fr: "France" }
259
+ },
260
+ {
261
+ value: "de",
262
+ label: { en: "Germany", fr: "Allemagne" }
263
+ }
264
+ ]
265
+ }
266
+ ```
267
+
268
+ **Backward Compatibility:** Simple string arrays still work:
269
+
270
+ ```tsx
271
+ options: ["USA", "France", "Germany"] // Still supported
272
+ ```
273
+
274
+ ### I18n Validation Messages
275
+
276
+ Validation messages also support I18n:
277
+
278
+ ```tsx
279
+ validations: {
280
+ required: {
281
+ en: "This field is required",
282
+ fr: "Ce champ est obligatoire",
283
+ es: "Este campo es obligatorio"
284
+ },
285
+ validate: (value) => value.length < 2
286
+ ? {
287
+ en: "Minimum 2 characters",
288
+ fr: "Minimum 2 caractères"
289
+ }
290
+ : null
291
+ }
292
+ ```
293
+
294
+ ### Complete I18n Example
295
+
296
+ ```tsx
297
+ const multilingualForm: FormDTO = {
298
+ title: {
299
+ en: "User Registration",
300
+ fr: "Inscription de l'utilisateur",
301
+ es: "Registro de Usuario"
302
+ },
303
+ description: {
304
+ en: "Please fill in your details",
305
+ fr: "Veuillez remplir vos coordonnées",
306
+ es: "Por favor complete sus datos"
307
+ },
308
+ sections: [
309
+ {
310
+ id: "personal",
311
+ heading: {
312
+ en: "Personal Information",
313
+ fr: "Informations personnelles",
314
+ es: "Información Personal"
315
+ },
316
+ fields: [
317
+ {
318
+ id: "title",
319
+ type: "select",
320
+ label: { en: "Title", fr: "Titre", es: "Título" },
321
+ options: [
322
+ { value: "mr", label: { en: "Mr", fr: "M.", es: "Sr." } },
323
+ { value: "ms", label: { en: "Ms", fr: "Mme", es: "Sra." } },
324
+ { value: "dr", label: { en: "Dr", fr: "Dr", es: "Dr." } }
325
+ ]
326
+ },
327
+ {
328
+ id: "firstName",
329
+ type: "text",
330
+ label: { en: "First Name", fr: "Prénom", es: "Nombre" },
331
+ placeholder: {
332
+ en: "Enter your first name",
333
+ fr: "Entrez votre prénom",
334
+ es: "Ingrese su nombre"
335
+ },
336
+ validations: {
337
+ required: {
338
+ en: "First name is required",
339
+ fr: "Le prénom est obligatoire",
340
+ es: "El nombre es obligatorio"
341
+ }
342
+ }
343
+ }
344
+ ]
345
+ }
346
+ ]
347
+ };
348
+ ```
349
+
350
+ > **Note:** The library provides the I18n structure. You'll need to implement locale selection and text resolution in your application layer.
351
+
352
+ ---
353
+
217
354
  ## Custom Field Renderers
218
355
 
219
356
  Override any default renderer by supplying your own component:
@@ -11,6 +11,7 @@ export interface FieldRendererProps {
11
11
  /** Callback function to update the field value */
12
12
  onChange: (val: any) => void;
13
13
  error?: string | null;
14
+ locale: string;
14
15
  }
15
16
  /**
16
17
  * Props for the Field component.
@@ -23,6 +23,8 @@ type FormBuilderProps = {
23
23
  dto: FormDTO;
24
24
  /** Optional custom renderers for specific field types */
25
25
  renderers?: Record<string, React.ComponentType<any>>;
26
+ /** Current locale for i18n string resolution (default: 'en') */
27
+ locale?: string;
26
28
  };
27
29
  /**
28
30
  * FormBuilder component that dynamically renders a form based on a FormDTO definition.
@@ -13,6 +13,8 @@ type SectionProps = {
13
13
  /** Optional custom renderers for specific field types */
14
14
  renderers?: Record<string, React.ComponentType<any>>;
15
15
  validateField: (id: string) => string[];
16
+ /** Current locale for i18n string resolution (default: 'en') */
17
+ locale?: string;
16
18
  };
17
19
  /**
18
20
  * Section component that renders a form section with its heading, description, and fields.
@@ -5,6 +5,7 @@ type AutoCompleteFieldProps = {
5
5
  value: any;
6
6
  onChange: (val: any) => void;
7
7
  error?: string | null;
8
+ locale?: string;
8
9
  };
9
10
  export declare const AutoCompleteField: React.FC<AutoCompleteFieldProps>;
10
11
  export {};
@@ -13,4 +13,4 @@ import { FieldRendererProps } from '../../components/Field';
13
13
  * onChange={handleChange}
14
14
  * />
15
15
  */
16
- export declare function CheckBoxInput({ field, value, onChange, error, }: FieldRendererProps): import("react/jsx-runtime").JSX.Element;
16
+ export declare function CheckBoxInput({ field, value, onChange, error, locale, }: FieldRendererProps): import("react/jsx-runtime").JSX.Element;
@@ -5,6 +5,7 @@ type MultiAutoCompleteFieldProps = {
5
5
  value: any[];
6
6
  onChange: (val: any) => void;
7
7
  error?: string | null;
8
+ locale?: string;
8
9
  };
9
10
  export declare const MultiAutoCompleteField: React.FC<MultiAutoCompleteFieldProps>;
10
11
  export {};
@@ -5,6 +5,7 @@ type RadioInputProps = {
5
5
  value: any;
6
6
  onChange: (val: any) => void;
7
7
  error?: string | null;
8
+ locale?: string;
8
9
  };
9
10
  export declare const RadioInput: React.FC<RadioInputProps>;
10
11
  export {};
@@ -13,4 +13,4 @@ import { FieldRendererProps } from '../../components/Field';
13
13
  * onChange={handleChange}
14
14
  * />
15
15
  */
16
- export declare function SelectInput({ field, value, onChange, error, }: FieldRendererProps): import("react/jsx-runtime").JSX.Element;
16
+ export declare function SelectInput({ field, value, onChange, error, locale, }: FieldRendererProps): import("react/jsx-runtime").JSX.Element;
@@ -13,4 +13,4 @@ import { FieldRendererProps } from '../../components/Field';
13
13
  * onChange={handleChange}
14
14
  * />
15
15
  */
16
- export declare function TextInput({ field, value, onChange, error, }: FieldRendererProps): import("react/jsx-runtime").JSX.Element;
16
+ export declare function TextInput({ field, value, onChange, error, locale, }: FieldRendererProps): import("react/jsx-runtime").JSX.Element;
@@ -5,6 +5,7 @@ type TextAreaInputProps = {
5
5
  value: any;
6
6
  onChange: (val: any) => void;
7
7
  error?: string | null;
8
+ locale?: string;
8
9
  };
9
10
  export declare const TextAreaInput: React.FC<TextAreaInputProps>;
10
11
  export {};
@@ -1,2 +1,3 @@
1
1
  import { FormDTO } from '../types';
2
2
  export declare const profileForm: FormDTO;
3
+ export declare const profileFormI18n: FormDTO;
@@ -12,7 +12,7 @@ type Errors = Record<string, string | null>;
12
12
  * form.handleChange('email', 'user@example.com');
13
13
  * const errors = form.validateAll();
14
14
  */
15
- export declare function useFormBuilder(dto: FormDTO): {
15
+ export declare function useFormBuilder(dto: FormDTO, locale?: string): {
16
16
  /** Current form values for all fields */
17
17
  values: Record<string, any>;
18
18
  /** Function to update a field value */