-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Removable and addable options for array items #373
Conversation
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.
Some early thoughts.
@@ -427,6 +433,30 @@ const uiSchema = { | |||
}; | |||
``` | |||
|
|||
#### `addable` option | |||
|
|||
if either `items` or `additionalItems` contains a schema object, an add button for new items is shown by default. You can turn this off with the `addable` option in `uiSchema`: |
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.
I feel like this is trying to fix a misused json schema; If you want a fixed size array, your schema should reflect that intent, and the lib should render the form appropriately. Have a look at the fixedItemsList
sample in the playground.
I understand that this would add more flexibility, but on the other hand I feel like providing features to help people fixing their schemas is an endless rabbit hole we're not really wanting to start exploring... Thoughts? Did you know about support for fixed size arrays?
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.
Thank you for your reply. I knew about fixed arrays, I even added a fixed sized array example with 2 unremovable additionalItems in the playground. :)
The use case I have, is that the items are addable, reorderable and removable, but not via this form. Another user may add items in another form, but this user may not add items in this form. So the items in the array are really part of the data, not part of the schema. If I'm not mistaken, a fixed array in the schema would not be a right representation of the data. Then every instance of this particular data type should have their own schema, which would undermine the purpose of json schema.
In angular-schema-form, which appears to be maintained by the same organisation as JSON Schema, the api for this is as follows (a uiSchema
is called a form
in angular-schema-form):
To suppress add and remove buttons set add to null and remove to null.
function FormCtrl($scope) {
$scope.form = [
{
key: "subforms",
add: null,
remove: null,
style: {
add: "btn-success"
},
items: [
"subforms[].nick",
"subforms[].name",
"subforms[].emails",
],
}
];
}
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.
This makes a lot of sense, I get the point. Also I realize that people may want to alter the UI according to some user ACLs, and your patch allows dealing with this in a very convenient fashion.
Scratch my concerns, this is valid.
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.
A few nits, a rebase and we land this. Thanks 👍
onClick={this.onAddClick} disabled={disabled || readonly}/> | ||
{addable ? <AddButton | ||
onClick={this.onAddClick} | ||
disabled={disabled || readonly}/> : null} |
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.
Nit: indent props with 2 spaces
addable: true, | ||
...uiSchema["ui:options"] | ||
}; | ||
const hasAdd = addable && additionalSchema; |
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.
how about canAdd
for consistency?
moveDown: orderable && canMoveDown, | ||
remove: removable && canRemove | ||
}; | ||
has.toolbar = Object.keys(has).map(key => has[key]).includes(true); |
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.
Object.keys(has).some(key => has[key]);
ca7e8d6
to
2169084
Compare
Updated those 3 things and rebased. :) |
Woops, I've landed #375 meanwhile which makes your patch conflicting again. I can take over the next rebase if you're bored ;) |
…he orderable option; add corresponding tests, README info and playground examples
2169084
to
37439bc
Compare
Re-rebased. :) |
Perfect, thank you so much! |
Thanks, this is a great addition! |
You're not, we'll release a new version very soon now. |
This release has been made possible by the combined work of @olzraiti, @maartenth, @vinhlh, @tiemevanveen , @dehli, @enjoylife, @pabloen, @israelshirk, @sjhewitt and @rom10. Thank you folks! Breaking changes ---------------- Support for passing `DescriptionField`, `TitleField` and `SchemaField` as `Form` props as been dropped in this release. You now have to always pass them through the `fields` prop. Deprecations ------------ * Leverage `ui:options` everywhere (fix #370) (#378) There's now a unique recommended way to specify options for widgets in uiSchema, which is the `ui:options` directive. Previous ways are still supported, but you'll get a warning in the console if you use them. New features ------------ * Allow overriding the default fields/widgets (#371) * Pass data to `FieldTemplate` as strings instead of as React components (#341) * Pass `schema` & `uiSchema` to the field template component (#379) * Add `ui:order` wildcard feature and improve error messages (#368) * Add support for widget autofocus (#288) * Array field optional sortability (#345) * Radio widget support for integers and numbers (#325) * Add support for inline radios and checkboxes. (fix #346) (#348) * Added ref to `FileWidget`. (#355) * Added `removable` and `addable` options for array items (#373) * Enable Windows development (#320) Enhancements and bugfixes ------------------------- * Fix `minimum/maximum==0` for `UpDownWidget` and `RangeWidget` (#344) * Handle numbers requiring trailing zeros with more grace (#334) * Pass empty title to `TitleField` instead of name (#311) * `asNumber`: return `undefined` when value is empty string (#369) * Use [glyphicons](http://getbootstrap.com/components/#glyphicons) for buttons by default. (fix #337) (#375) * Support old versions of React (#312)
Released in v0.41.0. |
Reasons for making this change
There are cases where you don't want an add button or a remove button for array items, even though the schema allows it. Just like with the recent
orderable
option. The array items in my current case come from another resource and I fill the array in the background on initial form load; the user should not be able to reorder, add or remove the items via the form. I think this is a common use case.I refactored the code a little bit to align all variable names (
orderable
,addable
andremovable
foruiSchema
settings, andcanMoveUp
,canMoveDown
,canDelete
for application logic).Checklist