Skip to content

Commit

Permalink
Merge pull request #9 from timarney/master
Browse files Browse the repository at this point in the history
issue # 7 - Repeat Field inside editor
  • Loading branch information
Zach Silveira committed May 8, 2018
2 parents 2c7454c + ca7e62b commit 49df7a7
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
dist
node_modules
node_modules
.DS_Store
18 changes: 16 additions & 2 deletions controls/src/repeat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { Button } = wp.components;
export default class Repeat extends React.Component {
constructor({ attribute, attributes }) {
super();
this.add = this.add.bind(this);
this.update = this.update.bind(this);
this.delete = this.delete.bind(this);

Expand All @@ -21,6 +22,13 @@ export default class Repeat extends React.Component {
return null;
}

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

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

Expand Down Expand Up @@ -104,9 +112,15 @@ export default class Repeat extends React.Component {
{max > items || !max ? (
<Button
isPrimary
onClick={() => this.setState({ items: items + 1 })}
onClick={() => {
this.setState(function (prevState, props) {
return { items: prevState.items + 1 }
}, () => {
this.props.setAttributes && this.add();
})
}}
>
{addNew}
{addNew}
</Button>
) : null}
</div>
Expand Down
14 changes: 14 additions & 0 deletions plugin/src/notes-block/block.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Save from './save';

const Block = {
title: 'Notes',
icon: 'edit',
category: 'common',
attributes: {
notes: {
type: 'array',
default: [{ title: "My First Item" }],
},
},
save: Save
};
19 changes: 19 additions & 0 deletions plugin/src/notes-block/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

import { Input, Repeat } from 'gutenblock-controls';

const Edit = ({ attributes, setAttributes }) => {
return (
<div>
<Repeat
attributes={attributes}
onChange={(name, value) => setAttributes({ [name]: value })}
setAttributes={setAttributes}
title="Notes"
max="50"
addNew="Add Item" attribute="notes"
>
<Input name="title" />
</Repeat>
</div>
)
};
10 changes: 10 additions & 0 deletions plugin/src/notes-block/save.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default ({ attributes }) => <div>
<h1>Notes:</h1>
<ul>
{attributes.notes.map(note => {
return (
<li> {note.title}</li>
)
})}
</ul>
</div>;

0 comments on commit 49df7a7

Please sign in to comment.