-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
0.2.4, add in Repeat component with example
- Loading branch information
Showing
15 changed files
with
240 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import React from 'react'; | ||
|
||
const { Button } = wp.components; | ||
|
||
export default class Repeat extends React.Component { | ||
constructor({ attribute, attributes }) { | ||
super(); | ||
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; | ||
} | ||
|
||
update(name, value, tabId, parentChange, customUpdate) { | ||
let { attribute, attributes, setAttributes } = this.props; | ||
|
||
//if nested repeats, pass state up to top most one | ||
if (parentChange) { | ||
let currentAttributes = attributes[attribute] || []; | ||
|
||
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) | ||
); | ||
|
||
return parentChange(attribute, newAttributes); | ||
} | ||
|
||
let topLevel = [...attributes[attribute]]; | ||
|
||
topLevel[tabId] = { ...topLevel[tabId], [name]: value }; | ||
|
||
setAttributes({ [attribute]: topLevel }); | ||
} | ||
|
||
delete(childAttributes, childAttribute, tabId, onDelete, customDelete) { | ||
let { attribute, attributes, setAttributes } = this.props; | ||
|
||
let newAttributes; | ||
if (!childAttributes) | ||
newAttributes = attributes[attribute].filter( | ||
(attr, index) => index !== tabId | ||
); | ||
|
||
if (childAttributes) { | ||
let newItem = { | ||
...attributes[attribute][tabId], | ||
[childAttribute]: childAttributes, | ||
}; | ||
newAttributes = attributes[attribute].map( | ||
(item, index) => (index === tabId ? newItem : item) | ||
); | ||
} | ||
|
||
if (onDelete) return onDelete(newAttributes, attribute); | ||
|
||
setAttributes({ [attribute]: newAttributes }); | ||
} | ||
|
||
renderChildren(index) { | ||
let { | ||
attribute, | ||
children, | ||
setAttributes, | ||
attributes, | ||
onChange, | ||
onDelete, | ||
} = this.props; | ||
|
||
return React.Children.map(children, child => | ||
React.cloneElement(child, { | ||
key: index, | ||
index, | ||
setAttributes, | ||
attributes: | ||
attributes && attributes[attribute] && attributes[attribute][index] | ||
? attributes[attribute][index] | ||
: undefined, | ||
onChange: (name, value) => | ||
this.update(name, value, index, onChange, child.onUpdate), | ||
onDelete: (childAttributes, childAttribute) => | ||
this.delete( | ||
childAttributes, | ||
childAttribute, | ||
index, | ||
onDelete, | ||
child.onDelete | ||
), | ||
style: { | ||
marginLeft: '10px', | ||
marginTop: '15px', | ||
}, | ||
}) | ||
); | ||
} | ||
|
||
render() { | ||
let { items } = this.state; | ||
let { style, title, indent, addNew, max } = this.props; | ||
|
||
let repeats = []; | ||
for (let item = 0; item < items; item++) { | ||
repeats.push(this.renderChildren(item)); | ||
} | ||
|
||
return ( | ||
<div style={style}> | ||
<h1>{title}</h1> | ||
<div> | ||
{repeats} | ||
<div style={{ marginTop: '10px' }}> | ||
{max > items || !max ? ( | ||
<Button | ||
isPrimary | ||
onClick={() => this.setState({ items: items + 1 })} | ||
> | ||
{addNew} | ||
</Button> | ||
) : null} | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import React from 'react'; | ||
|
||
const RepeatInput = ({ | ||
name, | ||
label, | ||
onChange, | ||
onDelete, | ||
type, | ||
attributes, | ||
setAttributes, | ||
}) => ( | ||
<div className="components-base-control__field"> | ||
<label htmlFor={name} className="components-base-control__label"> | ||
{label} | ||
</label> | ||
<div style={{ display: 'flex' }}> | ||
<input | ||
id={name} | ||
name={name} | ||
value={attributes ? attributes[name] : ''} | ||
type={type || 'string'} | ||
className="components-text-control__input" | ||
onChange={e => onChange(name, e.target.value)} | ||
/> | ||
<div style={{ alignSelf: 'center' }} onClick={() => onDelete()}> | ||
X | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
|
||
export default RepeatInput; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import React from 'react'; | ||
|
||
export default ({ note }) => ( | ||
<li style={{ listStyle: 'square !important' }}>{note.heading}</li> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import Note from './note'; | ||
|
||
export default ({ tabs }) => | ||
tabs.map(tab => ( | ||
<div> | ||
<h2>Tab: {tab.title}</h2> | ||
{tab.notes ? tab.notes.map(note => <Note note={note} />) : null} | ||
</div> | ||
)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,17 @@ | ||
import Inspector from './inspector'; | ||
import Tabs from './components/tabs'; | ||
import { RichText } from 'gutenblock-controls'; | ||
|
||
const Edit = () => ( | ||
const Edit = ({ attributes }) => ( | ||
<div> | ||
<RichText name="title" /> | ||
<div> | ||
<RichText | ||
tagName="h1" | ||
name="title" | ||
placeholder="Section Title: Add tabs to the right ->" | ||
/> | ||
</div> | ||
<Tabs tabs={attributes.tabs} /> | ||
<Inspector /> | ||
</div> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Inspector, Repeat, RepeatInput } from 'gutenblock-controls'; | ||
|
||
export default () => ( | ||
<Inspector> | ||
<Repeat title="Tabs" addNew="Add Tab" attribute="tabs"> | ||
<RepeatInput name="title" /> | ||
|
||
<Repeat title="Notes" addNew="Add Note" attribute="notes" max={3}> | ||
<RepeatInput name="heading" /> | ||
</Repeat> | ||
</Repeat> | ||
</Inspector> | ||
); |