graphql-form 0.1.6 → 0.1.8
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/lib/index.d.ts +1 -6
- package/lib/index.js +1 -6
- package/lib/index.js.map +1 -1
- package/lib/models.d.ts +34 -77
- package/lib/models.js.map +1 -1
- package/lib/render/fields.d.ts +3 -0
- package/lib/{renderer → render}/fields.js +10 -15
- package/lib/render/fields.js.map +1 -0
- package/lib/render/index.d.ts +4 -0
- package/lib/render/index.js +56 -0
- package/lib/render/index.js.map +1 -0
- package/package.json +4 -4
- package/src/index.tsx +1 -6
- package/src/models.ts +43 -100
- package/src/render/fields.tsx +58 -0
- package/src/render/index.tsx +65 -0
- package/tsconfig.json +3 -2
- package/lib/BuiltInFields/InjectedValue.d.ts +0 -5
- package/lib/BuiltInFields/InjectedValue.js +0 -49
- package/lib/BuiltInFields/InjectedValue.js.map +0 -1
- package/lib/FormBuilder.d.ts +0 -3
- package/lib/FormBuilder.js +0 -78
- package/lib/FormBuilder.js.map +0 -1
- package/lib/FormDisplayer.d.ts +0 -3
- package/lib/FormDisplayer.js +0 -76
- package/lib/FormDisplayer.js.map +0 -1
- package/lib/FormToCode/gql/formToGql.spec.d.ts +0 -1
- package/lib/FormToCode/gql/formToGql.spec.js +0 -50
- package/lib/FormToCode/gql/formToGql.spec.js.map +0 -1
- package/lib/FormToCode/gql/index.d.ts +0 -6
- package/lib/FormToCode/gql/index.js +0 -12
- package/lib/FormToCode/gql/index.js.map +0 -1
- package/lib/FormToCode/gql/reducers.d.ts +0 -6
- package/lib/FormToCode/gql/reducers.js +0 -56
- package/lib/FormToCode/gql/reducers.js.map +0 -1
- package/lib/FormToCode/gql/resolvers.d.ts +0 -4
- package/lib/FormToCode/gql/resolvers.js +0 -54
- package/lib/FormToCode/gql/resolvers.js.map +0 -1
- package/lib/FormToCode/index.d.ts +0 -3
- package/lib/FormToCode/index.js +0 -20
- package/lib/FormToCode/index.js.map +0 -1
- package/lib/FormToCode/models.d.ts +0 -8
- package/lib/FormToCode/models.js +0 -3
- package/lib/FormToCode/models.js.map +0 -1
- package/lib/FormToCode/zeus/formToZeus.spec.d.ts +0 -1
- package/lib/FormToCode/zeus/formToZeus.spec.js +0 -59
- package/lib/FormToCode/zeus/formToZeus.spec.js.map +0 -1
- package/lib/FormToCode/zeus/index.d.ts +0 -6
- package/lib/FormToCode/zeus/index.js +0 -12
- package/lib/FormToCode/zeus/index.js.map +0 -1
- package/lib/FormToCode/zeus/resolvers.d.ts +0 -5
- package/lib/FormToCode/zeus/resolvers.js +0 -57
- package/lib/FormToCode/zeus/resolvers.js.map +0 -1
- package/lib/__tests__/consts.d.ts +0 -11
- package/lib/__tests__/consts.js +0 -38
- package/lib/__tests__/consts.js.map +0 -1
- package/lib/__tests__/index.d.ts +0 -2
- package/lib/__tests__/index.js +0 -19
- package/lib/__tests__/index.js.map +0 -1
- package/lib/__tests__/testUtils.d.ts +0 -1
- package/lib/__tests__/testUtils.js +0 -8
- package/lib/__tests__/testUtils.js.map +0 -1
- package/lib/createWidget.d.ts +0 -4
- package/lib/createWidget.js +0 -19
- package/lib/createWidget.js.map +0 -1
- package/lib/outsideUse.d.ts +0 -11
- package/lib/outsideUse.js +0 -147
- package/lib/outsideUse.js.map +0 -1
- package/lib/renderer/fields.d.ts +0 -3
- package/lib/renderer/fields.js.map +0 -1
- package/lib/renderer/index.d.ts +0 -3
- package/lib/renderer/index.js +0 -74
- package/lib/renderer/index.js.map +0 -1
- package/src/BuiltInFields/InjectedValue.tsx +0 -14
- package/src/FormBuilder.tsx +0 -58
- package/src/FormDisplayer.tsx +0 -48
- package/src/FormToCode/gql/formToGql.spec.ts +0 -75
- package/src/FormToCode/gql/index.ts +0 -9
- package/src/FormToCode/gql/reducers.ts +0 -50
- package/src/FormToCode/gql/resolvers.ts +0 -51
- package/src/FormToCode/index.ts +0 -3
- package/src/FormToCode/models.ts +0 -9
- package/src/FormToCode/zeus/formToZeus.spec.ts +0 -84
- package/src/FormToCode/zeus/index.ts +0 -9
- package/src/FormToCode/zeus/resolvers.tsx +0 -58
- package/src/createWidget.tsx +0 -8
- package/src/outsideUse.tsx +0 -156
- package/src/renderer/fields.tsx +0 -60
- package/src/renderer/index.tsx +0 -53
@@ -1,75 +0,0 @@
|
|
1
|
-
import { formToGql } from './index';
|
2
|
-
import {
|
3
|
-
extenedFields,
|
4
|
-
fields,
|
5
|
-
nodes,
|
6
|
-
replSpace,
|
7
|
-
singleNode,
|
8
|
-
fieldsNodeWithArgs,
|
9
|
-
fieldsNodeWithArgsWithInput,
|
10
|
-
nodeWithArgs,
|
11
|
-
} from '../../__tests__';
|
12
|
-
|
13
|
-
it('tranform form to Gql, typeNode, No args', () => {
|
14
|
-
// Arrange
|
15
|
-
const expectedValue = 'test';
|
16
|
-
// Act
|
17
|
-
const result = formToGql({ nodes: singleNode, fields });
|
18
|
-
// Assert
|
19
|
-
const matchExact = replSpace(result);
|
20
|
-
matchExact(expectedValue);
|
21
|
-
});
|
22
|
-
|
23
|
-
it('tranform form to Gql, typeNode, With args', () => {
|
24
|
-
// Arrange
|
25
|
-
const expectedValue = `testInput(
|
26
|
-
test: 5
|
27
|
-
)`;
|
28
|
-
|
29
|
-
// Act
|
30
|
-
const result = formToGql({ nodes, fields: extenedFields });
|
31
|
-
|
32
|
-
// Assert
|
33
|
-
const matchExact = replSpace(result);
|
34
|
-
matchExact(expectedValue);
|
35
|
-
});
|
36
|
-
|
37
|
-
it('tranform form to Gql, typeNode, fieldsNodeWithArgs', () => {
|
38
|
-
// Arrange
|
39
|
-
const expectedValue = `testInput(
|
40
|
-
test: 5
|
41
|
-
)`;
|
42
|
-
|
43
|
-
// Act
|
44
|
-
const result = formToGql({ nodes, fields: fieldsNodeWithArgs });
|
45
|
-
|
46
|
-
// Assert
|
47
|
-
const matchExact = replSpace(result);
|
48
|
-
matchExact(expectedValue);
|
49
|
-
});
|
50
|
-
|
51
|
-
it('tranform form to Gql, typeNode, fieldsNodeWithArgsWithInput', () => {
|
52
|
-
// Arrange
|
53
|
-
const expectedValue = 'testInput(testInput:[objectObject])';
|
54
|
-
|
55
|
-
// Act
|
56
|
-
const result = formToGql({ nodes, fields: fieldsNodeWithArgsWithInput });
|
57
|
-
|
58
|
-
// Assert
|
59
|
-
const matchExact = replSpace(result);
|
60
|
-
matchExact(expectedValue);
|
61
|
-
});
|
62
|
-
|
63
|
-
it('tranform form to Gql, nodeWithArgs, fieldsNodeWithArgsWithInput', () => {
|
64
|
-
// Arrange
|
65
|
-
const expectedValue = 'testInput(testInput:[objectObject])';
|
66
|
-
|
67
|
-
// Act
|
68
|
-
const result = formToGql({ nodes: [nodeWithArgs], fields: fieldsNodeWithArgsWithInput });
|
69
|
-
|
70
|
-
// Assert
|
71
|
-
const matchExact = replSpace(result);
|
72
|
-
matchExact(expectedValue);
|
73
|
-
});
|
74
|
-
|
75
|
-
export {};
|
@@ -1,9 +0,0 @@
|
|
1
|
-
import { fieldsToReductor, reduceQl } from './reducers';
|
2
|
-
import { FormObject } from '@/models';
|
3
|
-
import { ParserField } from 'graphql-js-tree';
|
4
|
-
|
5
|
-
export const formToGql = ({ fields, nodes }: { nodes: ParserField[]; fields: Record<string, FormObject> }) => {
|
6
|
-
const reducedQl = fieldsToReductor(fields);
|
7
|
-
const q = reduceQl(reducedQl, nodes);
|
8
|
-
return q;
|
9
|
-
};
|
@@ -1,50 +0,0 @@
|
|
1
|
-
import { resolveQlValue } from './resolvers';
|
2
|
-
import { Reductor, ReductorValue } from '@/FormToCode/models';
|
3
|
-
import { FormObject } from '@/models';
|
4
|
-
import { ParserField } from 'graphql-js-tree';
|
5
|
-
|
6
|
-
export const fieldsToReductor = (toggledFields: Record<string, FormObject>): Reductor =>
|
7
|
-
Object.entries(toggledFields).reduce((a, [key, value]) => {
|
8
|
-
const pathElements = key
|
9
|
-
.replace('... on', '___on')
|
10
|
-
.split('.')
|
11
|
-
.map((e) => e.replace('___on', '... on'));
|
12
|
-
let start = a;
|
13
|
-
pathElements.forEach((el, index) => {
|
14
|
-
if (index === 0 && !!value.node.type.operations?.length) {
|
15
|
-
el = value.node.type.operations[0];
|
16
|
-
}
|
17
|
-
start[el] = start[el] || {};
|
18
|
-
start[el].reductor = start[el].reductor || {};
|
19
|
-
if (index === pathElements.length - 1) {
|
20
|
-
if (value.value && Object.keys(value.value).length > 0) {
|
21
|
-
start[el].value = value.value;
|
22
|
-
}
|
23
|
-
}
|
24
|
-
start = start[el].reductor;
|
25
|
-
});
|
26
|
-
return a;
|
27
|
-
}, {} as Reductor);
|
28
|
-
|
29
|
-
export const determineArgumentsQl = (v: ReductorValue, nodes: ParserField[], tabs = '') => {
|
30
|
-
if (v.value && Object.keys(v.value).length > 0) {
|
31
|
-
const resolvedValue = resolveQlValue(v.value, nodes, tabs);
|
32
|
-
if (resolvedValue) {
|
33
|
-
return `(${resolvedValue}\n${tabs})`;
|
34
|
-
}
|
35
|
-
}
|
36
|
-
return '';
|
37
|
-
};
|
38
|
-
|
39
|
-
export const reduceQl = (o: Reductor, nodes: ParserField[], tabs = ''): string => {
|
40
|
-
return Object.entries(o)
|
41
|
-
.map(([k, v]) => {
|
42
|
-
const kName = `${k}${determineArgumentsQl(v, nodes, tabs)}`;
|
43
|
-
const kObject =
|
44
|
-
Object.keys(v.reductor).length > 0
|
45
|
-
? `${tabs}${kName}{\n${reduceQl(v.reductor, nodes, tabs + '\t')}${tabs}}\n`
|
46
|
-
: `${tabs}${kName}\n`;
|
47
|
-
return kObject;
|
48
|
-
})
|
49
|
-
.join('');
|
50
|
-
};
|
@@ -1,51 +0,0 @@
|
|
1
|
-
import { FormObject, FormValue } from '@/models';
|
2
|
-
import { ParserField, getTypeName, TypeDefinition, ScalarTypes } from 'graphql-js-tree';
|
3
|
-
|
4
|
-
export const resolveSingularValue = (v: FormObject, nodes: ParserField[], tabs = ''): string => {
|
5
|
-
if (v.value === null) {
|
6
|
-
return 'null';
|
7
|
-
}
|
8
|
-
if (Array.isArray(v.value)) {
|
9
|
-
return `[${v.value
|
10
|
-
.map((subVal) => resolveSingularValue({ node: v.node, value: (subVal as FormObject).value }, nodes, tabs))
|
11
|
-
.join(', ')}]`;
|
12
|
-
}
|
13
|
-
const seekNode = nodes.find((n) => n.name === getTypeName(v.node.type.fieldType));
|
14
|
-
if (seekNode?.data.type === TypeDefinition.EnumTypeDefinition) {
|
15
|
-
return v.value as string;
|
16
|
-
}
|
17
|
-
if (seekNode?.data.type === TypeDefinition.ScalarTypeDefinition) {
|
18
|
-
return v.value as string;
|
19
|
-
}
|
20
|
-
if (seekNode) {
|
21
|
-
return `{${resolveQlValue(v.value, nodes, tabs + '\t')}\n${tabs}\t}`;
|
22
|
-
}
|
23
|
-
return getTypeName(v.node.type.fieldType) === ScalarTypes.String ? `"${v.value}"` : `${v.value}`;
|
24
|
-
};
|
25
|
-
|
26
|
-
export const resolveQlValue = (v: FormValue | undefined, nodes: ParserField[], tabs = '') => {
|
27
|
-
if (typeof v === 'undefined' || v === null) {
|
28
|
-
return '';
|
29
|
-
}
|
30
|
-
return Object.entries(v).reduce((a, [key, v]: [key: string, value: FormObject]) => {
|
31
|
-
if (typeof v.value === 'undefined' || v.value === '') {
|
32
|
-
return a;
|
33
|
-
}
|
34
|
-
if (Array.isArray(v.value) && v.value.length === 0) {
|
35
|
-
return a;
|
36
|
-
}
|
37
|
-
if (
|
38
|
-
typeof v.value === 'object' &&
|
39
|
-
v.value !== null &&
|
40
|
-
!Object.values(v.value).some(
|
41
|
-
(val) =>
|
42
|
-
typeof val === 'object' && val !== null && 'value' in val && (!!val.value || val.value === false),
|
43
|
-
)
|
44
|
-
) {
|
45
|
-
return a;
|
46
|
-
}
|
47
|
-
const resolvedValue = resolveSingularValue(v, nodes, tabs);
|
48
|
-
a = `${a}\n\t${tabs}${key}: ${resolvedValue}`;
|
49
|
-
return a;
|
50
|
-
}, '');
|
51
|
-
};
|
package/src/FormToCode/index.ts
DELETED
package/src/FormToCode/models.ts
DELETED
@@ -1,84 +0,0 @@
|
|
1
|
-
import { formToZeus } from './index';
|
2
|
-
import {
|
3
|
-
extenedFields,
|
4
|
-
nodes,
|
5
|
-
replSpace,
|
6
|
-
singleNode,
|
7
|
-
fields,
|
8
|
-
fieldsNodeWithArgs,
|
9
|
-
fieldsNodeWithArgsWithInput,
|
10
|
-
} from '../../__tests__';
|
11
|
-
|
12
|
-
it('tranform form to zeus, typeNode, No args', () => {
|
13
|
-
// Arrange
|
14
|
-
const expectedValue = 'api("undefined")({})';
|
15
|
-
|
16
|
-
// Act
|
17
|
-
const result = formToZeus({ nodes: singleNode, fields });
|
18
|
-
|
19
|
-
// Assert
|
20
|
-
const matchExact = replSpace(result);
|
21
|
-
matchExact(expectedValue);
|
22
|
-
});
|
23
|
-
|
24
|
-
it('tranform form to zeus, typeNode, With args', () => {
|
25
|
-
// Arrange
|
26
|
-
const expectedValue = 'api("undefined")({})';
|
27
|
-
|
28
|
-
// Act
|
29
|
-
const result = formToZeus({ nodes: nodes, fields });
|
30
|
-
|
31
|
-
// Assert
|
32
|
-
const matchExact = replSpace(result);
|
33
|
-
matchExact(expectedValue);
|
34
|
-
});
|
35
|
-
|
36
|
-
it('tranform form to zeus, inputNode, With args', () => {
|
37
|
-
// Arrange
|
38
|
-
const expectedValue = 'testInput:[{test:5},true]';
|
39
|
-
|
40
|
-
// Act
|
41
|
-
const result = formToZeus({ nodes: nodes, fields: extenedFields });
|
42
|
-
|
43
|
-
// Assert
|
44
|
-
const matchExact = replSpace(result);
|
45
|
-
matchExact(expectedValue);
|
46
|
-
});
|
47
|
-
|
48
|
-
it('tranform form to zeus, singleTypeNode, No args', () => {
|
49
|
-
// Arrange
|
50
|
-
const expectedValue = 'testInput:[{test:5},true]';
|
51
|
-
|
52
|
-
// Act
|
53
|
-
const result = formToZeus({ nodes: singleNode, fields: extenedFields });
|
54
|
-
|
55
|
-
// Assert
|
56
|
-
const matchExact = replSpace(result);
|
57
|
-
matchExact(expectedValue);
|
58
|
-
});
|
59
|
-
|
60
|
-
it('tranform form to zeus, singleTypeNode, fieldsNodeWithArgs', () => {
|
61
|
-
// Arrange
|
62
|
-
const expectedValue = 'testInput:[{test:5},true]';
|
63
|
-
|
64
|
-
// Act
|
65
|
-
const result = formToZeus({ nodes: singleNode, fields: fieldsNodeWithArgs });
|
66
|
-
|
67
|
-
// Assert
|
68
|
-
const matchExact = replSpace(result);
|
69
|
-
matchExact(expectedValue);
|
70
|
-
});
|
71
|
-
|
72
|
-
it('tranform form to zeus, singleTypeNode, fieldsNodeWithArgsWithInput', () => {
|
73
|
-
// Arrange
|
74
|
-
const expectedValue = 'testInput:[{testInput:[objectObject]},true]';
|
75
|
-
|
76
|
-
// Act
|
77
|
-
const result = formToZeus({ nodes: singleNode, fields: fieldsNodeWithArgsWithInput });
|
78
|
-
|
79
|
-
// Assert
|
80
|
-
const matchExact = replSpace(result);
|
81
|
-
matchExact(expectedValue);
|
82
|
-
});
|
83
|
-
|
84
|
-
export {};
|
@@ -1,9 +0,0 @@
|
|
1
|
-
import { zeusFieldsToReductor, zeusReduceQl } from './resolvers';
|
2
|
-
import { FormObject } from '@/models';
|
3
|
-
import { ParserField } from 'graphql-js-tree';
|
4
|
-
|
5
|
-
export const formToZeus = ({ fields, nodes }: { nodes: ParserField[]; fields: Record<string, FormObject> }) => {
|
6
|
-
const reducedQl = zeusFieldsToReductor(fields);
|
7
|
-
const q = zeusReduceQl(reducedQl, nodes);
|
8
|
-
return q;
|
9
|
-
};
|
@@ -1,58 +0,0 @@
|
|
1
|
-
import { resolveQlValue } from '../gql/resolvers';
|
2
|
-
import { Reductor } from '../models';
|
3
|
-
import { FormObject } from '@/models';
|
4
|
-
import { ParserField } from 'graphql-js-tree';
|
5
|
-
|
6
|
-
export const zeusFieldsToReductor = (toggledFields: Record<string, FormObject>): Reductor => {
|
7
|
-
const r = Object.entries(toggledFields).reduce((a, [key, value]) => {
|
8
|
-
const pathElements = key
|
9
|
-
.replace('... on', '___on')
|
10
|
-
.split('.')
|
11
|
-
.map((e) => e.replace('___on', '... on'));
|
12
|
-
let start = a;
|
13
|
-
pathElements.forEach((el, index) => {
|
14
|
-
if (index === 0 && !!value.node.type.operations?.length) {
|
15
|
-
el = value.node.type.operations[0];
|
16
|
-
return;
|
17
|
-
}
|
18
|
-
start[el] = start[el] || {};
|
19
|
-
start[el].reductor = start[el].reductor || {};
|
20
|
-
if (index === pathElements.length - 1) {
|
21
|
-
if (value.value && Object.keys(value.value).length > 0) {
|
22
|
-
start[el].value = value.value;
|
23
|
-
}
|
24
|
-
}
|
25
|
-
start = start[el].reductor;
|
26
|
-
});
|
27
|
-
return a;
|
28
|
-
}, {} as Reductor);
|
29
|
-
return r;
|
30
|
-
};
|
31
|
-
|
32
|
-
export const zeusReduceQl = (o: Reductor, nodes: ParserField[], tabs = '', level = 0): string => {
|
33
|
-
const lUP = level + 1;
|
34
|
-
return Object.entries(o)
|
35
|
-
.map(([k, v]) => {
|
36
|
-
if (v.value && Object.keys(v.value).length > 0) {
|
37
|
-
return Object.keys(v.reductor).length > 0
|
38
|
-
? `${tabs}${k}:[{${resolveQlValue(v.value, nodes, tabs)}\n${tabs}}, {\n${zeusReduceQl(
|
39
|
-
v.reductor,
|
40
|
-
nodes,
|
41
|
-
tabs + '\t',
|
42
|
-
lUP,
|
43
|
-
)}${tabs}}]\n`
|
44
|
-
: `${tabs}${k}:[{${resolveQlValue(v.value, nodes, tabs)}\n${tabs}}, true]`;
|
45
|
-
}
|
46
|
-
if (level === 0) {
|
47
|
-
return `const result = await api("${
|
48
|
-
nodes.find((n) => n.name === k)?.type.operations?.[0]
|
49
|
-
}")({\n${zeusReduceQl(v.reductor, nodes, tabs + '\t', lUP)}})`;
|
50
|
-
}
|
51
|
-
const kObject =
|
52
|
-
Object.keys(v.reductor).length > 0
|
53
|
-
? `${tabs}${k}:{\n${zeusReduceQl(v.reductor, nodes, tabs + '\t', lUP)}${tabs}}\n`
|
54
|
-
: `${tabs}${k}: true\n`;
|
55
|
-
return kObject;
|
56
|
-
})
|
57
|
-
.join('');
|
58
|
-
};
|
package/src/createWidget.tsx
DELETED
package/src/outsideUse.tsx
DELETED
@@ -1,156 +0,0 @@
|
|
1
|
-
import { Errs, FormFile, FormObject, FormValue, PassedFormProps } from '@/models';
|
2
|
-
import {
|
3
|
-
getTypeName,
|
4
|
-
Options,
|
5
|
-
Parser,
|
6
|
-
ScalarTypes,
|
7
|
-
TypeDefinition,
|
8
|
-
TypeSystemDefinition,
|
9
|
-
ValueDefinition,
|
10
|
-
} from 'graphql-js-tree';
|
11
|
-
|
12
|
-
export const getWidgetFromProps = (props: PassedFormProps) => {
|
13
|
-
const w = props.widgets?.[props.currentPath];
|
14
|
-
const FoundBasicWidget = props.widgetComponents.find((wc) => wc.name === w?.widget);
|
15
|
-
if (!FoundBasicWidget) {
|
16
|
-
const widgetVariant = props.widgetVariants?.find((wv) => wv.name === w?.widget);
|
17
|
-
if (!widgetVariant) {
|
18
|
-
return;
|
19
|
-
}
|
20
|
-
const FoundVariantWidget = props.widgetComponents.find((wc) => wc.name === widgetVariant.widget);
|
21
|
-
if (!FoundVariantWidget) {
|
22
|
-
return;
|
23
|
-
}
|
24
|
-
return {
|
25
|
-
data: {
|
26
|
-
widget: widgetVariant.widget,
|
27
|
-
...widgetVariant.data,
|
28
|
-
},
|
29
|
-
widget: FoundVariantWidget,
|
30
|
-
};
|
31
|
-
}
|
32
|
-
return {
|
33
|
-
data: w,
|
34
|
-
widget: FoundBasicWidget,
|
35
|
-
};
|
36
|
-
};
|
37
|
-
export const getErrorFromProps = (props: PassedFormProps) => {
|
38
|
-
return props.errors?.[props.currentPath];
|
39
|
-
};
|
40
|
-
|
41
|
-
export const graphqlFormUtils = (schema: string) => {
|
42
|
-
const nodes = Parser.parse(schema).nodes;
|
43
|
-
|
44
|
-
const validateValue = (f: FormValue, path: string, pushErrors: (path: string, value: Errs) => void) => {
|
45
|
-
if (
|
46
|
-
f === null ||
|
47
|
-
typeof f === 'string' ||
|
48
|
-
typeof f === 'boolean' ||
|
49
|
-
typeof f === 'number' ||
|
50
|
-
typeof f === 'undefined'
|
51
|
-
) {
|
52
|
-
return;
|
53
|
-
}
|
54
|
-
if (
|
55
|
-
'node' in f &&
|
56
|
-
typeof f.node === 'object' &&
|
57
|
-
f.node !== null &&
|
58
|
-
'args' in f.node &&
|
59
|
-
'data' in f.node &&
|
60
|
-
typeof f.node.data === 'object' &&
|
61
|
-
!!f.node.data &&
|
62
|
-
'type' in f.node.data &&
|
63
|
-
'directives' in f.node
|
64
|
-
) {
|
65
|
-
const { node, value } = f as FormObject;
|
66
|
-
if (node.data.type === ValueDefinition.InputValueDefinition) {
|
67
|
-
if (
|
68
|
-
node.type.fieldType.type === Options.required &&
|
69
|
-
getTypeName(node.type.fieldType) !== ScalarTypes.Boolean
|
70
|
-
) {
|
71
|
-
if (typeof value === 'undefined' || value === '') {
|
72
|
-
pushErrors(path, Errs.REQUIRED);
|
73
|
-
}
|
74
|
-
if (Array.isArray(value)) {
|
75
|
-
for (const v of value) {
|
76
|
-
validateValue(v, path, pushErrors);
|
77
|
-
}
|
78
|
-
}
|
79
|
-
}
|
80
|
-
}
|
81
|
-
}
|
82
|
-
if ('value' in f && !!f.value) {
|
83
|
-
const { value } = f;
|
84
|
-
if (Array.isArray(value)) {
|
85
|
-
value.map((vv) => validateValue(vv, path, pushErrors));
|
86
|
-
return;
|
87
|
-
}
|
88
|
-
if (typeof value === 'object' && !!value) {
|
89
|
-
Object.entries(value).forEach(([k, v]) => validateValue(v as FormObject, `${path}.${k}`, pushErrors));
|
90
|
-
return;
|
91
|
-
}
|
92
|
-
}
|
93
|
-
return;
|
94
|
-
};
|
95
|
-
|
96
|
-
const validateForm = (v: FormFile, errors: Record<Errs, string>) => {
|
97
|
-
const errorDict: Record<string, string> = {};
|
98
|
-
if (!v.forms) return errorDict;
|
99
|
-
Object.entries(v.forms).forEach(([k, val]) => {
|
100
|
-
validateValue(val, k, (p, err) => {
|
101
|
-
errorDict[p] = errors[err];
|
102
|
-
});
|
103
|
-
});
|
104
|
-
return errorDict;
|
105
|
-
};
|
106
|
-
|
107
|
-
const eraseValue = (f: FormObject): FormObject => {
|
108
|
-
const { value, node } = f;
|
109
|
-
|
110
|
-
const seekNode = nodes.find((n) => n.name === getTypeName(node.type.fieldType));
|
111
|
-
if (
|
112
|
-
!seekNode ||
|
113
|
-
seekNode?.data.type === TypeDefinition.EnumTypeDefinition ||
|
114
|
-
seekNode?.data.type === TypeDefinition.ScalarTypeDefinition
|
115
|
-
) {
|
116
|
-
return {
|
117
|
-
...f,
|
118
|
-
value: undefined,
|
119
|
-
};
|
120
|
-
}
|
121
|
-
if ((seekNode || node.data.type === TypeSystemDefinition.FieldDefinition) && value) {
|
122
|
-
return {
|
123
|
-
...f,
|
124
|
-
value: Object.fromEntries(Object.entries(value).map(([k, v]) => [k, eraseValue(v as FormObject)])),
|
125
|
-
};
|
126
|
-
}
|
127
|
-
return {
|
128
|
-
...f,
|
129
|
-
value: undefined,
|
130
|
-
};
|
131
|
-
};
|
132
|
-
|
133
|
-
const eraseForm = (v: FormFile) => {
|
134
|
-
if (!v.forms) return v;
|
135
|
-
const forms = Object.entries(v.forms)
|
136
|
-
.map(([k, val]) => {
|
137
|
-
return [k, eraseValue(val)] as const;
|
138
|
-
})
|
139
|
-
.reduce(
|
140
|
-
(a, [k, val]) => ({
|
141
|
-
...a,
|
142
|
-
[k]: val,
|
143
|
-
}),
|
144
|
-
{} as FormFile['forms'],
|
145
|
-
);
|
146
|
-
return {
|
147
|
-
...v,
|
148
|
-
forms,
|
149
|
-
};
|
150
|
-
};
|
151
|
-
return {
|
152
|
-
eraseForm,
|
153
|
-
validateForm,
|
154
|
-
validateValue,
|
155
|
-
};
|
156
|
-
};
|
package/src/renderer/fields.tsx
DELETED
@@ -1,60 +0,0 @@
|
|
1
|
-
import { PassedFormProps } from '@/models';
|
2
|
-
import { getWidgetFromProps } from '@/outsideUse';
|
3
|
-
import { getTypeName, Options, ScalarTypes, TypeDefinition, TypeSystemDefinition } from 'graphql-js-tree';
|
4
|
-
import React from 'react';
|
5
|
-
|
6
|
-
export const Fields: React.FC<PassedFormProps> = (props) => {
|
7
|
-
const {
|
8
|
-
nodes,
|
9
|
-
f,
|
10
|
-
components: { ArrayField, ObjectField },
|
11
|
-
} = props;
|
12
|
-
const seekNode = nodes.find((n) => n.name === getTypeName(f.type.fieldType));
|
13
|
-
const isInput = seekNode?.data.type === TypeDefinition.InputObjectTypeDefinition;
|
14
|
-
if (f.data.type === TypeSystemDefinition.FieldDefinition) {
|
15
|
-
return <ObjectField {...props} />;
|
16
|
-
}
|
17
|
-
if (f.type.fieldType.type === Options.array) {
|
18
|
-
return <ArrayField {...props} />;
|
19
|
-
}
|
20
|
-
if (isInput) {
|
21
|
-
return <ObjectField {...props} f={seekNode} />;
|
22
|
-
}
|
23
|
-
const w = getWidgetFromProps(props);
|
24
|
-
if (w) {
|
25
|
-
const {
|
26
|
-
data,
|
27
|
-
widget: { Component },
|
28
|
-
} = w;
|
29
|
-
return <Component {...props} widgetData={data} />;
|
30
|
-
}
|
31
|
-
return <ScalarField {...props} />;
|
32
|
-
};
|
33
|
-
const ScalarField: React.FC<PassedFormProps> = (props) => {
|
34
|
-
const {
|
35
|
-
f,
|
36
|
-
nodes,
|
37
|
-
children,
|
38
|
-
components: { BooleanField, NumberField, EnumField, UniversalField },
|
39
|
-
} = props;
|
40
|
-
const typeName = getTypeName(f.type.fieldType);
|
41
|
-
const seekNode = nodes.find((n) => n.name === getTypeName(f.type.fieldType));
|
42
|
-
if (
|
43
|
-
typeName === ScalarTypes.String ||
|
44
|
-
typeName === ScalarTypes.ID ||
|
45
|
-
seekNode?.data.type === TypeDefinition.ScalarTypeDefinition
|
46
|
-
) {
|
47
|
-
return <UniversalField {...props} />;
|
48
|
-
}
|
49
|
-
|
50
|
-
if (typeName === ScalarTypes.Float || typeName === ScalarTypes.Int) {
|
51
|
-
return <NumberField {...props} />;
|
52
|
-
}
|
53
|
-
if (typeName === ScalarTypes.Boolean) {
|
54
|
-
return <BooleanField {...props} />;
|
55
|
-
}
|
56
|
-
if (seekNode?.data.type === TypeDefinition.EnumTypeDefinition) {
|
57
|
-
return <EnumField {...props} />;
|
58
|
-
}
|
59
|
-
return <>{children}</>;
|
60
|
-
};
|
package/src/renderer/index.tsx
DELETED
@@ -1,53 +0,0 @@
|
|
1
|
-
import { InjectedValue } from '@/BuiltInFields/InjectedValue';
|
2
|
-
import { PassedFormProps } from '@/models';
|
3
|
-
import { Fields } from '@/renderer/fields';
|
4
|
-
import { TypeSystemDefinition, Options, getTypeName, TypeDefinition } from 'graphql-js-tree';
|
5
|
-
import React, { useState } from 'react';
|
6
|
-
|
7
|
-
export const Renderer: React.FC<PassedFormProps> = (props) => {
|
8
|
-
const {
|
9
|
-
formObject,
|
10
|
-
f,
|
11
|
-
nodes,
|
12
|
-
presetValues,
|
13
|
-
components: { NullField, FormLabel, FormField },
|
14
|
-
} = props;
|
15
|
-
const { children, ...allProps } = props;
|
16
|
-
const seekNode = nodes.find((n) => n.name === getTypeName(f.type.fieldType));
|
17
|
-
const isInput = seekNode?.data.type === TypeDefinition.InputObjectTypeDefinition;
|
18
|
-
const canBeOpened = props.f.data.type === TypeSystemDefinition.FieldDefinition || isInput;
|
19
|
-
const [open, setOpen] = useState(props.f.data.type === TypeSystemDefinition.FieldDefinition || props.required);
|
20
|
-
if (f.type.fieldType.type === Options.required) {
|
21
|
-
return (
|
22
|
-
<Renderer
|
23
|
-
{...props}
|
24
|
-
f={{
|
25
|
-
...f,
|
26
|
-
type: { ...f.type, fieldType: { ...f.type.fieldType.nest } },
|
27
|
-
}}
|
28
|
-
required={true}
|
29
|
-
/>
|
30
|
-
);
|
31
|
-
}
|
32
|
-
const hasOverride = presetValues?.[props.currentPath];
|
33
|
-
if (hasOverride) {
|
34
|
-
return (
|
35
|
-
<InjectedValue
|
36
|
-
{...props}
|
37
|
-
widgetData={{
|
38
|
-
value: hasOverride,
|
39
|
-
}}
|
40
|
-
/>
|
41
|
-
);
|
42
|
-
}
|
43
|
-
return (
|
44
|
-
<FormField {...allProps}>
|
45
|
-
<FormLabel {...allProps} open={open} setOpen={setOpen} children={children} />
|
46
|
-
{formObject.value === null ? (
|
47
|
-
<NullField {...allProps} />
|
48
|
-
) : (
|
49
|
-
<>{(!canBeOpened || open) && <Fields {...allProps} />}</>
|
50
|
-
)}
|
51
|
-
</FormField>
|
52
|
-
);
|
53
|
-
};
|