Hi,
I'm using the bootstrap-4 theme. When I use the default ListItem component or a custom ListItem component, clicking 'Add' the first time always tries to set the count to 1.
For example, if the initialCount=0, clicking add once adds one ListItemField. This works as expected.
However, if the initialCount=1, clicking add once does nothing, clicking add again adds a second ListItemField.
If the initialCount=2, clicking add once removes the second ListItemField so that there is only 1. Clicking it again then adds a second ListItemField back.
This occurred using this code:
<ListField
name="prices"
addIcon={<Add />}
removeIcon={<Delete />}
initialCount={1}
showInlineError
/>
It was also happening for my custom list field, but I'm assuming the fix will apply to both.
Hi @LordA98. Thanks for the report! @Monteth: could you take a look at it?
Hi @LordA98,
I tried to reproduce your issue on this branch, but I did not succeed.
Could you introduce more details, or just change my reproduction environment to fit your case?
Hi @Monteth,
Sorry for the delayed response.
I will take a look at the reproduction environment now.
Okay, it seems to be caused by the requiredByDefault flag (at least for me anyway).
This is my form:
<AutoForm schema={ItemBridge}>
<ListField name="options" addIcon="+" initialCount={1} />
<SubmitField />
</AutoForm>
This is my schema:
ItemSchema = new SimpleSchema(
{
options: { type: Array, label: "Option Groups" },
'options.$': String,
},
{ requiredByDefault: false }
);
const ItemBridge = new SimpleSchema2Bridge(ItemSchema);
export { ItemBridge, ItemSchema };
Removing the requiredByDefault flag at the bottom "fixes" the issue.
So, is this related to Uniforms or to Simpl Schema?
Hi @LordA98. I've dug into it today, and it's indeed hard to debug. So, let me describe the problem first and then how to fix it.
requiredByDefault: false (globally) or optional: true (on the field), makes it optional. And, of course, the aforementioned condition is false.model, using their default values (and these received through props). The code is here. The important part is that it happens only if the field is required.ListAddField, that actually _adds_ new items, is not aware of its parent default value here. And why? Because it calculates it without the initialCount, used in the schema.Now, _most probably_ adding initialCount to the second argument of useField in the ListAddField will fix it. We have to check it and if so, release a fix. However, if you have a custom list field - just give it a try.
@Monteth: Could you add a unit test to verify this problem and this solution? :crossed_fingers: