Skip to content

Commit

Permalink
Refactor and clean up repeat to not use state at all
Browse files Browse the repository at this point in the history
  • Loading branch information
zackify committed May 8, 2018
1 parent 49df7a7 commit 796477c
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 83 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.4.6",
"version": "0.5.1",
"description": "Useful inspector controls for gutenberg",
"main": "dist/index.js",
"scripts": {
Expand Down
45 changes: 14 additions & 31 deletions controls/src/repeat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,32 @@ import React from 'react';

const { Button } = wp.components;

let getItems = ({ attributes, attribute }) =>
attributes && attributes[attribute] ? attributes[attribute] : [];

export default class Repeat extends React.Component {
constructor({ attribute, attributes }) {
constructor() {
super();
this.add = this.add.bind(this);
this.update = this.update.bind(this);
this.delete = this.delete.bind(this);

let items =
attributes && attributes[attribute] ? attributes[attribute] : [];
this.state = { items: items.length };
}

static getDerivedStateFromProps({ attributes, attribute }, state) {
let items =
attributes && attributes[attribute] ? attributes[attribute] : [];
if (items.length !== state.items) return { items: items.length };

return null;
}

add() {
let { attribute, attributes, setAttributes } = this.props;
let items =
attributes && attributes[attribute] ? attributes[attribute] : [];
setAttributes({ [attribute]: [...items, {}] })
let { attribute, onChange } = this.props;

onChange(attribute, [...getItems(this.props), {}]);
}

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

let currentAttributes = attributes[attribute] || [];
let currentAttributes = getItems(this.props);

let foundAttribute = currentAttributes.find(
(attr, index) => index === tabId
);

let newAttributes;
if (!foundAttribute)
newAttributes = [...currentAttributes, { [name]: value }];
Expand Down Expand Up @@ -95,9 +86,10 @@ export default class Repeat extends React.Component {
}

render() {
let { items } = this.state;
let { style, title, indent, addNew, max } = this.props;

let items = getItems(this.props).length;

let repeats = [];
for (let item = 0; item < items; item++) {
repeats.push(this.renderChildren(item));
Expand All @@ -110,17 +102,8 @@ export default class Repeat extends React.Component {
{repeats}
<div style={{ marginTop: '10px' }}>
{max > items || !max ? (
<Button
isPrimary
onClick={() => {
this.setState(function (prevState, props) {
return { items: prevState.items + 1 }
}, () => {
this.props.setAttributes && this.add();
})
}}
>
{addNew}
<Button isPrimary onClick={() => this.add()}>
{addNew}
</Button>
) : null}
</div>
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.6.1",
"version": "0.6.3",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
96 changes: 48 additions & 48 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.4.6",
"gutenblock-controls": "^0.5.1",
"react": "^16.3.2",
"react-dom": "^16.3.2",
"react-hot-loader": "^4.0.1"
Expand Down
2 changes: 1 addition & 1 deletion plugin/plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
* License: GPL2+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*
* @package CGB
* @package Gutenblock
*/
require_once plugin_dir_path( __FILE__ ) . 'src/init.php';
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 796477c

Please sign in to comment.