Skip to content
This repository has been archived by the owner on Dec 31, 2020. It is now read-only.

Commit

Permalink
Key-value pair in additional properties retains order when edited (rj…
Browse files Browse the repository at this point in the history
…sf-team#1101)

* Make key-value pair stay at the same place and remove strange behavior with label

* Handle case when you click input and immediately unclick

* Add two test cases
  • Loading branch information
aerfio authored and edi9999 committed Dec 21, 2018
1 parent 0ac0da7 commit ae9e58d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/components/fields/ObjectField.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,27 @@ class ObjectField extends Component {
getAvailableKey = (preferredKey, formData) => {
var index = 0;
var newKey = preferredKey;
while (this.props.formData.hasOwnProperty(newKey)) {
while (formData.hasOwnProperty(newKey)) {
newKey = `${preferredKey}-${++index}`;
}
return newKey;
};

onKeyChange = oldValue => {
return (value, errorSchema) => {
if (oldValue === value) {
return;
}
value = this.getAvailableKey(value, this.props.formData);
const newFormData = { ...this.props.formData };
const property = newFormData[oldValue];
delete newFormData[oldValue];
newFormData[value] = property;
const newKeys = { [oldValue]: value };
const keyValues = Object.keys(newFormData).map(key => {
const newKey = newKeys[key] || key;
return { [newKey]: newFormData[key] };
});
const renamedObj = Object.assign({}, ...keyValues);
this.props.onChange(
newFormData,
renamedObj,
errorSchema &&
this.props.errorSchema && {
...this.props.errorSchema,
Expand Down
33 changes: 33 additions & 0 deletions test/ObjectField_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,24 @@ describe("ObjectField", () => {
expect(comp.state.formData.newFirst).eql(1);
});

it("should keep order of renamed key-value pairs while renaming key", () => {
const { comp, node } = createFormComponent({
schema,
formData: { first: 1, second: 2, third: 3 },
});

const textNode = node.querySelector("#root_second-key");
Simulate.blur(textNode, {
target: { value: "newSecond" },
});

expect(Object.keys(comp.state.formData)).eql([
"first",
"newSecond",
"third",
]);
});

it("should attach suffix to formData key if new key already exists when key input is renamed", () => {
const formData = {
first: 1,
Expand All @@ -538,6 +556,21 @@ describe("ObjectField", () => {
expect(comp.state.formData["second-1"]).eql(1);
});

it("should not attach suffix when input is only clicked", () => {
const formData = {
first: 1,
};
const { comp, node } = createFormComponent({
schema,
formData,
});

const textNode = node.querySelector("#root_first-key");
Simulate.blur(textNode);

expect(comp.state.formData.hasOwnProperty("first")).to.be.true;
});

it("should continue incrementing suffix to formData key until that key name is unique after a key input collision", () => {
const formData = {
first: 1,
Expand Down

0 comments on commit ae9e58d

Please sign in to comment.