Skip to content

Commit 606f659

Browse files
committed
add Checkbox field
1 parent 703085d commit 606f659

File tree

6 files changed

+41
-3
lines changed

6 files changed

+41
-3
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ __pycache__/
1818
.vscode
1919
test
2020
.idea
21-
todo.md
21+
todo.md
22+
venv

backend/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from pydantic_forms.exceptions import FormException
2222
from pydantic_forms.core import FormPage as PydanticFormsFormPage
2323
from pydantic_forms.types import JSON
24-
from pydantic_forms.validators import LongText, Label, Divider, Hidden
24+
from pydantic_forms.validators import LongText, Label, Divider
2525

2626
# Choice,
2727
# CustomerId,
@@ -86,7 +86,7 @@ class TestForm(FormPage):
8686
textArea: LongText
8787
divider: Divider
8888
label: Label = "Label"
89-
hidden: Hidden = "Hidden"
89+
checkbox: bool = True
9090

9191
form_data_1 = yield TestForm
9292

frontend/packages/pydantic-forms/src/components/defaultComponentMatchers.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
IntegerField,
1010
LabelField,
1111
TextAreaField,
12+
CheckboxField
1213
} from '@/components/fields';
1314
import {
1415
PydanticComponentMatcher,
@@ -83,6 +84,19 @@ const defaultComponentMatchers: PydanticComponentMatcher[] = [
8384
);
8485
},
8586
},
87+
{
88+
id: 'checkbox',
89+
ElementMatch: {
90+
Element: CheckboxField,
91+
isControlledElement: true,
92+
},
93+
matcher(field) {
94+
return (
95+
field.type === PydanticFormFieldType.BOOLEAN
96+
);
97+
}
98+
},
8699
];
100+
87101
// If nothing matches, it defaults to Text field in the mapToComponent function
88102
export default defaultComponentMatchers;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { PydanticFormControlledElementProps } from '@/types';
2+
3+
export const CheckboxField = ({
4+
onChange,
5+
onBlur,
6+
value,
7+
name,
8+
pydanticFormField
9+
}: PydanticFormControlledElementProps) => {
10+
console.log(pydanticFormField);
11+
return (
12+
<input
13+
type="checkbox"
14+
checked={value}
15+
onChange={() => onChange(!value)}
16+
onBlur={onBlur}
17+
name={name}
18+
/>
19+
);
20+
};

frontend/packages/pydantic-forms/src/components/fields/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export * from './FormRow';
33
export * from './TextField';
44
export * from './IntegerField';
55
export * from './TextAreaField';
6+
export * from './CheckboxField';
67
export * from './LabelField';
78
export * from './DividerField';
89
export * from './HiddenField';

frontend/packages/pydantic-forms/src/core/mapFieldToComponent.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,11 @@ export const getComponentMatcher = (
8484
return matcher(field);
8585
});
8686

87+
console.log('Matched component __' + matchedComponent?.matcher)
8788
if (matchedComponent) return matchedComponent;
8889

8990
// Defaults to textField when there are no matches
91+
console.log('No match found for field ' + field.id);
9092
return {
9193
id: 'textfield',
9294
ElementMatch: {

0 commit comments

Comments
 (0)