@formily/core
Version:
90 lines • 2.95 kB
JavaScript
import { FormPath, isFn, each } from '@formily/shared';
import { buildDataPath } from '../shared/internals';
var output = function (field, taker) {
if (!field)
return;
if (isFn(taker)) {
return taker(field, field.address);
}
return field;
};
var takeMatchPattern = function (form, pattern) {
var identifier = pattern.toString();
var indexIdentifier = form.indexes[identifier];
var absoluteField = form.fields[identifier];
var indexField = form.fields[indexIdentifier];
if (absoluteField) {
return identifier;
}
else if (indexField) {
return indexIdentifier;
}
};
var Query = /** @class */ (function () {
function Query(props) {
var _this = this;
this.addresses = [];
this.pattern = FormPath.parse(props.pattern, props.base);
this.form = props.form;
if (!this.pattern.isMatchPattern) {
var matched = takeMatchPattern(this.form, this.pattern.haveRelativePattern
? buildDataPath(props.form.fields, this.pattern)
: this.pattern);
if (matched) {
this.addresses = [matched];
}
}
else {
each(this.form.fields, function (field, address) {
if (!field) {
delete _this.form.fields[address];
return;
}
if (field.match(_this.pattern)) {
_this.addresses.push(address);
}
});
}
}
Query.prototype.take = function (taker) {
return output(this.form.fields[this.addresses[0]], taker);
};
Query.prototype.map = function (iterator) {
var _this = this;
return this.addresses.map(function (address) {
return output(_this.form.fields[address], iterator);
});
};
Query.prototype.forEach = function (iterator) {
var _this = this;
return this.addresses.forEach(function (address) {
return output(_this.form.fields[address], iterator);
});
};
Query.prototype.reduce = function (reducer, initial) {
var _this = this;
return this.addresses.reduce(function (value, address) {
return output(_this.form.fields[address], function (field, address) {
return reducer(value, field, address);
});
}, initial);
};
Query.prototype.get = function (key) {
var results = this.take();
if (results) {
return results[key];
}
};
Query.prototype.getIn = function (pattern) {
return FormPath.getIn(this.take(), pattern);
};
Query.prototype.value = function () {
return this.get('value');
};
Query.prototype.initialValue = function () {
return this.get('initialValue');
};
return Query;
}());
export { Query };
//# sourceMappingURL=Query.js.map