ssc-grid
Version:
React grid component for SSC 3.0
168 lines (158 loc) • 4.67 kB
JavaScript
// TODO: The publicly exposed parts of this should be in lib/SSCGridUtils.
import validator from 'validator';
/**
* @export
* @param {any} {type, helpText} helpText是可选参数,用于指定自定义提示信息
* @returns
*/
export function getValidationObj(_ref) {
var type = _ref.type,
helpText = _ref.helpText;
var vs = {
currency: {
matchFunc: function matchFunc(value) {
return validator.isDecimal(value);
},
helpText: function helpText() {
return '请输入正确的货币格式!';
}
},
decimal: {
matchFunc: function matchFunc(value) {
return validator.isDecimal(value);
},
helpText: function helpText() {
return '请输入正确的数字格式!';
}
},
email: {
matchFunc: function matchFunc(value) {
return validator.isEmail(value);
},
helpText: function helpText() {
return '请输入正确的邮箱格式!';
}
},
int: {
matchFunc: function matchFunc(value) {
return validator.isInt(value);
},
helpText: function helpText() {
return '请输入正确的整数格式!';
}
},
length: {
/**
* v.options = { min: 3, max: 6 }
*/
matchFunc: function matchFunc(value, v) {
value = value ? value : '';
return validator.isLength(value, v.options);
},
helpText: function helpText(value, v) {
return '\u8F93\u5165\u957F\u5EA6\u5FC5\u987B\u4ECB\u4E8E ' + v.options.min + ' \u548C ' + v.options.max + ' \u4E4B\u95F4\u7684\u5B57\u7B26\u4E32';
}
},
mobilePhone: {
matchFunc: function matchFunc(value) {
return validator.isMobilePhone(value, 'zh-CN');
},
helpText: function helpText() {
return '请输入正确的手机号格式!';
}
},
required: {
matchFunc: function matchFunc(value) {
value = value ? value : '';
// Removes whitespace from both ends of a string
// Whitespace in this context is all the whitespace characters
// (space, tab, no-break space, etc.) and all the line terminator
// characters (LF, CR, etc.).
value = value.trim();
// 这个判断没有啥实际意义,就是装逼用的
// Remove zero-width space characters from a JavaScript string
// http://stackoverflow.com/a/11305926/4685522
value = value.replace(/[\u200B-\u200D\uFEFF]/g, '');
// TODO 更装逼的做法是使用https://github.com/slevithan/XRegExp
// http://stackoverflow.com/a/11598864/4685522
return !validator.isEmpty(value);
},
helpText: function helpText() {
return '必须输入该字段!';
}
}
};
var validationObj = vs[type];
if (helpText) {
// 自定义错误提示
validationObj.helpText = helpText;
}
return validationObj;
}
/**
* 校验字段是否不为'error'
* @param {String|null} vstate react-bootstrap的验证状态
* @param {Boolean}
*/
export function isFieldValid(vstate) {
return vstate !== 'error';
}
/**
* 判断校验对象是否全部正确
* @param {Object} states
* ```
* {
* email: 'error',
* name: 'success'
* }
* ```
* @return {boolean}
*/
export function isStatesValid(states) {
var isAllValid = true;
var fieldId = void 0;
// 遍历检查每个需要校验的字段的状态
for (fieldId in states) {
if (states.hasOwnProperty(fieldId)) {
isAllValid = isAllValid && isFieldValid(states[fieldId]);
}
}
return isAllValid;
}
/**
* 对一个字段进行校验
* @param {String} value 进行校验的值必须是字符串
* @param {Array} validators
* @return {Object} 校验之后的状态和提示信息
* ```json
* {
* validationState: 'error',
* helpText: '请输入正确的邮箱地址'
* }
* ```
*/
export function calcValidationState(value, validators) {
var validationState = 'success';
var helpTextStr = '';
var helpTexts = [];
if (!validators) {
validators = [];
}
validators.forEach(function (v) {
var _ref2 = v.type === 'custom' ? v : getValidationObj(v),
matchFunc = _ref2.matchFunc,
helpText = _ref2.helpText;
var isValid = matchFunc(value, v);
if (!isValid) {
validationState = 'error';
helpTextStr += '\n' + helpText(value, v);
helpTexts.push(helpText(value, v));
}
});
// TODO 当存在多个校验类型的时候,使用数组helpTexts返回校验
return {
validationState: validationState,
helpText: helpTextStr,
helpTexts: helpTexts
};
}