-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Sidebar] Add ability to add form fields to a sidebar
- Loading branch information
1 parent
0bca945
commit fa53d22
Showing
15 changed files
with
919 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/vendor | ||
/node_modules | ||
/node_modules | ||
/.vscode |
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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import Input from './input' | ||
const Checkbox = window.wp.components.CheckboxControl | ||
|
||
export default class CheckboxInput extends Input { | ||
constructor (props) { | ||
super(props) | ||
this.state = { | ||
checked: this.props.element.checked | ||
} | ||
} | ||
|
||
onChange (checked) { | ||
this.props.element.checked = checked | ||
this.setState({ checked: checked }) | ||
} | ||
|
||
render () { | ||
return ( | ||
<Checkbox | ||
label={this.getLabel()} | ||
value={this.props.element.value} | ||
checked={this.state.checked} | ||
onChange={this.onChange} /> | ||
) | ||
} | ||
} |
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,24 @@ | ||
const { element } = window.wp | ||
const { Component } = element | ||
|
||
export default class Input extends Component { | ||
constructor (props) { | ||
super(props) | ||
|
||
this.getLabel = this.getLabel.bind(this) | ||
this.onChange = this.onChange.bind(this) | ||
|
||
this.state = { | ||
value: this.props.element.value | ||
} | ||
} | ||
|
||
getLabel () { | ||
return (this.props.element.labels[0] || {}).innerText || this.props.element.placeholder | ||
} | ||
|
||
onChange (content) { | ||
this.props.element.value = content | ||
this.setState({ value: content }) | ||
} | ||
} |
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,67 @@ | ||
const { element } = window.wp | ||
const { Component } = element | ||
const Radio = window.wp.components.RadioControl | ||
|
||
export default class RadioInput extends Component { | ||
constructor (props) { | ||
super(props) | ||
|
||
this.getLabel = this.getLabel.bind(this) | ||
this.getOptions = this.getOptions.bind(this) | ||
this.getSelected = this.getSelected.bind(this) | ||
this.onChange = this.onChange.bind(this) | ||
|
||
this.state = { | ||
options: this.getOptions(), | ||
selected: this.getSelected() | ||
} | ||
} | ||
|
||
onChange (option) { | ||
this.props.options.forEach(radio => { | ||
if (radio.value === option) radio.checked = true | ||
}) | ||
|
||
this.setState({ selected: option }) | ||
} | ||
|
||
getLabel (option) { | ||
if (option.labels && option.labels[0]) { | ||
return option.labels[0].innerText | ||
} else { | ||
return null | ||
} | ||
} | ||
|
||
getOptions () { | ||
return this.props.options.map(option => { | ||
return { | ||
label: this.getLabel(option), | ||
value: option.value | ||
} | ||
}) | ||
} | ||
|
||
getSelected () { | ||
const options = this.props.options | ||
|
||
let selected = null | ||
Object.keys(options).some(key => { | ||
if (options[key].checked) { | ||
selected = options[key].value | ||
return true | ||
} | ||
}) | ||
|
||
return selected | ||
} | ||
|
||
render () { | ||
return ( | ||
<Radio | ||
options={this.state.options} | ||
selected={this.state.selected} | ||
onChange={this.onChange} /> | ||
) | ||
} | ||
} |
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,29 @@ | ||
import Input from './input' | ||
const Select = window.wp.components.SelectControl | ||
|
||
export default class SelectInput extends Input { | ||
constructor (props) { | ||
super(props) | ||
this.getOptions = this.getOptions.bind(this) | ||
} | ||
|
||
getOptions () { | ||
const element = this.props.element | ||
return Object.keys(element.options).map(key => { | ||
return { | ||
label: element[key].text, | ||
value: element[key].value | ||
} | ||
}) | ||
} | ||
|
||
render () { | ||
return ( | ||
<Select | ||
label={this.getLabel()} | ||
options={this.getOptions()} | ||
value={this.state.value} | ||
onChange={this.onChange} /> | ||
) | ||
} | ||
} |
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,73 @@ | ||
import CheckboxInput from './checkbox-input' | ||
import RadioInput from './radio-input' | ||
import SelectInput from './select-input' | ||
import TextInput from './text-input' | ||
import TextareaInput from './textarea-input' | ||
|
||
const { registerPlugin } = window.wp.plugins | ||
const { PluginSidebar } = window.wp.editPost | ||
const el = window.wp.element.createElement | ||
|
||
export default function () { | ||
registerPlugin('laraberg-sidebar', { | ||
render: () => { | ||
return el(PluginSidebar, { | ||
name: 'laraberg-sidebar', | ||
icon: 'media-text', | ||
title: 'Laraberg' | ||
}, renderElements()) | ||
} | ||
}) | ||
} | ||
|
||
function getElements (selector) { | ||
return Array.from(document.querySelectorAll(`.laraberg-sidebar ${selector}`)) | ||
} | ||
|
||
function renderInputCheckbox (element, index) { | ||
return <CheckboxInput key={index} element={element} /> | ||
} | ||
|
||
function renderInputRadio (elements) { | ||
return <RadioInput key={elements[0].name} options={elements} /> | ||
} | ||
|
||
function renderInputSelect (element, index) { | ||
return <SelectInput key={index} element={element} /> | ||
} | ||
|
||
function renderInputText (element, index) { | ||
return <TextInput key={index} element={element} /> | ||
} | ||
|
||
function renderInputTextarea (element, index) { | ||
return <TextareaInput key={index} element={element} /> | ||
} | ||
|
||
function getRadios () { | ||
const radios = getElements('input[type="radio"]') | ||
let result = radios.reduce((object, radio) => { | ||
if (!object[radio.name]) { | ||
object[radio.name] = [] | ||
} | ||
object[radio.name].push(radio) | ||
return object | ||
}, {}) | ||
return result | ||
} | ||
|
||
function renderElements () { | ||
const elements = [] | ||
elements.push(getElements('input[type="text"]').map(renderInputText)) | ||
elements.push(getElements('input[type="checkbox"]').map(renderInputCheckbox)) | ||
elements.push(getElements('textarea').map(renderInputTextarea)) | ||
elements.push(getElements('select').map(renderInputSelect)) | ||
|
||
const radios = getRadios() | ||
elements.push(Object.keys(radios).map(key => renderInputRadio(radios[key]))) | ||
|
||
return el('div', | ||
{ className: 'plugin-sidebar-content laraberg-sidebar-content' }, | ||
elements | ||
) | ||
} |
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 Input from './input' | ||
const Text = window.wp.components.TextControl | ||
|
||
export default class TextInput extends Input { | ||
render () { | ||
return ( | ||
<Text | ||
label={this.getLabel()} | ||
value={this.state.value} | ||
onChange={this.onChange} /> | ||
) | ||
} | ||
} |
Oops, something went wrong.