Skip to content

Commit

Permalink
feat(validate): enable sub component input validate
Browse files Browse the repository at this point in the history
pass enableComponentInputValidate to $validateAll fn to enable component's input validate
  • Loading branch information
Genuifx committed Mar 11, 2020
1 parent c63aa4d commit 72999b6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
17 changes: 14 additions & 3 deletions packages/wxa-validate/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {normalizeRules, arraySomeSync} from './utils';
import regeneratorRuntime from "regenerator-runtime/runtime";

export default (options = {}) => {
options.enableComponentInputValidate = options.enableComponentInputValidate || false;
options.componentClassName = ['.wxa-input-component', '.wxa-input-com'] || options.componentClassName;
options.ignoreErrorRule = options.ignoreErrorRule || [];

const MESSAGES = {...defaultMessages, ...options.messages};
Expand All @@ -15,6 +17,12 @@ export default (options = {}) => {
}
})

let generateQuery = (componentClassName) => {
componentClassName = Array.isArray(componentClassName) ? componentClassName : [componentClassName];

return componentClassName.map((item)=>item+' >>> .wxa-input').join(', ');
}

return (vm, type) => {
if (['Page', 'Component'].indexOf(type) == -1) return;

Expand Down Expand Up @@ -116,14 +124,17 @@ export default (options = {}) => {
this.$validate(e);
};

vm.$validateAll = function (except = '') {
vm.$validateAll = function (except = '', {enableComponentInputValidate} = {}) {
enableComponentInputValidate = enableComponentInputValidate == null ? options.enableComponentInputValidate : enableComponentInputValidate;
let exceptRule = Array.isArray(except) ? except : [except];
return new Promise((resolve, reject) => {
let q = wx.createSelectorQuery();

if (type === 'Component') q.in(this);
if(type === 'Component') q.in(this);

let query = '.wxa-input' + (enableComponentInputValidate ? ', '+generateQuery(options.componentClassName) : '');

q.selectAll('.wxa-input')
q.selectAll(query)
.fields({
dataset: true,
id: true,
Expand Down
4 changes: 2 additions & 2 deletions packages/wxa-validate/src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ export const toArray = (arrayLike) => {
};

export const arraySomeSync = async function (arr, callback) {
for (let [index, item] of Object.entries(arr)) {
if (await callback(item, index, arr)) return true;
for (let i = 0; i < arr.length; i ++) {
if (await callback(arr[i], i, arr)) return true;
}
return false;
}

0 comments on commit 72999b6

Please sign in to comment.