UNPKG

ldx-widgets

Version:

widgets

147 lines (141 loc) 4.96 kB
(function() { var ENTER, _, makeGuid; _ = require('lodash'); makeGuid = require('../utils').makeGuid; ENTER = require('../constants/keyboard').ENTER; module.exports = { getInitialState: function() { return { valueHasChanged: false }; }, componentWillMount: function() { return this.inputId = makeGuid(); }, componentDidMount: function() { var focusOnMount, ref, validation, value; ref = this.props, value = ref.value, validation = ref.validation, focusOnMount = ref.focusOnMount; this.validate(validation, value); if (focusOnMount) { return this.focus(); } }, 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(this.inputId, isInPopover); }, handleChange: function(e) { var onChange, ref, validation, value, valueHasChanged; value = e.target.value; ref = this.props, onChange = ref.onChange, validation = ref.validation; valueHasChanged = this.state.valueHasChanged; if (!valueHasChanged) { this.setState({ valueHasChanged: true }); } this.validate(validation, value); if (typeof onChange === "function") { onChange(value); } 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, validationError; if (validation === false) { return; } if (typeof validation === 'function') { validationError = validation(value); } else { validationError = validation; } isInPopover = this.props.isInPopover; if (validationError != null) { return this.context.addValidationError(this.refs.errorAnchor, validationError, this.inputId, isInPopover); } else { return this.context.clearValidationError(this.inputId, 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, textTransform, value, valueField, valueObj; ref = this.props, returnFullObjects = ref.returnFullObjects, textTransform = ref.textTransform, options = ref.options, valueField = ref.valueField; value = (function(_this) { return function() { 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 || null; } return value; }, clear: function() { return this.props.onChange(''); }, focus: function() { return this.refs.input.focus(); }, blur: function() { return this.refs.input.blur(); }, handleErrorMouseOver: function() { var isInPopover; isInPopover = this.props.isInPopover; return this.context.toggleValidationError(this.inputId, true, isInPopover); }, handleErrorMouseOut: function() { var isInPopover; isInPopover = this.props.isInPopover; return this.context.toggleValidationError(this.inputId, false, isInPopover); } }; }).call(this);