-
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
Array field optional sortability #345
Changes from 27 commits
251974c
8e69465
7ae018c
1217215
4136956
f8158b9
9eeaf75
39fb8c3
7ada187
6ab8b12
933f808
b7670f4
770f761
295e572
7bc5952
90c0bd8
2861c92
9e0caac
9c158ed
4974362
8d120e4
a919ae1
e936e07
0d2f33f
0283a95
4c68350
b7465cf
32c77e7
a7e917e
f16c111
b8adc95
b34235f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ A [live playground](https://mozilla-services.github.io/react-jsonschema-form/) i | |
- [File widgets](#file-widgets) | ||
- [Multiple files](#multiple-files) | ||
- [Object fields ordering](#object-fields-ordering) | ||
- [Array items ordering](#array-items-ordering) | ||
- [Custom CSS class names](#custom-css-class-names) | ||
- [Custom labels for enum fields](#custom-labels-for-enum-fields) | ||
- [Multiple choices list](#multiple-choices-list) | ||
|
@@ -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: | ||
|
||
```jsx | ||
const schema = { | ||
type: "array", | ||
properties: { | ||
type: "string" | ||
} | ||
}; | ||
|
||
const uiSchema = { | ||
"ui:options": { | ||
sortable: false | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having the same approach as widgets would be more consistent, but the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually like the Don't rush reverting though, I may actually just switch to using your |
||
}; | ||
``` | ||
|
||
### Custom CSS class names | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -337,8 +337,15 @@ class ArrayField extends Component { | |
autofocus | ||
}) { | ||
const {SchemaField} = this.props.registry.fields; | ||
const {disabled, readonly} = this.props; | ||
const {disabled, readonly, uiSchema} = this.props; | ||
|
||
const sortable = uiSchema["ui:options"] ? uiSchema["ui:options"].sortable : true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What If options is defined but There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
The PropType won't catch |
||
if (!sortable) { | ||
canMoveUp = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
canMoveDown = false; | ||
} | ||
const hasToolbar = removable || canMoveUp || canMoveDown; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); |
||
|
||
const btnStyle = {flex: 1, paddingLeft: 6, paddingRight: 6, fontWeight: "bold"}; | ||
|
||
return ( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -157,6 +157,15 @@ describe("ArrayField", () => { | |
expect(moveDownBtns[1].disabled).eql(true); | ||
}); | ||
|
||
it("should not be sortable when uiSchema[\"ui:options\"].sortable === false", () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should not show move up/down buttons is sortable is false |
||
const {node} = createFormComponent({schema, formData: ["foo", "bar"], uiSchema: {"ui:options": {sortable: false}}}); | ||
const moveUpBtns = node.querySelector(".array-item-move-up"); | ||
const moveDownBtns = node.querySelector(".array-item-move-down"); | ||
|
||
expect(moveUpBtns).to.be.null; | ||
expect(moveDownBtns).to.be.null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
}); | ||
|
||
it("should remove a field from the list", () => { | ||
const {node} = createFormComponent({schema, formData: ["foo", "bar"]}); | ||
const dropBtns = node.querySelectorAll(".array-item-remove"); | ||
|
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.
are orderable by default, and react-jsonschema-form renders move up/down buttons along them.
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 would probably say "alongside" :)