-
-
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
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #1343 +/- ##
==========================================
+ Coverage 94.18% 94.19% +0.01%
==========================================
Files 226 226
Lines 3664 3672 +8
Branches 989 988 -1
==========================================
+ Hits 3451 3459 +8
Misses 81 81
Partials 132 132 ☔ View full report in Codecov by Sentry. |
|
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.
Playground link doesn't work for me (I installed dependencies, built everything and started doc website). I've got this error:
TypeError: Cannot read properties of undefined (reading 'length')
}, | ||
formRef: {} as BaseForm<UnknownObject>, | ||
}; | ||
const bridge = new SimpleSchema2Bridge({ |
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.
Shouldn't we also change the render-zod
function?
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.
Good call. It also showed some flaws in our tests. I fixed them.
{ | ||
x: { type: Array, optional: true }, | ||
'x.$': { type: Object }, | ||
'x.$.name': { type: String, defaultValue: 'kebab' }, |
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.
Maybe it's just me, but I would use a different name 😛
export function render<P, Model extends UnknownObject>( | ||
element: ReactElement<P>, | ||
schema?: SimpleSchemaDefinition, | ||
contextValueExtension?: Partial<Context<Model>>, | ||
contextValueExtension?: Pick<Partial<Context<Model>>, 'onChange'>, |
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 the onChange
function. Just create the onChange
param.
The whole render
could also be refactored. schema
, onChange
and model
are optional. They can be moved to an object param. We could avoid these kind of uses:
render(
<ListField name="x">
<Child />
PlainText
</ListField>,
{ x: Array, 'x.$': String },
undefined,
{ x: [undefined, undefined] },
);
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.
Works for me. |
@kestarumper I tried it with
However, it is working for @Monteth. |
export function render<P, Model extends UnknownObject>( | ||
element: ReactElement<P>, | ||
schema?: SimpleSchemaDefinition, | ||
contextValueExtension?: Partial<Context<Model>>, | ||
contextValueExtension?: Pick<Partial<Context<Model>>, 'onChange'>, |
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.
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.
It is a great change. I'd like to see what is the impact on first render time 🤔
@@ -84,6 +85,19 @@ export abstract class Bridge { | |||
); | |||
} | |||
|
|||
/** | |||
* Get initial model value recursively. |
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.
* Get initial model value recursively. | |
* Get the initial model value walking the tree recursively. |
Maybe it is nitpicking, but you do not recursively run the getInitialModel
function.
Closes #1066.
Changes
initialValues
taken fromschema
(Bridge
) -schema.model
andprops.model
and the latter one takes precedence__tests__
only)render(...)
changed wrapper from<context.Provider>
to<AutoForm>
because it changed behavior of<ListField>
(the list of child fields was incorrect)Notes
Field.onChange
of the fields that set theirinitialValue
on the first render. However, the fields that are rendered after, e.g., after adding another field in anArray
, or due to conditional rendering, will still triggerField.onChange
.Example