Skip to content

Commit 503fb1f

Browse files
olzraitin1k0
authored andcommitted
Array field optional sortability (rjsf-team#345)
1 parent eb168c3 commit 503fb1f

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ A [live playground](https://mozilla-services.github.io/react-jsonschema-form/) i
3333
- [File widgets](#file-widgets)
3434
- [Multiple files](#multiple-files)
3535
- [Object fields ordering](#object-fields-ordering)
36+
- [Array items ordering](#array-items-ordering)
3637
- [Custom CSS class names](#custom-css-class-names)
3738
- [Custom labels for enum fields](#custom-labels-for-enum-fields)
3839
- [Multiple choices list](#multiple-choices-list)
@@ -399,6 +400,24 @@ render((
399400
uiSchema={uiSchema} />
400401
), document.getElementById("app"));
401402
```
403+
### Array items ordering
404+
405+
Array items are orderable by default, and react-jsonschema-form renders move up/down buttons alongside them. The `uiSchema` object spec allows you to disable ordering:
406+
407+
```jsx
408+
const schema = {
409+
type: "array",
410+
properties: {
411+
type: "string"
412+
}
413+
};
414+
415+
const uiSchema = {
416+
"ui:options": {
417+
orderable: false
418+
}
419+
};
420+
```
402421

403422
### Custom CSS class names
404423

src/components/fields/ArrayField.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,15 @@ class ArrayField extends Component {
338338
autofocus
339339
}) {
340340
const {SchemaField} = this.props.registry.fields;
341-
const {disabled, readonly} = this.props;
342-
const hasToolbar = removable || canMoveUp || canMoveDown;
341+
const {disabled, readonly, uiSchema} = this.props;
342+
343+
const {orderable} = {orderable: true, ...uiSchema["ui:options"]};
344+
345+
const _canMoveUp = orderable && canMoveUp;
346+
const _canMoveDown = orderable && canMoveDown;
347+
348+
const hasToolbar = removable || _canMoveUp || _canMoveDown;
349+
343350
const btnStyle = {flex: 1, paddingLeft: 6, paddingRight: 6, fontWeight: "bold"};
344351

345352
return (
@@ -362,18 +369,18 @@ class ArrayField extends Component {
362369
hasToolbar ?
363370
<div className="col-xs-2 array-item-toolbox text-right">
364371
<div className="btn-group" style={{display: "flex"}}>
365-
{canMoveUp || canMoveDown ?
372+
{_canMoveUp || _canMoveDown ?
366373
<button type="button" className="btn btn-default array-item-move-up"
367374
style={btnStyle}
368375
tabIndex="-1"
369-
disabled={disabled || readonly || !canMoveUp}
376+
disabled={disabled || readonly || !_canMoveUp}
370377
onClick={this.onReorderClick(index, index - 1)}></button>
371378
: null}
372-
{canMoveUp || canMoveDown ?
379+
{_canMoveUp || _canMoveDown ?
373380
<button type="button" className="btn btn-default array-item-move-down"
374381
style={btnStyle}
375382
tabIndex="-1"
376-
disabled={disabled || readonly || !canMoveDown}
383+
disabled={disabled || readonly || !_canMoveDown}
377384
onClick={this.onReorderClick(index, index + 1)}></button>
378385
: null}
379386
{removable ?
@@ -407,7 +414,11 @@ function AddButton({onClick, disabled}) {
407414
if (process.env.NODE_ENV !== "production") {
408415
ArrayField.propTypes = {
409416
schema: PropTypes.object.isRequired,
410-
uiSchema: PropTypes.object,
417+
uiSchema: PropTypes.shape({
418+
"ui:options": PropTypes.shape({
419+
orderable: PropTypes.bool
420+
})
421+
}),
411422
idSchema: PropTypes.object,
412423
errorSchema: PropTypes.object,
413424
onChange: PropTypes.func.isRequired,

test/ArrayField_test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,15 @@ describe("ArrayField", () => {
157157
expect(moveDownBtns[1].disabled).eql(true);
158158
});
159159

160+
it("should not show move up/down buttons is orderable is false", () => {
161+
const {node} = createFormComponent({schema, formData: ["foo", "bar"], uiSchema: {"ui:options": {orderable: false}}});
162+
const moveUpBtns = node.querySelector(".array-item-move-up");
163+
const moveDownBtns = node.querySelector(".array-item-move-down");
164+
165+
expect(moveUpBtns).to.be.null;
166+
expect(moveDownBtns).to.be.null;
167+
});
168+
160169
it("should remove a field from the list", () => {
161170
const {node} = createFormComponent({schema, formData: ["foo", "bar"]});
162171
const dropBtns = node.querySelectorAll(".array-item-remove");

0 commit comments

Comments
 (0)