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

Removable and addable options for array items #373

Merged
merged 4 commits into from
Nov 4, 2016

Conversation

maartenth
Copy link
Contributor

@maartenth maartenth commented Nov 3, 2016

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 and removable for uiSchema settings, and canMoveUp, canMoveDown, canDelete for application logic).

options

Checklist

  • I'm updating documentation
    • I've checked the rendering of the Markdown text I've added
    • If I'm adding a new section, I've updated the Table of Content
  • I'm adding or updating code
    • I've added and/or updated tests
    • I've updated docs if needed
  • I'm adding a new feature
    • I've updated the playground with an example use of the feature

Copy link
Collaborator

@n1k0 n1k0 left a 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`:
Copy link
Collaborator

@n1k0 n1k0 Nov 4, 2016

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?

Copy link
Contributor Author

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",
      ],
    }
  ];
}

Copy link
Collaborator

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.

Copy link
Collaborator

@n1k0 n1k0 left a 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}
Copy link
Collaborator

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;
Copy link
Collaborator

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);
Copy link
Collaborator

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]);

@maartenth
Copy link
Contributor Author

Updated those 3 things and rebased. :)

@n1k0
Copy link
Collaborator

n1k0 commented Nov 4, 2016

Woops, I've landed #375 meanwhile which makes your patch conflicting again. I can take over the next rebase if you're bored ;)

@maartenth
Copy link
Contributor Author

Re-rebased. :)

@n1k0
Copy link
Collaborator

n1k0 commented Nov 4, 2016

Perfect, thank you so much!

@n1k0 n1k0 merged commit 2908af5 into rjsf-team:master Nov 4, 2016
@pablen
Copy link
Contributor

pablen commented Nov 8, 2016

Thanks, this is a great addition!
I think the documentation is visible in the README but the feature is not released yet. Am I wrong?

@n1k0
Copy link
Collaborator

n1k0 commented Nov 8, 2016

You're not, we'll release a new version very soon now.

@maartenth maartenth deleted the removable-and-addable branch November 8, 2016 21:19
n1k0 added a commit that referenced this pull request Nov 9, 2016
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)
@n1k0
Copy link
Collaborator

n1k0 commented Nov 9, 2016

Released in v0.41.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants