Skip to content

Commit 703085d

Browse files
authored
Merge pull request #21 from workfloworchestrator/1694-label-and-divider
1694 label and divider
2 parents b418b39 + abf2902 commit 703085d

File tree

22 files changed

+650
-541
lines changed

22 files changed

+650
-541
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ __pycache__/
1818
.vscode
1919
test
2020
.idea
21+
todo.md

backend/main.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
from dataclasses import dataclass
22
from typing import Annotated, ClassVar, Iterator
3-
from annotated_types import SLOTS, BaseMetadata, GroupedMetadata
3+
from annotated_types import (
4+
SLOTS,
5+
BaseMetadata,
6+
GroupedMetadata,
7+
Ge,
8+
Le,
9+
MultipleOf,
10+
Predicate,
11+
doc,
12+
)
13+
414
from fastapi import FastAPI
515
from fastapi.middleware.cors import CORSMiddleware
616

@@ -11,6 +21,14 @@
1121
from pydantic_forms.exceptions import FormException
1222
from pydantic_forms.core import FormPage as PydanticFormsFormPage
1323
from pydantic_forms.types import JSON
24+
from pydantic_forms.validators import LongText, Label, Divider, Hidden
25+
26+
# Choice,
27+
# CustomerId,
28+
# DisplaySubscription,
29+
# ListOfOne,
30+
# ListOfTwo,
31+
# migration_summary
1432

1533

1634
class FormPage(PydanticFormsFormPage):
@@ -46,14 +64,29 @@ def __iter__(self) -> Iterator[BaseMetadata]:
4664
yield Field(json_schema_extra=self.props)
4765

4866

67+
def example_backend_validation(val: int) -> bool:
68+
if val == 9:
69+
raise ValueError("Value cannot be 9")
70+
return True
71+
72+
73+
NumberExample = Annotated[
74+
int, Ge(1), Le(10), MultipleOf(multiple_of=3), Predicate(example_backend_validation)
75+
]
76+
77+
4978
@app.post("/form")
5079
async def form(form_data: list[dict] = []):
5180
def form_generator(state: State):
5281
class TestForm(FormPage):
5382
model_config = ConfigDict(title="Form Title")
5483

55-
name: Annotated[str, Field(min_length=3)]
56-
# input: Annotated[str, ExtraData(props={"prop1": "val", "prop2": "val"})]
84+
number: NumberExample
85+
text: Annotated[str, Field(min_length=3, max_length=12)] = "Default text"
86+
textArea: LongText
87+
divider: Divider
88+
label: Label = "Label"
89+
hidden: Hidden = "Hidden"
5790

5891
form_data_1 = yield TestForm
5992

frontend/apps/example/src/app/page.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,25 @@ export default function Home() {
6060
const componentMatcher = (
6161
currentMatchers: PydanticComponentMatcher[],
6262
): PydanticComponentMatcher[] => {
63+
return currentMatchers;
6364
return [
65+
...currentMatchers,
6466
{
6567
id: 'textarea',
66-
Element: TextArea,
68+
ElementMatch: {
69+
Element: TextArea,
70+
isControlledElement: true,
71+
},
6772
matcher(field) {
6873
return field.type === PydanticFormFieldType.STRING;
6974
},
7075
},
71-
...currentMatchers,
7276
];
7377
};
7478

7579
return (
7680
<div className={styles.page}>
77-
<h1 style={{ marginBottom: '40px' }}>Pydantic Form</h1>
81+
<h1 style={{ marginBottom: '20px' }}>Pydantic Form</h1>
7882

7983
<PydanticForm
8084
id="theForm"

frontend/apps/example/src/fields/TextArea.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
import React from 'react';
2-
3-
import type {
4-
PydanticFormFieldElement,
5-
PydanticFormFieldElementProps,
6-
} from 'pydantic-forms';
7-
81
/**
92
* Pydantic Forms
103
*
114
* Text component
125
*/
13-
export const TextArea: PydanticFormFieldElement = ({
6+
import React from 'react';
7+
8+
import type {
9+
PydanticFormControlledElement,
10+
PydanticFormControlledElementProps,
11+
} from 'pydantic-forms';
12+
13+
export const TextArea: PydanticFormControlledElement = ({
1414
value,
1515
onChange,
16-
}: PydanticFormFieldElementProps) => {
16+
}: PydanticFormControlledElementProps) => {
1717
return (
1818
<textarea
1919
defaultValue={value}

0 commit comments

Comments
 (0)