Skip to content

Commit

Permalink
feat: add Data.set callback
Browse files Browse the repository at this point in the history
addSetCallback take a regex string to match data paths
  • Loading branch information
kevinchappell committed Aug 30, 2020
1 parent 6a43466 commit b7c8ff3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
16 changes: 16 additions & 0 deletions src/js/components/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ export default class Data {

const data = set(this.data, path, newVal)

const callBackPath = Array.isArray(path) ? path.join('.') : path
const callBackGroups = Object.keys(this.setCallbacks).filter(setKey => new RegExp(setKey).test(callBackPath))
const cbArgs = { newVal, oldVal, path }
callBackGroups.forEach(cbGroup => this.setCallbacks[cbGroup].forEach(cb => cb(cbArgs)))

if (!this.disableEvents) {
const change = this.getChangeType(oldVal, newVal)
const evtData = {
Expand All @@ -54,6 +59,16 @@ export default class Data {

return data
}
addSetCallback(path, cb) {
if (this.setCallbacks[path]) {
this.setCallbacks[path].push(cb)
} else {
this.setCallbacks[path] = [cb]
}
}
removeSetCallback(path, cb) {
this.setCallbacks[path] = this.setCallbacks[path].filter(setCb => setCb !== cb)
}
add = (id, data = Object.create(null)) => {
const { id: dataId } = data
const elemId = id || dataId || uuid()
Expand All @@ -79,5 +94,6 @@ export default class Data {
return acc
}, {})
}
setCallbacks = {}
configVal = Object.create(null)
}
30 changes: 22 additions & 8 deletions src/js/components/fields/edit-panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,28 @@ export default class EditPanel {
this.name = panelName
this.field = field

this.props = this.createProps()
this.panelConfig = this.getPanelConfig(this.data)

if (panelName === 'options') {
field.addSetCallback(`^${panelName}`, () => {
this.data = field.get('options')
const { config, ...panelConfig } = this.getPanelConfig(this.data)
const editPanel = document.getElementById(this.panelConfig.id)
editPanel.replaceWith(dom.create(panelConfig))
})
}
}

getPanelConfig(data) {
this.props = this.createProps(data)
this.editButtons = this.createEditButtons()
this.panelConfig = {
return {
id: `${this.field.id}-${this.name}-panel`,
config: {
label: i18n.get(`panel.label.${panelName}`),
label: i18n.get(`panel.label.${this.name}`),
},
attrs: {
className: `f-panel ${panelName}-panel`,
className: `f-panel ${this.name}-panel`,
},
children: [this.props, this.editButtons],
}
Expand All @@ -42,11 +56,11 @@ export default class EditPanel {
* @param {Object} dataObj field config object
* @return {Object} formeo DOM config object
*/
createProps() {
this.editPanelItems = Array.from(this.data).map((data, index) => {
createProps(data) {
this.editPanelItems = Array.from(data).map((dataVal, index) => {
const isArray = this.type === 'array'
const itemKey = [this.name, isArray ? String(index) : data[0]].join('.')
const itemData = isArray ? data : { [data[0]]: data[1] }
const itemKey = [this.name, isArray ? String(index) : dataVal[0]].join('.')
const itemData = isArray ? dataVal : { [dataVal[0]]: dataVal[1] }

return new EditPanelItem({ key: itemKey, data: itemData, field: this.field })
})
Expand Down

0 comments on commit b7c8ff3

Please sign in to comment.