ldx-widgets
Version:
widgets
187 lines (180 loc) • 6.12 kB
JavaScript
(function() {
var ENTER, isEqual, makeGuid, moment;
isEqual = require('lodash/isEqual');
makeGuid = require('../utils').makeGuid;
ENTER = require('../constants/keyboard').ENTER;
moment = require('moment');
module.exports = {
getInitialState: function() {
return {
valueHasChanged: false
};
},
componentWillMount: function() {
return this.inputId = makeGuid();
},
componentDidMount: function() {
var focusOnMount, ref, selectOnMount, validation, value;
ref = this.props, value = ref.value, validation = ref.validation, focusOnMount = ref.focusOnMount, selectOnMount = ref.selectOnMount;
value = this.getDateValue != null ? moment(this.getDateValue()) : value;
this.validate(validation, value);
if (focusOnMount) {
this.focus();
}
if (selectOnMount) {
return this.selectText();
}
},
componentWillReceiveProps: function(nextProps) {
var ref, validation, validationChanged, value;
value = nextProps.value, validation = nextProps.validation;
validationChanged = (typeof validation === 'function' ? false : !isEqual(validation != null ? validation.messages : void 0, (ref = this.props.validation) != null ? ref.messages : void 0));
if (validationChanged) {
return this.validate(validation, value);
}
},
componentWillUnmount: function() {
var isInPopover;
isInPopover = this.props.isInPopover;
return this.context.clearValidationError({
groupId: this.inputId,
isInPopover: isInPopover
});
},
handleChange: function(e) {
var jsonPath, onChange, ref, validation, value, valueHasChanged;
value = e.target.value;
ref = this.props, onChange = ref.onChange, validation = ref.validation, jsonPath = ref.jsonPath;
valueHasChanged = this.state.valueHasChanged;
if (!valueHasChanged) {
this.setState({
valueHasChanged: true
});
}
this.validate(validation, value);
if (typeof onChange === "function") {
onChange(this.getValue(), jsonPath);
}
return this.fireDelayedAction();
},
fireDelayedAction: function() {
var action, delayedActionOnChange, interval, ref, value;
ref = this.props, delayedActionOnChange = ref.delayedActionOnChange, value = ref.value;
if (delayedActionOnChange != null) {
action = delayedActionOnChange.action, interval = delayedActionOnChange.interval;
clearInterval(this.delayedActionTimer);
return this.delayedActionTimer = setTimeout((function(_this) {
return function() {
return action(value);
};
})(this), interval);
}
},
validate: function(validation, value) {
var isInPopover, ref, tabId, validationError;
if (validation === false) {
return;
}
if (typeof validation === 'function') {
validationError = validation(value);
} else {
validationError = validation;
}
ref = this.props, isInPopover = ref.isInPopover, tabId = ref.tabId;
if (validationError != null) {
return this.context.addValidationError({
anchor: this.refs.errorAnchor,
error: validationError,
groupId: this.inputId,
isInPopover: isInPopover,
tabId: tabId
});
} else {
return this.context.clearValidationError({
groupId: this.inputId,
isInPopover: isInPopover
});
}
},
handleKeyUp: function(e) {
var onEnterKey, onKeyUp, ref;
ref = this.props, onEnterKey = ref.onEnterKey, onKeyUp = ref.onKeyUp;
if (e.keyCode === ENTER) {
if (typeof onEnterKey === "function") {
onEnterKey(e);
}
}
return typeof onKeyUp === "function" ? onKeyUp(e) : void 0;
},
getValue: function() {
var i, item, len, options, ref, returnFullObjects, returnNull, textTransform, type, value, valueField, valueObj;
ref = this.props, returnFullObjects = ref.returnFullObjects, textTransform = ref.textTransform, options = ref.options, valueField = ref.valueField, returnNull = ref.returnNull, type = ref.type;
if (this.getDateValue != null) {
return this.getDateValue();
}
value = (function(_this) {
return function() {
if (_this.value != null) {
return _this.value;
} else if (_this.refs.input.getValue != null) {
return _this.refs.input.getValue();
} else {
return _this.refs.input.value;
}
};
})(this)();
if ((textTransform != null) && value) {
value = textTransform(value);
}
if (returnFullObjects) {
valueField = valueField || 'value';
for (i = 0, len = options.length; i < len; i++) {
item = options[i];
if (!(item[valueField] === value)) {
continue;
}
valueObj = item;
break;
}
return valueObj || '';
}
if (returnNull && value === '') {
return null;
} else {
return value;
}
},
clear: function() {
return this.props.onChange('', this.props.jsonPath);
},
focus: function() {
return this.refs.input.focus();
},
blur: function() {
return this.refs.input.blur();
},
selectText: function() {
var input;
input = this.refs.input;
return input.setSelectionRange(0, input.value.length);
},
handleErrorMouseOver: function() {
var isInPopover;
isInPopover = this.props.isInPopover;
return this.context.toggleValidationError({
groupId: this.inputId,
status: true,
isInPopover: isInPopover
});
},
handleErrorMouseOut: function() {
var isInPopover;
isInPopover = this.props.isInPopover;
return this.context.toggleValidationError({
groupId: this.inputId,
status: false,
isInPopover: isInPopover
});
}
};
}).call(this);