Skip to content

Commit

Permalink
feat: global inject for veui-loader, i18n for rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Justineo committed Nov 3, 2018
1 parent 3335f78 commit f5d96a8
Show file tree
Hide file tree
Showing 17 changed files with 140 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -42,6 +42,7 @@
* `managers/config` 模块的配置现在为响应式数据,支持在组件渲染后进行全局修改。
* `Field` 组件对应的数据字段名现在默认优先取 `name` prop,且可以被 `field` prop 覆盖。
* `rule` 模板占位符由 `${...}` 变更为 `{...}`,以方便在模板字符串中进行书写。老语法仍然保持兼容。
* `veui-loader` 新增支持通过 `global` 选项配置全局引入的模块。
### 🐞 问题修复
Expand Down
2 changes: 1 addition & 1 deletion README.md
@@ -1,6 +1,6 @@
# VEUI

![](https://travis-ci.org/ecomfe/veui.svg?branch=dev)
![](https://badgen.net/circleci/github/ecomfe/veui)

Enterprise UI components for Vue.js. Based on ONE DESIGN from Baidu, Inc.

Expand Down
24 changes: 19 additions & 5 deletions packages/veui-loader/src/index.js
Expand Up @@ -117,23 +117,33 @@ function getParts (component, options) {
path: packPath = COMPONENTS_DIRNAME,
transform,
fileName,
locale
locale,
global = []
} = options

if (pack && fileName) {
modules.push({ package: pack, path: packPath, transform, fileName })
}

if (locale) {
if (locale !== false) {
if (!locale) {
locale = 'zh-Hans'
}

if (!Array.isArray(locale)) {
locale = [locale]
}
modules = locale.filter(l => typeof l === 'string').map(l => {
locale = locale.filter(l => typeof l === 'string')
modules = locale.map(l => {
return { package: 'veui', path: `locale/${l}`, transform: false, fileName: '{module}.js' }
}).concat(modules)

global = locale.map(l => {
return { path: `veui/locale/${l}/common` }
}).concat(global)
}

return modules.reduce(
let a = modules.reduce(
(
acc,
{
Expand All @@ -152,10 +162,14 @@ function getParts (component, options) {
return acc
},
{
script: [],
script: [...global],
style: []
}
)

console.log(a.script)

return a
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/veui/src/locale/en-US/common.js
@@ -0,0 +1 @@
import './rules'
20 changes: 20 additions & 0 deletions packages/veui/src/locale/en-US/index.js
@@ -0,0 +1,20 @@
import './common'
import './Alert'
import './Calendar'
import './Carousel'
import './DatePicker'
import './Dialog'
import './FilterPanel'
import './Input'
import './NumberInput'
import './Pagination'
import './Progress'
import './RegionPicker'
import './Schedule'
import './Searchbox'
import './Select'
import './Steps'
import './Table'
import './Tabs'
import './Transfer'
import './Uploader'
17 changes: 17 additions & 0 deletions packages/veui/src/locale/en-US/rules.js
@@ -0,0 +1,17 @@
import i18n from '../../managers/i18n'

i18n.register(
'en-US',
{
max: 'Can\'t be greater than {ruleValue}',
maxLength: 'Can\'t be more than {ruleValue} characters',
min: 'Can\'t be less than {ruleValue}',
minLength: 'Can\'t be less than {ruleValue} characters',
numeric: 'The value must be a number',
pattern: 'Invalid pattern',
required: 'Required'
},
{
ns: 'rules'
}
)
1 change: 1 addition & 0 deletions packages/veui/src/locale/zh-Hans/common.js
@@ -0,0 +1 @@
import './rules'
20 changes: 20 additions & 0 deletions packages/veui/src/locale/zh-Hans/index.js
@@ -0,0 +1,20 @@
import './common'
import './Alert'
import './Calendar'
import './Carousel'
import './DatePicker'
import './Dialog'
import './FilterPanel'
import './Input'
import './NumberInput'
import './Pagination'
import './Progress'
import './RegionPicker'
import './Schedule'
import './Searchbox'
import './Select'
import './Steps'
import './Table'
import './Tabs'
import './Transfer'
import './Uploader'
17 changes: 17 additions & 0 deletions packages/veui/src/locale/zh-Hans/rules.js
@@ -0,0 +1,17 @@
import i18n from '../../managers/i18n'

i18n.register(
'zh-Hans',
{
max: '不能大于{ruleValue}',
maxLength: '字符长度不能超过{ruleValue}',
min: '不能小于{ruleValue}',
minLength: '字符长度不能短于{ruleValue}',
numeric: '值必须为数字',
pattern: '格式不符合要求',
required: '请填写'
},
{
ns: 'rules'
}
)
23 changes: 13 additions & 10 deletions packages/veui/src/managers/rule.js
@@ -1,3 +1,5 @@
import Vue from 'vue'
import { isObject, isFunction } from 'lodash'
import required from './rules/required'
import maxLength from './rules/maxLength'
import minLength from './rules/minLength'
Expand All @@ -6,7 +8,6 @@ import min from './rules/min'
import numeric from './rules/numeric'
import pattern from './rules/pattern'
import type from './type'
import { isObject, isFunction } from 'lodash'

/**
* 变量匹配正则
Expand All @@ -21,15 +22,17 @@ const valueRe = /\$?\{value\}/g

export class Rule {
constructor () {
this.ruleValidators = {
required,
maxLength,
minLength,
max,
min,
numeric,
pattern
}
this.ruleValidators = new Vue({
data: {
required,
maxLength,
minLength,
max,
min,
numeric,
pattern
}
})
}

validate (val, rules, context) {
Expand Down
7 changes: 5 additions & 2 deletions packages/veui/src/managers/rules/max.js
@@ -1,10 +1,13 @@
import { isEmpty } from '../../utils/helper'
import { isNumber } from 'lodash'
import { isEmpty } from '../../utils/helper'
import i18n from '../i18n'

export default {
validate (val, ruleValue) {
return !isEmpty(val) && isNumber(val) ? val <= ruleValue : true
},
message: '不能大于${ruleValue}',
message (value, ruleValue) {
return i18n.get('rules.max', { value, ruleValue })
},
priority: 200
}
5 changes: 4 additions & 1 deletion packages/veui/src/managers/rules/maxLength.js
@@ -1,9 +1,12 @@
import { isEmpty } from '../../utils/helper'
import i18n from '../i18n'

export default {
validate (val, ruleValue) {
return !isEmpty(val) ? val.length <= ruleValue : true
},
message: '字符长度不能超过${ruleValue}',
message (value, ruleValue) {
return i18n.get('rules.maxLength', { value, ruleValue })
},
priority: 100
}
7 changes: 5 additions & 2 deletions packages/veui/src/managers/rules/min.js
@@ -1,10 +1,13 @@
import { isEmpty } from '../../utils/helper'
import { isNumber } from 'lodash'
import { isEmpty } from '../../utils/helper'
import i18n from '../i18n'

export default {
validate (val, ruleValue) {
return !isEmpty(val) && isNumber(val) ? val >= ruleValue : true
},
message: '不能小于${ruleValue}',
message (value, ruleValue) {
return i18n.get('rules.min', { value, ruleValue })
},
priority: 200
}
5 changes: 4 additions & 1 deletion packages/veui/src/managers/rules/minLength.js
@@ -1,9 +1,12 @@
import { isEmpty } from '../../utils/helper'
import i18n from '../i18n'

export default {
validate (val, ruleValue) {
return !isEmpty(val) ? val.length >= ruleValue : true
},
message: '字符长度不能短于${ruleValue}',
message (value, ruleValue) {
return i18n.get('rules.minLength', { value, ruleValue })
},
priority: 100
}
5 changes: 4 additions & 1 deletion packages/veui/src/managers/rules/numeric.js
@@ -1,12 +1,15 @@
import { isNumber, toNumber, isNaN, isString } from 'lodash'
import { isEmpty } from '../../utils/helper'
import i18n from '../i18n'

export default {
validate (val) {
return !isEmpty(val)
? isNumber(val) || isString(val) && !/^0(?!\.|e)/.test(val) && !isNaN(toNumber(val))
: true
},
message: '值必须为数字',
message (value, ruleValue) {
return i18n.get('rules.numeric', { value, ruleValue })
},
priority: 10
}
5 changes: 4 additions & 1 deletion packages/veui/src/managers/rules/pattern.js
@@ -1,11 +1,14 @@
import { isType, getTypeByInstance } from '../../utils/lang'
import i18n from '../i18n'

export default {
validate (val, ruleValue) {
return isType(getTypeByInstance(ruleValue), RegExp)
? ruleValue.test(val)
: new RegExp(ruleValue).test(val)
},
message: '格式不符合要求',
message (value, ruleValue) {
return i18n.get('rules.pattern', { value, ruleValue })
},
priority: 50
}
5 changes: 4 additions & 1 deletion packages/veui/src/managers/rules/required.js
@@ -1,9 +1,12 @@
import { isEmpty } from '../../utils/helper'
import i18n from '../i18n'

export default {
validate (val) {
return !isEmpty(val)
},
message: '请填写',
message (value, ruleValue) {
return i18n.get('rules.required', { value, ruleValue })
},
priority: 0
}

0 comments on commit f5d96a8

Please sign in to comment.