-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from workfloworchestrator/1785-npm-release
1785 npm release
- Loading branch information
Showing
32 changed files
with
732 additions
and
566 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'pydantic-forms': patch | ||
--- | ||
|
||
Adds object field |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
frontend/packages/pydantic-forms/src/components/componentMatcher.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { useForm } from 'react-hook-form'; | ||
|
||
import { z } from 'zod'; | ||
|
||
import defaultComponentMatchers from '@/components/defaultComponentMatchers'; | ||
import { TextField } from '@/components/fields'; | ||
import type { | ||
ElementMatch, | ||
Properties, | ||
PydanticComponentMatcher, | ||
PydanticFormComponents, | ||
PydanticFormField, | ||
PydanticFormsContextConfig, | ||
} from '@/types'; | ||
|
||
export const getMatcher = ( | ||
customComponentMatcher: PydanticFormsContextConfig['componentMatcher'], | ||
) => { | ||
const componentMatchers = customComponentMatcher | ||
? customComponentMatcher(defaultComponentMatchers) | ||
: defaultComponentMatchers; | ||
|
||
return (field: PydanticFormField): PydanticComponentMatcher | undefined => { | ||
return componentMatchers.find(({ matcher }) => { | ||
return matcher(field); | ||
}); | ||
}; | ||
}; | ||
|
||
export const getClientSideValidationRule = ( | ||
field: PydanticFormField, | ||
rhf?: ReturnType<typeof useForm>, | ||
customComponentMatcher?: PydanticFormsContextConfig['componentMatcher'], | ||
) => { | ||
const matcher = getMatcher(customComponentMatcher); | ||
|
||
const componentMatch = matcher(field); | ||
|
||
let validationRule = componentMatch?.validator?.(field, rhf) ?? z.string(); | ||
|
||
if (!field.required) { | ||
validationRule = validationRule.optional(); | ||
} | ||
|
||
if (field.validations.isNullable) { | ||
validationRule = validationRule.nullable(); | ||
} | ||
|
||
return validationRule; | ||
}; | ||
|
||
export const componentMatcher = ( | ||
properties: Properties, | ||
customComponentMatcher: PydanticFormsContextConfig['componentMatcher'], | ||
): PydanticFormComponents => { | ||
const matcher = getMatcher(customComponentMatcher); | ||
|
||
const components: PydanticFormComponents = Object.entries(properties).map( | ||
([, pydanticFormField]) => { | ||
const matchedComponent = matcher(pydanticFormField); | ||
|
||
const ElementMatch: ElementMatch = matchedComponent | ||
? matchedComponent.ElementMatch | ||
: { | ||
Element: TextField, | ||
isControlledElement: true, | ||
}; | ||
|
||
// Defaults to textField when there are no matches | ||
return { | ||
Element: ElementMatch, | ||
pydanticFormField: pydanticFormField, | ||
}; | ||
}, | ||
); | ||
|
||
return components; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
frontend/packages/pydantic-forms/src/components/fields/HiddenField.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import React from 'react'; | ||
|
||
import { usePydanticFormContext } from '@/core'; | ||
import { PydanticFormElementProps } from '@/types'; | ||
|
||
export const HiddenField = ({ | ||
pydanticFormField, | ||
rhf, | ||
}: PydanticFormElementProps) => { | ||
const { rhf } = usePydanticFormContext(); | ||
return <input type="hidden" {...rhf.register(pydanticFormField.id)} />; | ||
}; |
25 changes: 25 additions & 0 deletions
25
frontend/packages/pydantic-forms/src/components/fields/ObjectField.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import React from 'react'; | ||
|
||
import { usePydanticFormContext } from '@/core'; | ||
import { componentMatcher } from '@/core'; | ||
import { PydanticFormElementProps } from '@/types'; | ||
|
||
import { RenderFields } from '../render'; | ||
|
||
export const ObjectField = ({ | ||
pydanticFormField, | ||
}: PydanticFormElementProps) => { | ||
const { config } = usePydanticFormContext(); | ||
|
||
const components = componentMatcher( | ||
pydanticFormField.properties || {}, | ||
config?.componentMatcher, | ||
); | ||
|
||
return ( | ||
<div> | ||
<h1>{pydanticFormField.title}</h1> | ||
<RenderFields components={components} /> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 0 additions & 43 deletions
43
frontend/packages/pydantic-forms/src/components/render/Fields.tsx
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
frontend/packages/pydantic-forms/src/components/render/RenderFields.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* Pydantic Forms | ||
* | ||
* This component will render all the fields based on the | ||
* config in the pydanticFormContext | ||
*/ | ||
import React from 'react'; | ||
|
||
import { WrapFieldElement } from '@/core/WrapFieldElement'; | ||
import { PydanticFormComponents, PydanticFormField } from '@/types'; | ||
|
||
interface RenderFieldsProps { | ||
components: PydanticFormComponents; | ||
} | ||
|
||
export function RenderFields({ components }: RenderFieldsProps) { | ||
return components.map((component) => { | ||
const { Element, isControlledElement } = component.Element; | ||
const field: PydanticFormField = component.pydanticFormField; | ||
|
||
if (!Element) { | ||
return undefined; | ||
} | ||
|
||
if (isControlledElement) { | ||
return ( | ||
<div key={field.id}> | ||
<WrapFieldElement | ||
PydanticFormControlledElement={Element} | ||
pydanticFormField={field} | ||
/> | ||
</div> | ||
); | ||
} else { | ||
return <Element pydanticFormField={field} key={field.id} />; | ||
} | ||
}); | ||
} |
Oops, something went wrong.