Skip to content

Commit 2b2ae90

Browse files
authored
feat(spec): add generic for InputProps and LayoutProps into Spec (#164)
Co-authored-by: Andrei Bitiutskii <[email protected]>
1 parent ab23f6c commit 2b2ae90

File tree

13 files changed

+328
-91
lines changed

13 files changed

+328
-91
lines changed

src/lib/core/components/Form/Controller/utils.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,30 @@ export const getRender = <DirtyValue extends FieldValue, SpecType extends Spec>(
135135
const render = (props: FieldRenderProps<DirtyValue>) => {
136136
if (inputEntity && isCorrectSpec(spec) && _.isString(name)) {
137137
if (!spec.viewSpec.hidden) {
138+
const {layoutProps, inputProps} = spec.viewSpec;
138139
if (inputEntity.independent) {
139140
const InputComponent = inputEntity.Component;
140141

141-
return <InputComponent spec={spec} name={name} Layout={Layout} {...props} />;
142+
return (
143+
<InputComponent
144+
spec={spec}
145+
name={name}
146+
Layout={Layout}
147+
inputProps={inputProps}
148+
layoutProps={layoutProps}
149+
{...props}
150+
/>
151+
);
142152
}
143153

144154
const InputComponent = inputEntity.Component;
145-
const input = <InputComponent spec={spec} name={name} {...props} />;
155+
const input = (
156+
<InputComponent spec={spec} name={name} inputProps={inputProps} {...props} />
157+
);
146158

147159
if (Layout) {
148160
return (
149-
<Layout spec={spec} name={name} {...props}>
161+
<Layout spec={spec} name={name} layoutProps={layoutProps} {...props}>
150162
{input}
151163
</Layout>
152164
);

src/lib/core/components/Form/types/array.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,39 @@ import {
1616
ValidatorsMap,
1717
} from './';
1818

19-
export type ArrayInputProps = InputProps<FieldArrayValue, ArraySpec>;
20-
export type ArrayIndependentInputProps = IndependentInputProps<FieldArrayValue, ArraySpec>;
21-
export type ArrayLayoutProps = LayoutProps<FieldArrayValue, ArraySpec>;
19+
export type ArrayInputProps<InputComponentProps extends Record<string, any> = {}> = InputProps<
20+
FieldArrayValue,
21+
ArraySpec<undefined, InputComponentProps>
22+
>;
23+
export type ArrayIndependentInputProps<
24+
InputComponentProps extends Record<string, any> = {},
25+
LayoutComponentProps extends Record<string, any> = {},
26+
> = IndependentInputProps<
27+
FieldArrayValue,
28+
ArraySpec<undefined, InputComponentProps, LayoutComponentProps>
29+
>;
30+
31+
export type ArrayLayoutProps<LayoutComponentProps extends Record<string, any> = {}> = LayoutProps<
32+
FieldArrayValue,
33+
ArraySpec<undefined, any, LayoutComponentProps>
34+
>;
2235

23-
export type ArrayInput = InputType<FieldArrayValue, ArraySpec>;
24-
export type ArrayIndependentInput = IndependentInputType<FieldArrayValue, ArraySpec>;
25-
export type ArrayLayout = LayoutType<FieldArrayValue, ArraySpec>;
36+
export type ArrayInput<InputComponentProps extends Record<string, any> = {}> = InputType<
37+
FieldArrayValue,
38+
ArraySpec<undefined, InputComponentProps>
39+
>;
40+
export type ArrayIndependentInput<
41+
InputComponentProps extends Record<string, any> = {},
42+
LayoutComponentProps extends Record<string, any> = {},
43+
> = IndependentInputType<
44+
FieldArrayValue,
45+
ArraySpec<undefined, InputComponentProps, LayoutComponentProps>
46+
>;
47+
48+
export type ArrayLayout<LayoutComponentProps extends Record<string, any> = {}> = LayoutType<
49+
FieldArrayValue,
50+
ArraySpec<undefined, any, LayoutComponentProps>
51+
>;
2652

2753
export type ArrayInputEntity = InputEntity<FieldArrayValue, ArraySpec>;
2854
export type ArrayIndependentInputEntity = IndependentInputEntity<FieldArrayValue, ArraySpec>;

src/lib/core/components/Form/types/boolean.ts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,41 @@ import {
1515
ValidatorsMap,
1616
} from './';
1717

18-
export type BooleanInputProps = InputProps<boolean, BooleanSpec>;
19-
export type BooleanIndependentInputProps = IndependentInputProps<boolean, BooleanSpec>;
20-
export type BooleanLayoutProps = LayoutProps<boolean, BooleanSpec>;
18+
export type BooleanInputProps<InputComponentProps extends Record<string, any> = {}> = InputProps<
19+
boolean,
20+
BooleanSpec<undefined, InputComponentProps>
21+
>;
2122

22-
export type BooleanInput = InputType<boolean, BooleanSpec>;
23-
export type BooleanIndependentInput = IndependentInputType<boolean, BooleanSpec>;
24-
export type BooleanLayout = LayoutType<boolean, BooleanSpec>;
23+
export type BooleanIndependentInputProps<
24+
InputComponentProps extends Record<string, any> = {},
25+
LayoutComponentProps extends Record<string, any> = {},
26+
> = IndependentInputProps<
27+
boolean,
28+
BooleanSpec<undefined, InputComponentProps, LayoutComponentProps>
29+
>;
30+
31+
export type BooleanLayoutProps<LayoutComponentProps extends Record<string, any> = {}> = LayoutProps<
32+
boolean,
33+
BooleanSpec<undefined, any, LayoutComponentProps>
34+
>;
35+
36+
export type BooleanInput<InputComponentProps extends Record<string, any> = {}> = InputType<
37+
boolean,
38+
BooleanSpec<undefined, InputComponentProps>
39+
>;
40+
41+
export type BooleanIndependentInput<
42+
InputComponentProps extends Record<string, any> = {},
43+
LayoutComponentProps extends Record<string, any> = {},
44+
> = IndependentInputType<
45+
boolean,
46+
BooleanSpec<undefined, InputComponentProps, LayoutComponentProps>
47+
>;
48+
49+
export type BooleanLayout<LayoutComponentProps extends Record<string, any> = {}> = LayoutType<
50+
boolean,
51+
BooleanSpec<undefined, any, LayoutComponentProps>
52+
>;
2553

2654
export type BooleanInputEntity = InputEntity<boolean, BooleanSpec>;
2755
export type BooleanIndependentInputEntity = IndependentInputEntity<boolean, BooleanSpec>;

src/lib/core/components/Form/types/input.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import {FieldRenderProps, FieldValue, LayoutType} from './';
77
export type InputProps<Value extends FieldValue, SpecType extends Spec> = {
88
spec: SpecType;
99
name: string;
10+
inputProps?: SpecType['viewSpec']['inputProps'];
1011
} & FieldRenderProps<Value>;
1112

1213
export type IndependentInputProps<Value extends FieldValue, SpecType extends Spec> = {
1314
Layout: LayoutType<Value, SpecType> | undefined;
15+
layoutProps?: SpecType['viewSpec']['layoutProps'];
1416
} & InputProps<Value, SpecType>;
1517

1618
export type InputType<Value extends FieldValue, SpecType extends Spec> = (

src/lib/core/components/Form/types/layout.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {FieldValue, InputProps} from './';
66

77
export type LayoutProps<Value extends FieldValue, SpecType extends Spec> = {
88
children: React.ReactElement;
9+
layoutProps?: SpecType['viewSpec']['layoutProps'];
910
} & InputProps<Value, SpecType>;
1011

1112
export type LayoutType<Value extends FieldValue, SpecType extends Spec> = (

src/lib/core/components/Form/types/number.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,35 @@ import {
1515
ValidatorsMap,
1616
} from './';
1717

18-
export type NumberInputProps = InputProps<number, NumberSpec>;
19-
export type NumberIndependentInputProps = IndependentInputProps<number, NumberSpec>;
20-
export type NumberLayoutProps = LayoutProps<number, NumberSpec>;
18+
export type NumberInputProps<InputComponentProps extends Record<string, any> = {}> = InputProps<
19+
number,
20+
NumberSpec<undefined, InputComponentProps>
21+
>;
2122

22-
export type NumberInput = InputType<number, NumberSpec>;
23-
export type NumberIndependentInput = IndependentInputType<number, NumberSpec>;
24-
export type NumberLayout = LayoutType<number, NumberSpec>;
23+
export type NumberIndependentInputProps<
24+
InputComponentProps extends Record<string, any> = {},
25+
LayoutComponentProps extends Record<string, any> = {},
26+
> = IndependentInputProps<number, NumberSpec<undefined, InputComponentProps, LayoutComponentProps>>;
27+
28+
export type NumberLayoutProps<LayoutComponentProps extends Record<string, any> = {}> = LayoutProps<
29+
number,
30+
NumberSpec<undefined, any, LayoutComponentProps>
31+
>;
32+
33+
export type NumberInput<InputComponentProps extends Record<string, any> = {}> = InputType<
34+
number,
35+
NumberSpec<undefined, InputComponentProps>
36+
>;
37+
38+
export type NumberIndependentInput<
39+
InputComponentProps extends Record<string, any> = {},
40+
LayoutComponentProps extends Record<string, any> = {},
41+
> = IndependentInputType<number, NumberSpec<undefined, InputComponentProps, LayoutComponentProps>>;
42+
43+
export type NumberLayout<LayoutComponentProps extends Record<string, any> = {}> = LayoutType<
44+
number,
45+
NumberSpec<undefined, any, LayoutComponentProps>
46+
>;
2547

2648
export type NumberInputEntity = InputEntity<number, NumberSpec>;
2749
export type NumberIndependentInputEntity = IndependentInputEntity<number, NumberSpec>;

src/lib/core/components/Form/types/object.ts

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,40 @@ import {
1616
ValidatorsMap,
1717
} from './';
1818

19-
export type ObjectInputProps = InputProps<FieldObjectValue, ObjectSpec>;
20-
export type ObjectIndependentInputProps = IndependentInputProps<FieldObjectValue, ObjectSpec>;
21-
export type ObjectLayoutProps = LayoutProps<FieldObjectValue, ObjectSpec>;
19+
export type ObjectInputProps<InputComponentProps extends Record<string, any> = {}> = InputProps<
20+
FieldObjectValue,
21+
ObjectSpec<undefined, InputComponentProps>
22+
>;
23+
export type ObjectIndependentInputProps<
24+
InputComponentProps extends Record<string, any> = {},
25+
LayoutComponentProps extends Record<string, any> = {},
26+
> = IndependentInputProps<
27+
FieldObjectValue,
28+
ObjectSpec<undefined, InputComponentProps, LayoutComponentProps>
29+
>;
2230

23-
export type ObjectInput = InputType<FieldObjectValue, ObjectSpec>;
24-
export type ObjectIndependentInput = IndependentInputType<FieldObjectValue, ObjectSpec>;
25-
export type ObjectLayout = LayoutType<FieldObjectValue, ObjectSpec>;
31+
export type ObjectLayoutProps<LayoutComponentProps extends Record<string, any> = {}> = LayoutProps<
32+
FieldObjectValue,
33+
ObjectSpec<undefined, any, LayoutComponentProps>
34+
>;
35+
36+
export type ObjectInput<InputComponentProps extends Record<string, any> = {}> = InputType<
37+
FieldObjectValue,
38+
ObjectSpec<undefined, InputComponentProps>
39+
>;
40+
41+
export type ObjectIndependentInput<
42+
InputComponentProps extends Record<string, any> = {},
43+
LayoutComponentProps extends Record<string, any> = {},
44+
> = IndependentInputType<
45+
FieldObjectValue,
46+
ObjectSpec<undefined, InputComponentProps, LayoutComponentProps>
47+
>;
48+
49+
export type ObjectLayout<LayoutComponentProps extends Record<string, any> = {}> = LayoutType<
50+
FieldObjectValue,
51+
ObjectSpec<undefined, any, LayoutComponentProps>
52+
>;
2653

2754
export type ObjectInputEntity = InputEntity<FieldObjectValue, ObjectSpec>;
2855
export type ObjectIndependentInputEntity = IndependentInputEntity<FieldObjectValue, ObjectSpec>;

src/lib/core/components/Form/types/string.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,35 @@ import {
1515
ValidatorsMap,
1616
} from './';
1717

18-
export type StringInputProps = InputProps<string, StringSpec>;
19-
export type StringIndependentInputProps = IndependentInputProps<string, StringSpec>;
20-
export type StringLayoutProps = LayoutProps<string, StringSpec>;
18+
export type StringInputProps<InputComponentProps extends Record<string, any> = {}> = InputProps<
19+
string,
20+
StringSpec<undefined, InputComponentProps>
21+
>;
2122

22-
export type StringInput = InputType<string, StringSpec>;
23-
export type StringIndependentInput = IndependentInputType<string, StringSpec>;
24-
export type StringLayout = LayoutType<string, StringSpec>;
23+
export type StringIndependentInputProps<
24+
InputComponentProps extends Record<string, any> = {},
25+
LayoutComponentProps extends Record<string, any> = {},
26+
> = IndependentInputProps<string, StringSpec<undefined, InputComponentProps, LayoutComponentProps>>;
27+
28+
export type StringLayoutProps<LayoutComponentProps extends Record<string, any> = {}> = LayoutProps<
29+
string,
30+
StringSpec<undefined, any, LayoutComponentProps>
31+
>;
32+
33+
export type StringInput<InputComponentProps extends Record<string, any> = {}> = InputType<
34+
string,
35+
StringSpec<undefined, InputComponentProps>
36+
>;
37+
38+
export type StringIndependentInput<
39+
InputComponentProps extends Record<string, any> = {},
40+
LayoutComponentProps extends Record<string, any> = {},
41+
> = IndependentInputType<string, StringSpec<undefined, InputComponentProps, LayoutComponentProps>>;
42+
43+
export type StringLayout<LayoutComponentProps extends Record<string, any> = {}> = LayoutType<
44+
string,
45+
StringSpec<undefined, any, LayoutComponentProps>
46+
>;
2547

2648
export type StringInputEntity = InputEntity<string, StringSpec>;
2749
export type StringIndependentInputEntity = IndependentInputEntity<string, StringSpec>;

src/lib/core/types/specs.ts

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import {ReadAsMethod, SpecTypes} from '../constants';
55

66
import {ArrayValue, ObjectValue} from './';
77

8-
export interface ArraySpec<LinkType = any> {
8+
//TODO: Need to move on InputComponentProps extends Record<string, any> | undefined = undefined;
9+
export interface ArraySpec<
10+
LinkType = any,
11+
InputComponentProps extends Record<string, any> = {},
12+
LayoutComponentProps extends Record<string, any> = {},
13+
> {
914
defaultValue?: ArrayValue;
1015
type: SpecTypes.Array;
1116
required?: boolean;
@@ -36,10 +41,16 @@ export interface ArraySpec<LinkType = any> {
3641
filterPlaceholder?: string;
3742
meta?: Record<string, string>;
3843
};
44+
inputProps?: InputComponentProps;
45+
layoutProps?: LayoutComponentProps;
3946
};
4047
}
4148

42-
export interface BooleanSpec<LinkType = any> {
49+
export interface BooleanSpec<
50+
LinkType = any,
51+
InputComponentProps extends Record<string, any> = {},
52+
LayoutComponentProps extends Record<string, any> = {},
53+
> {
4354
defaultValue?: boolean;
4455
type: SpecTypes.Boolean;
4556
required?: boolean;
@@ -53,10 +64,16 @@ export interface BooleanSpec<LinkType = any> {
5364
layoutOpen?: boolean;
5465
link?: LinkType;
5566
hidden?: boolean;
67+
inputProps?: InputComponentProps;
68+
layoutProps?: LayoutComponentProps;
5669
};
5770
}
5871

59-
export interface NumberSpec<LinkType = any> {
72+
export interface NumberSpec<
73+
LinkType = any,
74+
InputComponentProps extends Record<string, any> = {},
75+
LayoutComponentProps extends Record<string, any> = {},
76+
> {
6077
defaultValue?: number;
6178
type: SpecTypes.Number;
6279
required?: boolean;
@@ -75,10 +92,16 @@ export interface NumberSpec<LinkType = any> {
7592
placeholder?: string;
7693
copy?: boolean;
7794
hidden?: boolean;
95+
inputProps?: InputComponentProps;
96+
layoutProps?: LayoutComponentProps;
7897
};
7998
}
8099

81-
export interface ObjectSpec<LinkType = any> {
100+
export interface ObjectSpec<
101+
LinkType = any,
102+
InputComponentProps extends Record<string, any> = {},
103+
LayoutComponentProps extends Record<string, any> = {},
104+
> {
82105
defaultValue?: ObjectValue;
83106
type: SpecTypes.Object;
84107
required?: boolean;
@@ -99,10 +122,16 @@ export interface ObjectSpec<LinkType = any> {
99122
};
100123
placeholder?: string;
101124
hidden?: boolean;
125+
inputProps?: InputComponentProps;
126+
layoutProps?: LayoutComponentProps;
102127
};
103128
}
104129

105-
export interface StringSpec<LinkType = any> {
130+
export interface StringSpec<
131+
LinkType = any,
132+
InputComponentProps extends Record<string, any> = {},
133+
LayoutComponentProps extends Record<string, any> = {},
134+
> {
106135
defaultValue?: string;
107136
type: SpecTypes.String;
108137
required?: boolean;
@@ -149,6 +178,8 @@ export interface StringSpec<LinkType = any> {
149178
filterPlaceholder?: string;
150179
meta?: Record<string, string>;
151180
};
181+
inputProps?: InputComponentProps;
182+
layoutProps?: LayoutComponentProps;
152183
generateRandomValueButton?: boolean;
153184
};
154185
}

0 commit comments

Comments
 (0)