Skip to content

Commit b7c8ff3

Browse files
committed
feat: add Data.set callback
addSetCallback take a regex string to match data paths
1 parent 6a43466 commit b7c8ff3

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

src/js/components/data.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ export default class Data {
3535

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

38+
const callBackPath = Array.isArray(path) ? path.join('.') : path
39+
const callBackGroups = Object.keys(this.setCallbacks).filter(setKey => new RegExp(setKey).test(callBackPath))
40+
const cbArgs = { newVal, oldVal, path }
41+
callBackGroups.forEach(cbGroup => this.setCallbacks[cbGroup].forEach(cb => cb(cbArgs)))
42+
3843
if (!this.disableEvents) {
3944
const change = this.getChangeType(oldVal, newVal)
4045
const evtData = {
@@ -54,6 +59,16 @@ export default class Data {
5459

5560
return data
5661
}
62+
addSetCallback(path, cb) {
63+
if (this.setCallbacks[path]) {
64+
this.setCallbacks[path].push(cb)
65+
} else {
66+
this.setCallbacks[path] = [cb]
67+
}
68+
}
69+
removeSetCallback(path, cb) {
70+
this.setCallbacks[path] = this.setCallbacks[path].filter(setCb => setCb !== cb)
71+
}
5772
add = (id, data = Object.create(null)) => {
5873
const { id: dataId } = data
5974
const elemId = id || dataId || uuid()
@@ -79,5 +94,6 @@ export default class Data {
7994
return acc
8095
}, {})
8196
}
97+
setCallbacks = {}
8298
configVal = Object.create(null)
8399
}

src/js/components/fields/edit-panel.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,28 @@ export default class EditPanel {
2323
this.name = panelName
2424
this.field = field
2525

26-
this.props = this.createProps()
26+
this.panelConfig = this.getPanelConfig(this.data)
27+
28+
if (panelName === 'options') {
29+
field.addSetCallback(`^${panelName}`, () => {
30+
this.data = field.get('options')
31+
const { config, ...panelConfig } = this.getPanelConfig(this.data)
32+
const editPanel = document.getElementById(this.panelConfig.id)
33+
editPanel.replaceWith(dom.create(panelConfig))
34+
})
35+
}
36+
}
37+
38+
getPanelConfig(data) {
39+
this.props = this.createProps(data)
2740
this.editButtons = this.createEditButtons()
28-
this.panelConfig = {
41+
return {
42+
id: `${this.field.id}-${this.name}-panel`,
2943
config: {
30-
label: i18n.get(`panel.label.${panelName}`),
44+
label: i18n.get(`panel.label.${this.name}`),
3145
},
3246
attrs: {
33-
className: `f-panel ${panelName}-panel`,
47+
className: `f-panel ${this.name}-panel`,
3448
},
3549
children: [this.props, this.editButtons],
3650
}
@@ -42,11 +56,11 @@ export default class EditPanel {
4256
* @param {Object} dataObj field config object
4357
* @return {Object} formeo DOM config object
4458
*/
45-
createProps() {
46-
this.editPanelItems = Array.from(this.data).map((data, index) => {
59+
createProps(data) {
60+
this.editPanelItems = Array.from(data).map((dataVal, index) => {
4761
const isArray = this.type === 'array'
48-
const itemKey = [this.name, isArray ? String(index) : data[0]].join('.')
49-
const itemData = isArray ? data : { [data[0]]: data[1] }
62+
const itemKey = [this.name, isArray ? String(index) : dataVal[0]].join('.')
63+
const itemData = isArray ? dataVal : { [dataVal[0]]: dataVal[1] }
5064

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

0 commit comments

Comments
 (0)