@ttoss/forms 0.14.28 → 0.14.30
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/esm/index.js +98 -1
- package/dist/index.d.ts +11 -1
- package/dist/index.js +101 -0
- package/package.json +9 -9
- package/src/FormGroup.tsx +116 -0
- package/src/index.ts +1 -0
package/dist/esm/index.js
CHANGED
|
@@ -305,4 +305,101 @@ var FormFieldTextarea = ({
|
|
|
305
305
|
}
|
|
306
306
|
});
|
|
307
307
|
};
|
|
308
|
-
|
|
308
|
+
|
|
309
|
+
// src/FormGroup.tsx
|
|
310
|
+
import { Flex as Flex2 } from "@ttoss/ui";
|
|
311
|
+
import React3 from "react";
|
|
312
|
+
import { jsx as jsx9 } from "react/jsx-runtime";
|
|
313
|
+
var FormGroupLevelsManagerContext = /*#__PURE__*/React3.createContext({
|
|
314
|
+
registerChild: () => {
|
|
315
|
+
return null;
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
var FormGroupLevelsManager = ({
|
|
319
|
+
children
|
|
320
|
+
}) => {
|
|
321
|
+
const [levelsLength, setLevelsLength] = React3.useState(0);
|
|
322
|
+
const registerChild = React3.useCallback(level => {
|
|
323
|
+
if (level + 1 > levelsLength) {
|
|
324
|
+
setLevelsLength(level + 1);
|
|
325
|
+
}
|
|
326
|
+
}, [levelsLength]);
|
|
327
|
+
return /* @__PURE__ */jsx9(FormGroupLevelsManagerContext.Provider, {
|
|
328
|
+
value: {
|
|
329
|
+
levelsLength,
|
|
330
|
+
registerChild
|
|
331
|
+
},
|
|
332
|
+
children
|
|
333
|
+
});
|
|
334
|
+
};
|
|
335
|
+
var FormGroupContext = /*#__PURE__*/React3.createContext({});
|
|
336
|
+
var useFormGroup = () => {
|
|
337
|
+
const {
|
|
338
|
+
parentLevel
|
|
339
|
+
} = React3.useContext(FormGroupContext);
|
|
340
|
+
const {
|
|
341
|
+
levelsLength
|
|
342
|
+
} = React3.useContext(FormGroupLevelsManagerContext);
|
|
343
|
+
return {
|
|
344
|
+
level: parentLevel,
|
|
345
|
+
levelsLength
|
|
346
|
+
};
|
|
347
|
+
};
|
|
348
|
+
var FormGroupWrapper = ({
|
|
349
|
+
children
|
|
350
|
+
}) => {
|
|
351
|
+
const {
|
|
352
|
+
level,
|
|
353
|
+
levelsLength
|
|
354
|
+
} = useFormGroup();
|
|
355
|
+
const {
|
|
356
|
+
registerChild
|
|
357
|
+
} = React3.useContext(FormGroupLevelsManagerContext);
|
|
358
|
+
React3.useEffect(() => {
|
|
359
|
+
if (typeof level === "number") {
|
|
360
|
+
registerChild(level);
|
|
361
|
+
}
|
|
362
|
+
}, [level, registerChild]);
|
|
363
|
+
const sx = React3.useMemo(() => {
|
|
364
|
+
return {
|
|
365
|
+
flexDirection: "column",
|
|
366
|
+
width: "100%",
|
|
367
|
+
gap: "md",
|
|
368
|
+
paddingLeft: ({
|
|
369
|
+
space
|
|
370
|
+
}) => {
|
|
371
|
+
if (!level || !levelsLength) {
|
|
372
|
+
return 0;
|
|
373
|
+
}
|
|
374
|
+
const value = levelsLength / level;
|
|
375
|
+
return `calc(${space["lg"]} / ${value})`;
|
|
376
|
+
}
|
|
377
|
+
};
|
|
378
|
+
}, [level, levelsLength]);
|
|
379
|
+
return /* @__PURE__ */jsx9(Flex2, {
|
|
380
|
+
"aria-level": level,
|
|
381
|
+
sx,
|
|
382
|
+
children
|
|
383
|
+
});
|
|
384
|
+
};
|
|
385
|
+
var FormGroup = ({
|
|
386
|
+
children
|
|
387
|
+
}) => {
|
|
388
|
+
const {
|
|
389
|
+
level
|
|
390
|
+
} = useFormGroup();
|
|
391
|
+
const currentLevel = level === void 0 ? 0 : level + 1;
|
|
392
|
+
return /* @__PURE__ */jsx9(FormGroupContext.Provider, {
|
|
393
|
+
value: {
|
|
394
|
+
parentLevel: currentLevel
|
|
395
|
+
},
|
|
396
|
+
children: currentLevel === 0 ? /* @__PURE__ */jsx9(FormGroupLevelsManager, {
|
|
397
|
+
children: /* @__PURE__ */jsx9(FormGroupWrapper, {
|
|
398
|
+
children
|
|
399
|
+
})
|
|
400
|
+
}) : /* @__PURE__ */jsx9(FormGroupWrapper, {
|
|
401
|
+
children
|
|
402
|
+
})
|
|
403
|
+
});
|
|
404
|
+
};
|
|
405
|
+
export { Form, FormField, FormFieldCheckbox, FormFieldInput, FormFieldRadio, FormFieldSelect, FormFieldTextarea, FormGroup, useFormGroup, yup, yupResolver };
|
package/dist/index.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ export * from 'react-hook-form';
|
|
|
5
5
|
import * as yup from 'yup';
|
|
6
6
|
export { yup };
|
|
7
7
|
import * as React from 'react';
|
|
8
|
+
import React__default from 'react';
|
|
8
9
|
import * as _ttoss_ui from '@ttoss/ui';
|
|
9
10
|
import { BoxProps, CheckboxProps, LabelProps, RadioProps, TextareaProps } from '@ttoss/ui';
|
|
10
11
|
import * as theme_ui from 'theme-ui';
|
|
@@ -63,4 +64,13 @@ declare const FormFieldTextarea: <TFieldValues extends FieldValues = FieldValues
|
|
|
63
64
|
name: TName;
|
|
64
65
|
} & TextareaProps) => JSX.Element;
|
|
65
66
|
|
|
66
|
-
|
|
67
|
+
declare const useFormGroup: () => {
|
|
68
|
+
level: number | undefined;
|
|
69
|
+
levelsLength: number | undefined;
|
|
70
|
+
};
|
|
71
|
+
type FormGroupProps = {
|
|
72
|
+
children: React__default.ReactNode;
|
|
73
|
+
};
|
|
74
|
+
declare const FormGroup: ({ children }: FormGroupProps) => JSX.Element;
|
|
75
|
+
|
|
76
|
+
export { Form, FormField, FormFieldCheckbox, FormFieldInput, FormFieldRadio, FormFieldSelect, FormFieldTextarea, FormGroup, useFormGroup };
|
package/dist/index.js
CHANGED
|
@@ -46,6 +46,8 @@ __export(src_exports, {
|
|
|
46
46
|
FormFieldRadio: () => FormFieldRadio,
|
|
47
47
|
FormFieldSelect: () => FormFieldSelect,
|
|
48
48
|
FormFieldTextarea: () => FormFieldTextarea,
|
|
49
|
+
FormGroup: () => FormGroup,
|
|
50
|
+
useFormGroup: () => useFormGroup,
|
|
49
51
|
yup: () => yup,
|
|
50
52
|
yupResolver: () => import_yup.yupResolver
|
|
51
53
|
});
|
|
@@ -354,6 +356,103 @@ var FormFieldTextarea = ({
|
|
|
354
356
|
}
|
|
355
357
|
});
|
|
356
358
|
};
|
|
359
|
+
|
|
360
|
+
// src/FormGroup.tsx
|
|
361
|
+
var import_ui9 = require("@ttoss/ui");
|
|
362
|
+
var import_react = __toESM(require("react"));
|
|
363
|
+
var import_jsx_runtime9 = require("react/jsx-runtime");
|
|
364
|
+
var FormGroupLevelsManagerContext = import_react.default.createContext({
|
|
365
|
+
registerChild: () => {
|
|
366
|
+
return null;
|
|
367
|
+
}
|
|
368
|
+
});
|
|
369
|
+
var FormGroupLevelsManager = ({
|
|
370
|
+
children
|
|
371
|
+
}) => {
|
|
372
|
+
const [levelsLength, setLevelsLength] = import_react.default.useState(0);
|
|
373
|
+
const registerChild = import_react.default.useCallback(level => {
|
|
374
|
+
if (level + 1 > levelsLength) {
|
|
375
|
+
setLevelsLength(level + 1);
|
|
376
|
+
}
|
|
377
|
+
}, [levelsLength]);
|
|
378
|
+
return /* @__PURE__ */(0, import_jsx_runtime9.jsx)(FormGroupLevelsManagerContext.Provider, {
|
|
379
|
+
value: {
|
|
380
|
+
levelsLength,
|
|
381
|
+
registerChild
|
|
382
|
+
},
|
|
383
|
+
children
|
|
384
|
+
});
|
|
385
|
+
};
|
|
386
|
+
var FormGroupContext = import_react.default.createContext({});
|
|
387
|
+
var useFormGroup = () => {
|
|
388
|
+
const {
|
|
389
|
+
parentLevel
|
|
390
|
+
} = import_react.default.useContext(FormGroupContext);
|
|
391
|
+
const {
|
|
392
|
+
levelsLength
|
|
393
|
+
} = import_react.default.useContext(FormGroupLevelsManagerContext);
|
|
394
|
+
return {
|
|
395
|
+
level: parentLevel,
|
|
396
|
+
levelsLength
|
|
397
|
+
};
|
|
398
|
+
};
|
|
399
|
+
var FormGroupWrapper = ({
|
|
400
|
+
children
|
|
401
|
+
}) => {
|
|
402
|
+
const {
|
|
403
|
+
level,
|
|
404
|
+
levelsLength
|
|
405
|
+
} = useFormGroup();
|
|
406
|
+
const {
|
|
407
|
+
registerChild
|
|
408
|
+
} = import_react.default.useContext(FormGroupLevelsManagerContext);
|
|
409
|
+
import_react.default.useEffect(() => {
|
|
410
|
+
if (typeof level === "number") {
|
|
411
|
+
registerChild(level);
|
|
412
|
+
}
|
|
413
|
+
}, [level, registerChild]);
|
|
414
|
+
const sx = import_react.default.useMemo(() => {
|
|
415
|
+
return {
|
|
416
|
+
flexDirection: "column",
|
|
417
|
+
width: "100%",
|
|
418
|
+
gap: "md",
|
|
419
|
+
paddingLeft: ({
|
|
420
|
+
space
|
|
421
|
+
}) => {
|
|
422
|
+
if (!level || !levelsLength) {
|
|
423
|
+
return 0;
|
|
424
|
+
}
|
|
425
|
+
const value = levelsLength / level;
|
|
426
|
+
return `calc(${space["lg"]} / ${value})`;
|
|
427
|
+
}
|
|
428
|
+
};
|
|
429
|
+
}, [level, levelsLength]);
|
|
430
|
+
return /* @__PURE__ */(0, import_jsx_runtime9.jsx)(import_ui9.Flex, {
|
|
431
|
+
"aria-level": level,
|
|
432
|
+
sx,
|
|
433
|
+
children
|
|
434
|
+
});
|
|
435
|
+
};
|
|
436
|
+
var FormGroup = ({
|
|
437
|
+
children
|
|
438
|
+
}) => {
|
|
439
|
+
const {
|
|
440
|
+
level
|
|
441
|
+
} = useFormGroup();
|
|
442
|
+
const currentLevel = level === void 0 ? 0 : level + 1;
|
|
443
|
+
return /* @__PURE__ */(0, import_jsx_runtime9.jsx)(FormGroupContext.Provider, {
|
|
444
|
+
value: {
|
|
445
|
+
parentLevel: currentLevel
|
|
446
|
+
},
|
|
447
|
+
children: currentLevel === 0 ? /* @__PURE__ */(0, import_jsx_runtime9.jsx)(FormGroupLevelsManager, {
|
|
448
|
+
children: /* @__PURE__ */(0, import_jsx_runtime9.jsx)(FormGroupWrapper, {
|
|
449
|
+
children
|
|
450
|
+
})
|
|
451
|
+
}) : /* @__PURE__ */(0, import_jsx_runtime9.jsx)(FormGroupWrapper, {
|
|
452
|
+
children
|
|
453
|
+
})
|
|
454
|
+
});
|
|
455
|
+
};
|
|
357
456
|
// Annotate the CommonJS export names for ESM import in node:
|
|
358
457
|
0 && (module.exports = {
|
|
359
458
|
Form,
|
|
@@ -363,6 +462,8 @@ var FormFieldTextarea = ({
|
|
|
363
462
|
FormFieldRadio,
|
|
364
463
|
FormFieldSelect,
|
|
365
464
|
FormFieldTextarea,
|
|
465
|
+
FormGroup,
|
|
466
|
+
useFormGroup,
|
|
366
467
|
yup,
|
|
367
468
|
yupResolver,
|
|
368
469
|
...require("react-hook-form")
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ttoss/forms",
|
|
3
|
-
"version": "0.14.
|
|
3
|
+
"version": "0.14.30",
|
|
4
4
|
"license": "UNLICENSED",
|
|
5
5
|
"author": "ttoss",
|
|
6
6
|
"contributors": [
|
|
@@ -20,26 +20,26 @@
|
|
|
20
20
|
"typings": "dist/index.d.ts",
|
|
21
21
|
"dependencies": {
|
|
22
22
|
"@hookform/error-message": "^2.0.1",
|
|
23
|
-
"@hookform/resolvers": "^
|
|
24
|
-
"react-hook-form": "^7.43.
|
|
25
|
-
"yup": "^1.
|
|
23
|
+
"@hookform/resolvers": "^3.0.1",
|
|
24
|
+
"react-hook-form": "^7.43.9",
|
|
25
|
+
"yup": "^1.1.0"
|
|
26
26
|
},
|
|
27
27
|
"peerDependencies": {
|
|
28
|
-
"@ttoss/ui": "^1.
|
|
28
|
+
"@ttoss/ui": "^1.31.15",
|
|
29
29
|
"react": "^18.2.0"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@ttoss/config": "^1.29.3",
|
|
33
33
|
"@ttoss/test-utils": "^1.21.2",
|
|
34
|
-
"@ttoss/ui": "^1.31.
|
|
34
|
+
"@ttoss/ui": "^1.31.16",
|
|
35
35
|
"@types/jest": "^29.5.0",
|
|
36
36
|
"jest": "^29.5.0",
|
|
37
37
|
"react": "^18.2.0",
|
|
38
|
-
"react-hook-form": "^7.43.
|
|
39
|
-
"yup": "^1.
|
|
38
|
+
"react-hook-form": "^7.43.9",
|
|
39
|
+
"yup": "^1.1.0"
|
|
40
40
|
},
|
|
41
41
|
"publishConfig": {
|
|
42
42
|
"access": "public"
|
|
43
43
|
},
|
|
44
|
-
"gitHead": "
|
|
44
|
+
"gitHead": "aa31505953d2717d84fa5e69dbae5b488ef04986"
|
|
45
45
|
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { Flex, FlexProps } from '@ttoss/ui';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
|
|
4
|
+
type FormGroupLevelsManagerContextType = {
|
|
5
|
+
levelsLength?: number;
|
|
6
|
+
registerChild: (level: number) => void;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const FormGroupLevelsManagerContext =
|
|
10
|
+
React.createContext<FormGroupLevelsManagerContextType>({
|
|
11
|
+
registerChild: () => {
|
|
12
|
+
return null;
|
|
13
|
+
},
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
const FormGroupLevelsManager = ({
|
|
17
|
+
children,
|
|
18
|
+
}: {
|
|
19
|
+
children: React.ReactNode;
|
|
20
|
+
}) => {
|
|
21
|
+
const [levelsLength, setLevelsLength] = React.useState(0);
|
|
22
|
+
|
|
23
|
+
const registerChild = React.useCallback(
|
|
24
|
+
(level: number) => {
|
|
25
|
+
if (level + 1 > levelsLength) {
|
|
26
|
+
setLevelsLength(level + 1);
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
[levelsLength]
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
return (
|
|
33
|
+
<FormGroupLevelsManagerContext.Provider
|
|
34
|
+
value={{ levelsLength, registerChild }}
|
|
35
|
+
>
|
|
36
|
+
{children}
|
|
37
|
+
</FormGroupLevelsManagerContext.Provider>
|
|
38
|
+
);
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
type FormGroupContextType = {
|
|
42
|
+
parentLevel?: number;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const FormGroupContext = React.createContext<FormGroupContextType>({});
|
|
46
|
+
|
|
47
|
+
export const useFormGroup = () => {
|
|
48
|
+
const { parentLevel } = React.useContext(FormGroupContext);
|
|
49
|
+
const { levelsLength } = React.useContext(FormGroupLevelsManagerContext);
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
level: parentLevel,
|
|
53
|
+
levelsLength,
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
type FormGroupProps = {
|
|
58
|
+
children: React.ReactNode;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const FormGroupWrapper = ({ children }: { children: React.ReactNode }) => {
|
|
62
|
+
const { level, levelsLength } = useFormGroup();
|
|
63
|
+
|
|
64
|
+
const { registerChild } = React.useContext(FormGroupLevelsManagerContext);
|
|
65
|
+
|
|
66
|
+
React.useEffect(() => {
|
|
67
|
+
/**
|
|
68
|
+
* We can't use if(level) because level can be 0 and we want to register
|
|
69
|
+
* it anyway.
|
|
70
|
+
*/
|
|
71
|
+
if (typeof level === 'number') {
|
|
72
|
+
registerChild(level);
|
|
73
|
+
}
|
|
74
|
+
}, [level, registerChild]);
|
|
75
|
+
|
|
76
|
+
const sx: FlexProps['sx'] = React.useMemo(() => {
|
|
77
|
+
return {
|
|
78
|
+
flexDirection: 'column',
|
|
79
|
+
width: '100%',
|
|
80
|
+
gap: 'md',
|
|
81
|
+
paddingLeft: ({ space }: any) => {
|
|
82
|
+
if (!level || !levelsLength) {
|
|
83
|
+
return 0;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const value = levelsLength / level;
|
|
87
|
+
|
|
88
|
+
return `calc(${space['lg']} / ${value})`;
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
}, [level, levelsLength]);
|
|
92
|
+
|
|
93
|
+
return (
|
|
94
|
+
<Flex aria-level={level} sx={sx}>
|
|
95
|
+
{children}
|
|
96
|
+
</Flex>
|
|
97
|
+
);
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
export const FormGroup = ({ children }: FormGroupProps) => {
|
|
101
|
+
const { level } = useFormGroup();
|
|
102
|
+
|
|
103
|
+
const currentLevel = level === undefined ? 0 : level + 1;
|
|
104
|
+
|
|
105
|
+
return (
|
|
106
|
+
<FormGroupContext.Provider value={{ parentLevel: currentLevel }}>
|
|
107
|
+
{currentLevel === 0 ? (
|
|
108
|
+
<FormGroupLevelsManager>
|
|
109
|
+
<FormGroupWrapper>{children}</FormGroupWrapper>
|
|
110
|
+
</FormGroupLevelsManager>
|
|
111
|
+
) : (
|
|
112
|
+
<FormGroupWrapper>{children}</FormGroupWrapper>
|
|
113
|
+
)}
|
|
114
|
+
</FormGroupContext.Provider>
|
|
115
|
+
);
|
|
116
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -9,3 +9,4 @@ export { FormFieldInput } from './FormFieldInput';
|
|
|
9
9
|
export { FormFieldRadio } from './FormFieldRadio';
|
|
10
10
|
export { FormFieldSelect } from './FormFieldSelect';
|
|
11
11
|
export { FormFieldTextarea } from './FormFieldTextarea';
|
|
12
|
+
export { FormGroup, useFormGroup } from './FormGroup';
|