Skip to content

Commit

Permalink
release v2.7.2
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Jun 30, 2015
1 parent 1e2eca9 commit 87229ec
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 22 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-widgets",
"version": "2.7.1",
"version": "2.7.2",
"main": "dist/react-widgets.js",
"description": "A set of input widgets for React",
"homepage": "http://jquense.github.io/react-widgets/docs",
Expand Down
10 changes: 5 additions & 5 deletions dist/react-widgets.js

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions lib/NumberInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ module.exports = React.createClass({
min: React.PropTypes.number,

onChange: React.PropTypes.func.isRequired,
onKeyDown: React.PropTypes.func },
onKeyDown: React.PropTypes.func
},

getDefaultProps: function getDefaultProps() {
return {
Expand Down Expand Up @@ -72,17 +73,17 @@ module.exports = React.createClass({
_change: function _change(e) {
var val = e.target.value,
number = this.props.parse(e.target.value, this.props.culture),
hasMin = this.props.min && isFinite(this.props.min);
valid = this.isValid(number);

if (val == null || val.trim() === '') return this.props.onChange(null);

if (this.isValid(number) && number !== this.props.value && !this.isAtDelimiter(number, val)) return this.props.onChange(number);
if (valid && number !== this.props.value && !this.isAtDelimiter(number, val)) return this.props.onChange(number);

//console.log(val !== 0 && !val)
this.current(e.target.value);
if (!isNaN(number) || this.isAtDelimiter(number, val)) this.current(e.target.value);
},

_finish: function _finish(e) {
_finish: function _finish() {
var str = this.state.stringValue,
number = this.props.parse(str, this.props.culture);

Expand Down
47 changes: 39 additions & 8 deletions lib/NumberPicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ var React = require('react'),
createUncontrolledWidget = require('uncontrollable'),
directions = require('./util/constants').directions,
repeater = require('./util/repeater'),
localizers = require('./util/configuration').locale,
Input = require('./NumberInput');

var Btn = require('./WidgetButton'),
propTypes = {
var Btn = require('./WidgetButton');

var format = function format(props) {
return props.format || localizers.number.formats['default'];
};

var propTypes = {

// -- controlled props -----------
value: React.PropTypes.number,
Expand All @@ -25,6 +31,8 @@ var Btn = require('./WidgetButton'),
max: React.PropTypes.number,
step: React.PropTypes.number,

precision: React.PropTypes.number,

culture: React.PropTypes.string,

format: CustomPropTypes.numberFormat,
Expand Down Expand Up @@ -168,12 +176,11 @@ var NumberPicker = React.createClass({

//allow for styling, focus stealing keeping me from the normal what have you
_mouseDown: _.ifNotDisabled(function (dir) {
var val = dir === directions.UP ? (this.props.value || 0) + this.props.step : (this.props.value || 0) - this.props.step;

val = this.constrainValue(val);
var method = dir === directions.UP ? this.increment : this.decrement;

this.setState({ active: dir });
this.change(val);

var val = method.call(this);

if (!(dir === directions.UP && val === this.props.max || dir === directions.DOWN && val === this.props.min)) {
if (!this._cancelRepeater) this._cancelRepeater = repeater(this._mouseDown.bind(null, dir));
Expand Down Expand Up @@ -212,11 +219,21 @@ var NumberPicker = React.createClass({
}),

increment: function increment() {
this.change(this.constrainValue((this.props.value || 0) + this.props.step));
return this.step(this.props.step);
},

decrement: function decrement() {
this.change(this.constrainValue((this.props.value || 0) - this.props.step));
return this.step(-this.props.step);
},

step: function step(amount) {
var value = (this.props.value || 0) + amount;

var decimals = this.props.precision != null ? this.props.precision : localizers.number.precision(format(this.props));

this.change(decimals != null ? round(value, decimals) : value);

return value;
},

change: function change(val) {
Expand All @@ -236,6 +253,20 @@ var NumberPicker = React.createClass({

});

// thank you kendo ui core
// https://github.com/telerik/kendo-ui-core/blob/master/src/kendo.core.js#L1036
function round(value, precision) {
precision = precision || 0;

value = ('' + value).split('e');
value = Math.round(+(value[0] + 'e' + (value[1] ? +value[1] + precision : precision)));

value = ('' + value).split('e');
value = +(value[0] + 'e' + (value[1] ? +value[1] - precision : -precision));

return value.toFixed(precision);
}

module.exports = createUncontrolledWidget(NumberPicker, { value: 'onChange' });

module.exports.BaseNumberPicker = NumberPicker;
6 changes: 4 additions & 2 deletions lib/TimeList.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ module.exports = React.createClass({
start = values.min,
startDay = dates.date(start);

while (i < 100 && (dates.date(start) === startDay && dates.lte(start, values.max))) {
while (dates.date(start) === startDay && dates.lte(start, values.max)) {
i++;
times.push({ date: start, label: localizers.date.format(start, format(props), props.culture) });
start = dates.add(start, props.step || 30, 'minutes');
Expand Down Expand Up @@ -190,4 +190,6 @@ module.exports = React.createClass({
_this2._searchTerm = '';
if (item) cb(item);
}, this.props.delay);
} });
}

});
21 changes: 21 additions & 0 deletions lib/globalize-localizers.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ function globalizeDateLocalizer(globalize) {
}

function globalizeNumberLocalizer(globalize) {

function getCulture(culture) {
return culture ? (localizer.globalize || globalize).findClosestCulture(culture) : (localizer.globalize || globalize).culture();
}

var localizer = new NumberLocalizer({

formats: {
Expand All @@ -85,6 +90,22 @@ function globalizeNumberLocalizer(globalize) {

format: function format(value, _format2, culture) {
return (this.globalize || globalize).format(value, _format2, culture);
},

precision: function precision(format, _culture) {
var culture = getCulture(_culture),
numFormat = culture.numberFormat;

if (typeof format === 'string') {
if (format.length > 1) return parseFloat(format.substr(1));

if (format.indexOf('p') !== -1) numFormat = numFormat.percent;
if (format.indexOf('c') !== -1) numFormat = numFormat.curency;

return numFormat.decimals || null;
}

return null;
}
});

Expand Down
8 changes: 8 additions & 0 deletions lib/util/localizers.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,24 @@ var NumberLocalizer = function NumberLocalizer(_ref) {

var format = _ref.format;
var parse = _ref.parse;
var precision = _ref.precision;
var formats = _ref.formats;
var propType = _ref.propType;
babelHelpers.classCallCheck(this, NumberLocalizer);

invariant(typeof format === 'function', 'number localizer `format(..)` must be a function');
invariant(typeof parse === 'function', 'number localizer `parse(..)` must be a function');

// invariant(typeof precision === 'function'
// , 'number localizer `precision(..)` must be a function')

checkFormats(REQUIRED_NUMBER_FORMATS, formats);

this.propType = propType || localePropType;
this.formats = formats;
this.precision = precision || function () {
return null;
};

this.format = function (value, str, culture) {
return _format(_this, format, value, str, culture);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-widgets",
"version": "2.7.1",
"version": "2.7.2",
"description": "An à la carte set of polished, extensible, and accessible inputs built for React",
"main": "lib/index.js",
"author": {
Expand Down

0 comments on commit 87229ec

Please sign in to comment.