-
-
Notifications
You must be signed in to change notification settings - Fork 244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initialize model with default values #1343
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
712ab00
Add Bridge.getInitialModel
kestarumper 1ee3f9d
Run onChange(initialValue) always
kestarumper ecbdf36
Fix duplicated fields
kestarumper 3919385
Fix tests
kestarumper f685aa9
Merge branch 'master' into optional-default-value-model
kestarumper 5f87cf5
Hide Material ui css function is deprecated warn message
kestarumper 2b839fe
renderWithZod using AutoForm, fix some tests ListAddDelField, RadioField
kestarumper f51be5f
Rename some defaultValue in tests
kestarumper File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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
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
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 |
---|---|---|
@@ -1,45 +1,23 @@ | ||
import { render as renderOnScreen } from '@testing-library/react'; | ||
import React, { ReactElement } from 'react'; | ||
import { BaseForm, Context, context, randomIds } from 'uniforms'; | ||
import React, { ComponentProps, ReactElement } from 'react'; | ||
import { ZodBridge } from 'uniforms-bridge-zod'; | ||
import { TypeOf, ZodObject, ZodRawShape } from 'zod'; | ||
import { AutoForm } from 'uniforms-unstyled'; | ||
import { ZodObject, ZodRawShape } from 'zod'; | ||
|
||
export function renderWithZod<Props, Schema extends ZodObject<ZodRawShape>>({ | ||
element, | ||
schema, | ||
...contextValueOverride | ||
...autoFormProps | ||
}: { | ||
element: ReactElement<Props>; | ||
schema: Schema; | ||
} & Omit<Partial<Context<TypeOf<Schema>>>, 'schema'>) { | ||
} & Partial<Omit<ComponentProps<typeof AutoForm>, 'schema'>>) { | ||
return renderOnScreen(element, { | ||
wrapper({ children }) { | ||
const value = { | ||
changed: false, | ||
changedMap: Object.create(null), | ||
error: null, | ||
formRef: null as unknown as BaseForm<TypeOf<Schema>>, | ||
model: Object.create(null), | ||
name: [], | ||
onChange() {}, | ||
onSubmit() {}, | ||
randomId: randomIds(), | ||
submitted: false, | ||
submitting: false, | ||
validating: false, | ||
...contextValueOverride, | ||
schema: new ZodBridge({ | ||
schema, | ||
}), | ||
state: { | ||
disabled: false, | ||
readOnly: false, | ||
showInlineError: false, | ||
...contextValueOverride?.state, | ||
}, | ||
}; | ||
|
||
return <context.Provider children={children} value={value} />; | ||
const bridge = new ZodBridge({ schema }); | ||
return ( | ||
<AutoForm {...autoFormProps} schema={bridge} children={children} /> | ||
); | ||
}, | ||
}); | ||
} |
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,43 +1,30 @@ | ||
import { render as renderOnScreen, RenderResult } from '@testing-library/react'; | ||
import React, { ReactElement, cloneElement } from 'react'; | ||
import SimpleSchema, { SimpleSchemaDefinition } from 'simpl-schema'; | ||
import { BaseForm, Context, UnknownObject, context, randomIds } from 'uniforms'; | ||
import { Context, UnknownObject } from 'uniforms'; | ||
import { SimpleSchema2Bridge } from 'uniforms-bridge-simple-schema-2'; | ||
import { AutoForm } from 'uniforms-unstyled'; | ||
|
||
const randomId = randomIds(); | ||
export function render<P, Model extends UnknownObject>( | ||
element: ReactElement<P>, | ||
schema?: SimpleSchemaDefinition, | ||
contextValueExtension?: Partial<Context<Model>>, | ||
contextValueExtension?: Pick<Partial<Context<Model>>, 'onChange'>, | ||
model = {} as Model, | ||
): RenderResult & { rerenderWithProps: (props: P) => void } { | ||
const renderResult = renderOnScreen(element, { | ||
wrapper({ children }) { | ||
if (schema) { | ||
const contextValue = { | ||
changed: false, | ||
changedMap: {}, | ||
error: null, | ||
model, | ||
name: [], | ||
onChange() {}, | ||
onSubmit() {}, | ||
randomId, | ||
submitted: false, | ||
submitting: false, | ||
validating: false, | ||
...contextValueExtension, | ||
schema: new SimpleSchema2Bridge({ schema: new SimpleSchema(schema) }), | ||
state: { | ||
disabled: false, | ||
readOnly: false, | ||
showInlineError: false, | ||
...contextValueExtension?.state, | ||
}, | ||
formRef: {} as BaseForm<UnknownObject>, | ||
}; | ||
const bridge = new SimpleSchema2Bridge({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we also change the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call. It also showed some flaws in our tests. I fixed them. |
||
schema: new SimpleSchema(schema), | ||
}); | ||
return ( | ||
<context.Provider value={contextValue}>{children}</context.Provider> | ||
<AutoForm | ||
onChange={contextValueExtension?.onChange} | ||
model={model} | ||
schema={bridge} | ||
> | ||
{children} | ||
</AutoForm> | ||
); | ||
} | ||
return <>{children}</>; | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
contextValueExtension
is no longer needed. It is used only for theonChange
function. Just create theonChange
param.The whole
render
could also be refactored.schema
,onChange
andmodel
are optional. They can be moved to an object param. We could avoid these kind of uses:However, this is a big refactor because we use
render
many times, so maybe we can do it in another PR.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, it is too much for this PR (for me). But if you want to refactor, you can commit to this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's do this in another PR. Maybe just write
TODO
comment.