Skip to content
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 8 commits into from
Jun 14, 2024
Merged

Conversation

kestarumper
Copy link
Member

@kestarumper kestarumper commented May 29, 2024

Closes #1066.

Changes

  • Model is initialized with the initialValues taken from schema (Bridge) -
  • Model results from a shallow merge of schema.model and props.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

  • BREAKING: This changes the initial render and doesn't trigger the Field.onChange of the fields that set their initialValue on the first render. However, the fields that are rendered after, e.g., after adding another field in an Array, or due to conditional rendering, will still trigger Field.onChange.
  • BREAKING: Optional / Required doesn't affect setting the model

Example

// SimpleSchema2Bridge
{
  string1: {
    type: String,
    defaultValue: "yooo"
  },
  string2: {
    type: String,
    defaultValue: "yooo2",
    optional: true
  },
  friends: { type: Array, defaultValue: [{ name: "John Doe", age: 42 }] },
  'friends.$': Object,
  'friends.$.name': String,
  'friends.$.age': { type: Number, defaultValue: 99 },
  'friends.$.address': Object,
  'friends.$.address.street': { type: String, defaultValue: 'Sesame' },
  'friends.$.favoriteNumbers': { type: Array, defaultValue: [21, 37] },
  'friends.$.favoriteNumbers.$': { type: Number },
}

@kestarumper kestarumper requested a review from wadamek65 as a code owner May 29, 2024 10:45
@github-actions github-actions bot added the Area: Core Affects the uniforms package label May 29, 2024
Copy link

codecov bot commented May 29, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.19%. Comparing base (d2b924a) to head (f51be5f).
Report is 1 commits behind head on master.

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.
📢 Have feedback on the report? Share it here.

@kestarumper kestarumper added this to the v4.0 milestone Jun 3, 2024
@kestarumper kestarumper self-assigned this Jun 3, 2024
@kestarumper
Copy link
Member Author

kestarumper commented Jun 3, 2024

  • add test to check if it works correctly with optional fields added during runtime

@github-actions github-actions bot added Area: Theme Affects some of the theme packages Theme: Unstyled Affects the uniforms-unstyled package labels Jun 5, 2024
Copy link
Collaborator

@piotrpospiech piotrpospiech left a 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({
Copy link
Collaborator

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?

Copy link
Member Author

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' },
Copy link
Collaborator

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'>,
Copy link
Collaborator

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.

Copy link
Member Author

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.

Copy link
Collaborator

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.

@kestarumper
Copy link
Member Author

@piotrpospiech

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')

Works for me.
I run npm run reset && npm ci (node v20.10.0).

@github-actions github-actions bot added Area: Infra Affects the repository itself (e.g., CI, dependencies) Theme: AntD Affects the uniforms-antd package Theme: Bootstrap 4 Affects the uniforms-bootstrap4 package Theme: Bootstrap 5 Affects the uniforms-bootstrap5 package Theme: MUI Affects the uniforms-mui package Theme: Semantic UI Affects the uniforms-semantic package labels Jun 6, 2024
@kestarumper kestarumper requested a review from piotrpospiech June 6, 2024 14:03
@piotrpospiech
Copy link
Collaborator

@kestarumper I tried it with Node v20.10 and npm 10.2.5. I ran npm run reset && npm ci. Still the same issue:

TypeError: Cannot read properties of undefined (reading 'length')

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'>,
Copy link
Collaborator

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.

Copy link
Member

@Monteth Monteth left a 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* 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.

@kestarumper kestarumper merged commit c047291 into master Jun 14, 2024
7 checks passed
@kestarumper kestarumper deleted the optional-default-value-model branch June 14, 2024 10:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Core Affects the uniforms package Area: Infra Affects the repository itself (e.g., CI, dependencies) Area: Theme Affects some of the theme packages Theme: AntD Affects the uniforms-antd package Theme: Bootstrap 4 Affects the uniforms-bootstrap4 package Theme: Bootstrap 5 Affects the uniforms-bootstrap5 package Theme: MUI Affects the uniforms-mui package Theme: Semantic UI Affects the uniforms-semantic package Theme: Unstyled Affects the uniforms-unstyled package
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Optional defaultValue does not populate form model, but field only
4 participants