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

Array field optional sortability #345

Merged
merged 32 commits into from
Oct 20, 2016

Conversation

olzraiti
Copy link
Contributor

In a complex form the sortability makes the form a bit cluttered and is often not needed.

I added a whole new object ui:options to the ArrayField's uiSchema for flexibility in case we add other options to ArrayField in the future.

Copy link
Contributor

@leplatrem leplatrem left a comment

Choose a reason for hiding this comment

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

Excellent!

That's indeed interesting since forms can look bloated when order does not matter (we have use-cases!)

Also, I think (but I'm not native english) that the term is more orderable than sortable.

I highlighted a few things, and once we define the best way to specifiy the options, we're good to go!

Thank you for this contribution!

const moveDownBtns = node.querySelector(".array-item-move-down");

expect(moveUpBtns).to.be.null;
expect(moveDownBtns).to.be.null;
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@@ -157,6 +157,15 @@ describe("ArrayField", () => {
expect(moveDownBtns[1].disabled).eql(true);
});

it("should not be sortable when uiSchema[\"ui:options\"].sortable === false", () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

should not show move up/down buttons is sortable is false

@@ -399,6 +400,24 @@ render((
uiSchema={uiSchema} />
), document.getElementById("app"));
```
### Array items ordering

Array items are sortable by default. The `uiSchema` object spec allows you to disable ordering:
Copy link
Contributor

Choose a reason for hiding this comment

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

are orderable by default, and react-jsonschema-form renders move up/down buttons along them.

Copy link
Contributor

Choose a reason for hiding this comment

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

I would probably say "alongside" :)

const uiSchema = {
"ui:options": {
sortable: false
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that @n1k0 took a different approach in #346 in order to introduce widget options:

const uiSchema = {
  "ui:widget": {
    component: "checkboxes",
    options: {
      inline: true
    }
  }
};

I think that both approaches are valid :) I'll let you figure out pros and cons for each ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Having the same approach as widgets would be more consistent, but the ui:options -style makes updating a uiSchema simpler (if you want to add an option to an existing uiSchema field, you don't replace any lines, just add new ones).

Copy link
Collaborator

Choose a reason for hiding this comment

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

I actually like the ui:options approach, definitely less verbose and more flexible. Too bad I've just landed #346 which leverages the widget options approach, which is making your approach inconsistent.

Don't rush reverting though, I may actually just switch to using your ui:options thing instead :)

const {disabled, readonly} = this.props;
const {disabled, readonly, uiSchema} = this.props;

const sortable = uiSchema["ui:options"] ? uiSchema["ui:options"].sortable : true;
Copy link
Contributor

Choose a reason for hiding this comment

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

What If options is defined but sortable is left undefined?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got me there! Fixed. I added also a PropType for safety. Now the only weird thing that could happen would be when orderable is set specifically to undefined:

...
"ui:options": {
  orderable: undefined
}
...

The PropType won't catch undefined, so we interpret it as false. It's unclear what the user would mean with undefined - I think false is a safe bet since it's a falsy value.

@olzraiti
Copy link
Contributor Author

I renamed the option to 'orderable'.

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.

Nice work! A few minor changes required though :)

const options = uiSchema["ui:options"];
if (options && options.hasOwnProperty("orderable")) {
orderable = !!options.orderable;
}
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

const options = {orderable: true, ...uiSchema["ui:options"]}

orderable = !!options.orderable;
}
if (!orderable) {
canMoveUp = false;
Copy link
Collaborator

Choose a reason for hiding this comment

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

As a general good practice, I'd rather not mutate passed arguments at all.

if (!orderable) {
canMoveUp = false;
canMoveDown = false;
}
const hasToolbar = removable || canMoveUp || canMoveDown;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually the same could be achieved by this (I think):

const {orderable} = {orderable: true, ...uiSchema["ui:options"]};
const hasToolbar = orderable && (removable || canMoveUp || canMoveDown);

@n1k0 n1k0 merged commit 503fb1f into rjsf-team:master Oct 20, 2016
@n1k0
Copy link
Collaborator

n1k0 commented Oct 20, 2016

Thank you! 👍

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.

4 participants