Skip to content

Commit 6fd89c1

Browse files
author
Ruben van Leeuwen
committed
1714: Adds component matcher file
1 parent 262af2d commit 6fd89c1

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { TextField } from '@/components/fields';
2+
import { getMatcher } from '@/core/helper';
3+
import type {
4+
ElementMatch,
5+
Properties,
6+
PydanticFormComponents,
7+
PydanticFormsContextConfig,
8+
} from '@/types';
9+
10+
export const componentMatcher = (
11+
properties: Properties,
12+
customComponentMatcher: PydanticFormsContextConfig['componentMatcher'],
13+
): PydanticFormComponents => {
14+
const matcher = getMatcher(customComponentMatcher);
15+
16+
const components: PydanticFormComponents = Object.entries(properties).map(
17+
([, pydanticFormField]) => {
18+
const matchedComponent = matcher(pydanticFormField);
19+
20+
const ElementMatch: ElementMatch = matchedComponent
21+
? matchedComponent.ElementMatch
22+
: {
23+
Element: TextField,
24+
isControlledElement: true,
25+
};
26+
27+
// Defaults to textField when there are no matches
28+
return {
29+
Element: ElementMatch,
30+
pydanticFormField: pydanticFormField,
31+
};
32+
},
33+
);
34+
35+
return components;
36+
};

0 commit comments

Comments
 (0)