Skip to content

Commit

Permalink
Fix bug with item deletion in repeat
Browse files Browse the repository at this point in the history
  • Loading branch information
zackify committed Apr 27, 2018
1 parent 62edf10 commit 93d088a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 43 deletions.
2 changes: 1 addition & 1 deletion controls/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gutenblock-controls",
"version": "0.3.4",
"version": "0.3.5",
"description": "Useful inspector controls for gutenberg",
"main": "dist/index.js",
"scripts": {
Expand Down
55 changes: 18 additions & 37 deletions controls/src/repeat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,27 @@ export default class Repeat extends React.Component {
return null;
}

update(name, value, tabId, parentChange, customUpdate) {
let { attribute, attributes, setAttributes } = this.props;
update(name, value, tabId, onChange) {
let { attribute, attributes } = this.props;

//if nested repeats, pass state up to top most one
if (parentChange) {
let currentAttributes = attributes[attribute] || [];
let currentAttributes = attributes[attribute] || [];

let foundAttribute = currentAttributes.find(
(attr, index) => index === tabId
let foundAttribute = currentAttributes.find(
(attr, index) => index === tabId
);
let newAttributes;
if (!foundAttribute)
newAttributes = [...currentAttributes, { [name]: value }];
else
newAttributes = currentAttributes.map(
(attr, index) => (index === tabId ? { ...attr, [name]: value } : attr)
);
let newAttributes;
if (!foundAttribute)
newAttributes = [...currentAttributes, { [name]: value }];
else
newAttributes = currentAttributes.map(
(attr, index) => (index === tabId ? { ...attr, [name]: value } : attr)
);

return parentChange(attribute, newAttributes);
}

let topLevel = [...attributes[attribute]];

topLevel[tabId] = { ...topLevel[tabId], [name]: value };

setAttributes({ [attribute]: topLevel });
return onChange(attribute, newAttributes);
}

delete(childAttributes, childAttribute, tabId, onDelete, customDelete) {
let { attribute, attributes, setAttributes } = this.props;
delete(childAttributes, childAttribute, tabId, onChange) {
let { attribute, attributes } = this.props;

let newAttributes;
if (!childAttributes)
Expand All @@ -68,9 +59,7 @@ export default class Repeat extends React.Component {
);
}

if (onDelete) return onDelete(newAttributes, attribute);

setAttributes({ [attribute]: newAttributes });
return onChange(attribute, newAttributes);
}

renderChildren(index) {
Expand All @@ -80,7 +69,6 @@ export default class Repeat extends React.Component {
setAttributes,
attributes,
onChange,
onDelete,
} = this.props;

return React.Children.map(children, child =>
Expand All @@ -92,16 +80,9 @@ export default class Repeat extends React.Component {
attributes && attributes[attribute] && attributes[attribute][index]
? attributes[attribute][index]
: {},
onChange: (name, value) =>
this.update(name, value, index, onChange, child.onUpdate),
onChange: (name, value) => this.update(name, value, index, onChange),
onDelete: (childAttributes, childAttribute) =>
this.delete(
childAttributes,
childAttribute,
index,
onDelete,
child.onDelete
),
this.delete(childAttributes, childAttribute, index, onChange),
style: {
marginLeft: '10px',
marginTop: '15px',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gutenblock",
"version": "0.5.1",
"version": "0.5.2",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
6 changes: 3 additions & 3 deletions plugin/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"license": "ISC",
"dependencies": {
"@babel/polyfill": "^7.0.0-beta.46",
"gutenblock-controls": "^0.3.4",
"gutenblock-controls": "^0.3.5",
"react": "^16.3.2",
"react-dom": "^16.3.2",
"react-hot-loader": "^4.0.1"
Expand Down

0 comments on commit 93d088a

Please sign in to comment.