Skip to content

Commit

Permalink
fix: del + container + handler
Browse files Browse the repository at this point in the history
  • Loading branch information
foxhound87 committed Jul 23, 2018
1 parent 4441940 commit 3301972
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 30 deletions.
13 changes: 6 additions & 7 deletions src/shared/Actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,18 +274,17 @@ export default {
Del Field
*/
@action
del(partialPath = null) {
const path = parser.parsePath(utils.$try(partialPath, this.path));
del($path = null) {
const isStrict = this.state.options.get('strictDelete', this);
const path = parser.parsePath(utils.$try($path, this.path));
const container = this.container($path);
const keys = _.split(path, '.');
const last = _.last(keys);
const isStrict = this.state.options.get('strictDelete', this);

const container = this.container(path);

if (isStrict && !container.fields.has(last)) {
const msg = `Key "${last}" not found when trying to delete field`;
const $path = _.trim([this.path, path].join('.'), '.');
utils.throwError($path, null, msg);
const fullpath = _.trim([this.path, path].join('.'), '.');
utils.throwError(fullpath, null, msg);
}

container.fields.delete(last);
Expand Down
19 changes: 12 additions & 7 deletions src/shared/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,19 @@ export default {
/**
Get Container
*/
container(path) {
const $path = parser.parsePath(utils.$try(path, this.path));
const cpath = _.trim($path.replace(new RegExp('[^./]+$'), ''), '.');

if (!!this.path && _.isNil(path)) {
return cpath !== '' ? this.state.form.select(cpath, null, false) : this.state.form;
container($path) {
const path = parser.parsePath(utils.$try($path, this.path));
const cpath = _.trim(path.replace(new RegExp('[^./]+$'), ''), '.');

if (!!this.path && _.isNil($path)) {
return cpath !== ''
? this.state.form.select(cpath, null, false)
: this.state.form;
}
return cpath !== '' ? this.select(cpath, null, false) : this;

return cpath !== ''
? this.select(cpath, null, false)
: this;
},

/**
Expand Down
24 changes: 9 additions & 15 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,15 @@ const props = {
},
};

const getObservableMapValues = (observableMap) => {
if (mobxValues) {
return mobxValues(observableMap);
}

return observableMap.values();
};

const getObservableMapKeys = (observableMap) => {
if (mobxValues) {
return mobxKeys(observableMap);
}

return observableMap.keys();
};
const getObservableMapValues = observableMap =>
mobxValues
? mobxValues(observableMap)
: observableMap.values();

const getObservableMapKeys = observableMap =>
mobxValues
? mobxKeys(observableMap)
: observableMap.keys();

const checkObserveItem = change => ({
key, to, type, exec,
Expand Down
12 changes: 11 additions & 1 deletion tests/data/forms/fixes/form.o.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,18 @@ class NewForm extends Form {
hooks() {
return {
onInit() {
this.$('array').add();
this.$('array').add(); // 0
this.$('array').add(); // 1
this.$('array').add(); // 2

this.$('array').del(0);

const proxy = new Proxy({
preventDefault: () => {},
}, {});

this.$('array').onDel(proxy, 1);
this.$('array[2]').onDel(proxy);
},
};
}
Expand Down

0 comments on commit 3301972

Please sign in to comment.