alaska-field-number
Advanced tools
Comparing version 0.2.1 to 0.3.0
114
index.js
@@ -12,3 +12,63 @@ /** | ||
exports.views = { | ||
class NumberField extends alaska.Field { | ||
init() { | ||
let field = this; | ||
this.underscoreMethod('format', function (format) { | ||
if (format) { | ||
return numeral(this.get(field.path)).format(format); | ||
} | ||
return this.get(field.path); | ||
}); | ||
} | ||
createFilter(filter) { | ||
let value; | ||
if (typeof filter === 'object') { | ||
value = filter.value; | ||
} else if (typeof filter === 'number' || typeof filter === 'string') { | ||
value = filter; | ||
} | ||
if (value !== undefined) { | ||
value = parseFloat(value); | ||
return isNaN(value) ? undefined : value; | ||
} | ||
//区间 | ||
let bt; | ||
if (filter instanceof Array) { | ||
bt = filter; | ||
} else if (filter.$bt && filter.$bt instanceof Array) { | ||
bt = filter.$bt; | ||
} | ||
if (bt && bt.length === 2) { | ||
let start = parseFloat(bt[0]); | ||
let end = parseFloat(bt[1]); | ||
if (isNaN(start) || isNaN(end)) { | ||
return; | ||
} | ||
return { $gte: start, $lte: end }; | ||
} | ||
//比较 | ||
['$gt', '$gte', '$lt', '$lte'].forEach((key) => { | ||
let val = filter[key]; | ||
if (val === undefined) { | ||
return; | ||
} | ||
val = parseFloat(val); | ||
if (isNaN(val)) { | ||
return; | ||
} | ||
if (!value) { | ||
value = {}; | ||
} | ||
value[key] = val; | ||
}); | ||
if (value) { | ||
return value; | ||
} | ||
} | ||
} | ||
NumberField.views = { | ||
cell: { | ||
@@ -24,50 +84,6 @@ name: 'NumberFieldCell', | ||
exports.plain = Number; | ||
NumberField.plain = Number; | ||
NumberField.options = ['min', 'max']; | ||
NumberField.viewOptions = ['min', 'max', 'format']; | ||
/** | ||
* 初始化Schema | ||
* @param field alaksa.Model中的字段配置 | ||
* @param schema | ||
* @param Model | ||
*/ | ||
exports.initSchema = function (field, schema, Model) { | ||
let options = { | ||
type: Number | ||
}; | ||
[ | ||
'get', | ||
'set', | ||
'default', | ||
'index', | ||
'required', | ||
'select', | ||
'min', | ||
'max' | ||
].forEach(function (key) { | ||
if (field[key] !== undefined) { | ||
options[key] = field[key]; | ||
} | ||
}); | ||
schema.path(field.path, options); | ||
Model.underscoreMethod(field.path, 'format', function (format) { | ||
if (format) { | ||
return numeral(this.get(field.path)).format(format); | ||
} | ||
return this.get(field.path); | ||
}); | ||
}; | ||
/** | ||
* alaska-admin-view 前端控件初始化参数 | ||
* @param field | ||
* @param Model | ||
*/ | ||
exports.viewOptions = function (field, Model) { | ||
let options = alaska.Field.viewOptions.apply(this, arguments); | ||
options.min = field.min instanceof Array ? field.min[0] : field.min; | ||
options.max = field.max instanceof Array ? field.max[0] : field.max; | ||
options.format = field.format || ''; | ||
return options; | ||
}; | ||
module.exports = NumberField; |
@@ -133,10 +133,13 @@ 'use strict'; | ||
var field = this.props.field; | ||
var unfomarted = undefined; | ||
if (field.format) { | ||
var value = numeral(this.props.value).format(field.format); | ||
this.setState({ value: value, display: value }); | ||
var unfomarted = numeral().unformat(value); | ||
if (unfomarted != this.props.value) { | ||
this.props.onChange && this.props.onChange(unfomarted); | ||
} | ||
unfomarted = numeral().unformat(value); | ||
} else { | ||
unfomarted = parseFloat(this.props.value) || ''; | ||
} | ||
if (unfomarted !== this.props.value) { | ||
this.props.onChange && this.props.onChange(unfomarted); | ||
} | ||
} | ||
@@ -143,0 +146,0 @@ }, { |
{ | ||
"name": "alaska-field-number", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "Alaska number field", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18774
294