'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.styles = undefined; var _extends2 = require('babel-runtime/helpers/extends'); var _extends3 = _interopRequireDefault(_extends2); var _defineProperty2 = require('babel-runtime/helpers/defineProperty'); var _defineProperty3 = _interopRequireDefault(_defineProperty2); var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties'); var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2); var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _createClass2 = require('babel-runtime/helpers/createClass'); var _createClass3 = _interopRequireDefault(_createClass2); var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); var _inherits2 = require('babel-runtime/helpers/inherits'); var _inherits3 = _interopRequireDefault(_inherits2); exports.hasValue = hasValue; exports.isDirty = isDirty; exports.isAdornedStart = isAdornedStart; var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _classnames = require('classnames'); var _classnames2 = _interopRequireDefault(_classnames); var _withStyles = require('../styles/withStyles'); var _withStyles2 = _interopRequireDefault(_withStyles); var _Textarea = require('./Textarea'); var _Textarea2 = _interopRequireDefault(_Textarea); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } // Supports determination of isControlled(). // Controlled input accepts its current value as a prop. // // @see https://facebook.github.io/react/docs/forms.html#controlled-components // @param value // @returns {boolean} true if string (including '') or number (including zero) function hasValue(value) { return value != null && !(Array.isArray(value) && value.length === 0); } // Determine if field is dirty (a.k.a. filled). // // Response determines if label is presented above field or as placeholder. // // @param obj // @param SSR // @returns {boolean} False when not present or empty string. // True when any number or string with length. function isDirty(obj) { var SSR = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; return obj && (hasValue(obj.value) && obj.value !== '' || SSR && hasValue(obj.defaultValue) && obj.defaultValue !== ''); } // Determine if an Input is adorned on start. // It's corresponding to the left with LTR. // // @param obj // @returns {boolean} False when no adornments. // True when adorned at the start. function isAdornedStart(obj) { return obj.startAdornment; } var styles = exports.styles = function styles(theme) { var light = theme.palette.type === 'light'; var placeholder = { color: 'currentColor', opacity: light ? 0.42 : 0.5, transition: theme.transitions.create('opacity', { duration: theme.transitions.duration.shorter }) }; var placeholderHidden = { opacity: 0 }; var placeholderVisible = { opacity: light ? 0.42 : 0.5 }; var bottomLineColor = light ? 'rgba(0, 0, 0, 0.42)' : 'rgba(255, 255, 255, 0.7)'; return { root: { // Mimics the default input display property used by browsers for an input. display: 'inline-flex', position: 'relative', fontFamily: theme.typography.fontFamily, color: light ? 'rgba(0, 0, 0, 0.87)' : theme.palette.common.white, fontSize: theme.typography.pxToRem(16), lineHeight: '1.1875em' // Reset (19px), match the native input line-height }, formControl: { 'label + &': { marginTop: theme.spacing.unit * 2 } }, inkbar: { '&:after': { backgroundColor: theme.palette.primary[light ? 'dark' : 'light'], left: 0, bottom: 0, // Doing the other way around crash on IE11 "''" https://github.com/cssinjs/jss/issues/242 content: '""', height: 2, position: 'absolute', right: 0, transform: 'scaleX(0)', transition: theme.transitions.create('transform', { duration: theme.transitions.duration.shorter, easing: theme.transitions.easing.easeOut }), pointerEvents: 'none' // Transparent to the hover style. }, '&$focused:after': { transform: 'scaleX(1)' } }, error: { '&:after': { backgroundColor: theme.palette.error.main, transform: 'scaleX(1)' // error is always underlined in red } }, focused: {}, disabled: { color: theme.palette.text.disabled }, underline: { '&:before': { backgroundColor: bottomLineColor, left: 0, bottom: 0, // Doing the other way around crash on IE11 "''" https://github.com/cssinjs/jss/issues/242 content: '""', height: 1, position: 'absolute', right: 0, transition: theme.transitions.create('background-color', { duration: theme.transitions.duration.shorter }), pointerEvents: 'none' // Transparent to the hover style. }, '&:hover:not($disabled):before': { backgroundColor: theme.palette.text.primary, height: 2 }, '&$disabled:before': { background: 'transparent', backgroundImage: 'linear-gradient(to right, ' + bottomLineColor + ' 33%, transparent 0%)', backgroundPosition: 'left top', backgroundRepeat: 'repeat-x', backgroundSize: '5px 1px' } }, multiline: { padding: theme.spacing.unit - 2 + 'px 0 ' + (theme.spacing.unit - 1) + 'px' }, fullWidth: { width: '100%' }, input: { font: 'inherit', color: 'currentColor', padding: theme.spacing.unit - 2 + 'px 0 ' + (theme.spacing.unit - 1) + 'px', border: 0, boxSizing: 'content-box', verticalAlign: 'middle', background: 'none', margin: 0, // Reset for Safari // Remove grey highlight WebkitTapHighlightColor: 'transparent', display: 'block', // Make the flex item shrink with Firefox minWidth: 0, width: '100%', '&::-webkit-input-placeholder': placeholder, '&::-moz-placeholder': placeholder, // Firefox 19+ '&:-ms-input-placeholder': placeholder, // IE 11 '&::-ms-input-placeholder': placeholder, // Edge '&:focus': { outline: 0 }, // Reset Firefox invalid required input style '&:invalid': { boxShadow: 'none' }, '&::-webkit-search-decoration': { // Remove the padding when type=search. '-webkit-appearance': 'none' }, // Show and hide the placeholder logic 'label[data-shrink=false] + $formControl &': { '&::-webkit-input-placeholder': placeholderHidden, '&::-moz-placeholder': placeholderHidden, // Firefox 19+ '&:-ms-input-placeholder': placeholderHidden, // IE 11 '&::-ms-input-placeholder': placeholderHidden, // Edge '&:focus::-webkit-input-placeholder': placeholderVisible, '&:focus::-moz-placeholder': placeholderVisible, // Firefox 19+ '&:focus:-ms-input-placeholder': placeholderVisible, // IE 11 '&:focus::-ms-input-placeholder': placeholderVisible // Edge } }, inputDense: { paddingTop: theme.spacing.unit / 2 - 1 }, inputDisabled: { opacity: 1 // Reset iOS opacity }, inputType: { // type="date" or type="time", etc. have specific styles we need to reset. height: '1.1875em' // Reset (19px), match the native input line-height }, inputMultiline: { resize: 'none', padding: 0 }, inputSearch: { // Improve type search style. '-moz-appearance': 'textfield', '-webkit-appearance': 'textfield' } }; }; function formControlState(props, context) { var disabled = props.disabled; var error = props.error; var margin = props.margin; if (context && context.muiFormControl) { if (typeof disabled === 'undefined') { disabled = context.muiFormControl.disabled; } if (typeof error === 'undefined') { error = context.muiFormControl.error; } if (typeof margin === 'undefined') { margin = context.muiFormControl.margin; } } return { disabled: disabled, error: error, margin: margin }; } var Input = function (_React$Component) { (0, _inherits3.default)(Input, _React$Component); function Input() { var _ref; var _temp, _this, _ret; (0, _classCallCheck3.default)(this, Input); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = Input.__proto__ || (0, _getPrototypeOf2.default)(Input)).call.apply(_ref, [this].concat(args))), _this), _this.state = { focused: false }, _this.input = null, _this.handleFocus = function (event) { // Fix an bug with IE11 where the focus/blur events are triggered // while the input is disabled. if (formControlState(_this.props, _this.context).disabled) { event.stopPropagation(); return; } _this.setState({ focused: true }); if (_this.props.onFocus) { _this.props.onFocus(event); } }, _this.handleBlur = function (event) { _this.setState({ focused: false }); if (_this.props.onBlur) { _this.props.onBlur(event); } }, _this.handleChange = function (event) { if (!_this.isControlled) { _this.checkDirty(_this.input); } // Perform in the willUpdate if (_this.props.onChange) { _this.props.onChange(event); } }, _this.handleRefInput = function (node) { _this.input = node; if (_this.props.inputRef) { _this.props.inputRef(node); } else if (_this.props.inputProps && _this.props.inputProps.ref) { _this.props.inputProps.ref(node); } }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret); } (0, _createClass3.default)(Input, [{ key: 'getChildContext', value: function getChildContext() { // We are consuming the parent muiFormControl context. // We don't want a child to consume it a second time. return { muiFormControl: null }; } }, { key: 'componentWillMount', value: function componentWillMount() { this.isControlled = this.props.value != null; if (this.isControlled) { this.checkDirty(this.props); } } }, { key: 'componentDidMount', value: function componentDidMount() { if (!this.isControlled) { this.checkDirty(this.input); } } }, { key: 'componentWillReceiveProps', value: function componentWillReceiveProps(nextProps, nextContext) { // The blur won't fire when the disabled state is set on a focused input. // We need to book keep the focused state manually. if (!formControlState(this.props, this.context).disabled && formControlState(nextProps, nextContext).disabled) { this.setState({ focused: false }); } } }, { key: 'componentWillUpdate', value: function componentWillUpdate(nextProps, nextState, nextContext) { if (this.isControlled) { this.checkDirty(nextProps); } // else performed in the onChange // Book keep the focused state. if (!formControlState(this.props, this.context).disabled && formControlState(nextProps, nextContext).disabled) { var muiFormControl = this.context.muiFormControl; if (muiFormControl && muiFormControl.onBlur) { muiFormControl.onBlur(); } } } // Holds the input reference }, { key: 'checkDirty', value: function checkDirty(obj) { var muiFormControl = this.context.muiFormControl; if (isDirty(obj)) { if (muiFormControl && muiFormControl.onDirty) { muiFormControl.onDirty(); } if (this.props.onDirty) { this.props.onDirty(); } return; } if (muiFormControl && muiFormControl.onClean) { muiFormControl.onClean(); } if (this.props.onClean) { this.props.onClean(); } } }, { key: 'render', value: function render() { var _classNames, _classNames2; var _props = this.props, autoComplete = _props.autoComplete, autoFocus = _props.autoFocus, classes = _props.classes, classNameProp = _props.className, defaultValue = _props.defaultValue, disabledProp = _props.disabled, disableUnderline = _props.disableUnderline, endAdornment = _props.endAdornment, errorProp = _props.error, fullWidth = _props.fullWidth, id = _props.id, inputComponent = _props.inputComponent, _props$inputProps = _props.inputProps; _props$inputProps = _props$inputProps === undefined ? {} : _props$inputProps; var inputPropsClassName = _props$inputProps.className, inputPropsProp = (0, _objectWithoutProperties3.default)(_props$inputProps, ['className']), inputRef = _props.inputRef, marginProp = _props.margin, multiline = _props.multiline, name = _props.name, onBlur = _props.onBlur, onChange = _props.onChange, onClean = _props.onClean, onDirty = _props.onDirty, onFocus = _props.onFocus, onKeyDown = _props.onKeyDown, onKeyUp = _props.onKeyUp, placeholder = _props.placeholder, readOnly = _props.readOnly, rows = _props.rows, rowsMax = _props.rowsMax, startAdornment = _props.startAdornment, type = _props.type, value = _props.value, other = (0, _objectWithoutProperties3.default)(_props, ['autoComplete', 'autoFocus', 'classes', 'className', 'defaultValue', 'disabled', 'disableUnderline', 'endAdornment', 'error', 'fullWidth', 'id', 'inputComponent', 'inputProps', 'inputRef', 'margin', 'multiline', 'name', 'onBlur', 'onChange', 'onClean', 'onDirty', 'onFocus', 'onKeyDown', 'onKeyUp', 'placeholder', 'readOnly', 'rows', 'rowsMax', 'startAdornment', 'type', 'value']); var muiFormControl = this.context.muiFormControl; var _formControlState = formControlState(this.props, this.context), disabled = _formControlState.disabled, error = _formControlState.error, margin = _formControlState.margin; var className = (0, _classnames2.default)(classes.root, (_classNames = {}, (0, _defineProperty3.default)(_classNames, classes.disabled, disabled), (0, _defineProperty3.default)(_classNames, classes.error, error), (0, _defineProperty3.default)(_classNames, classes.fullWidth, fullWidth), (0, _defineProperty3.default)(_classNames, classes.focused, this.state.focused), (0, _defineProperty3.default)(_classNames, classes.formControl, muiFormControl), (0, _defineProperty3.default)(_classNames, classes.inkbar, !disableUnderline), (0, _defineProperty3.default)(_classNames, classes.multiline, multiline), (0, _defineProperty3.default)(_classNames, classes.underline, !disableUnderline), _classNames), classNameProp); var inputClassName = (0, _classnames2.default)(classes.input, (_classNames2 = {}, (0, _defineProperty3.default)(_classNames2, classes.inputDisabled, disabled), (0, _defineProperty3.default)(_classNames2, classes.inputType, type !== 'text'), (0, _defineProperty3.default)(_classNames2, classes.inputMultiline, multiline), (0, _defineProperty3.default)(_classNames2, classes.inputSearch, type === 'search'), (0, _defineProperty3.default)(_classNames2, classes.inputDense, margin === 'dense'), _classNames2), inputPropsClassName); var required = muiFormControl && muiFormControl.required === true; var InputComponent = 'input'; var inputProps = (0, _extends3.default)({}, inputPropsProp, { ref: this.handleRefInput }); if (inputComponent) { InputComponent = inputComponent; inputProps = (0, _extends3.default)({ // Rename ref to inputRef as we don't know the // provided `inputComponent` structure. inputRef: this.handleRefInput }, inputProps, { ref: null }); } else if (multiline) { if (rows && !rowsMax) { InputComponent = 'textarea'; } else { inputProps = (0, _extends3.default)({ rowsMax: rowsMax, textareaRef: this.handleRefInput }, inputProps, { ref: null }); InputComponent = _Textarea2.default; } } return _react2.default.createElement( 'div', (0, _extends3.default)({ onBlur: this.handleBlur, onFocus: this.handleFocus, className: className }, other), startAdornment, _react2.default.createElement(InputComponent, (0, _extends3.default)({ autoComplete: autoComplete, autoFocus: autoFocus, className: inputClassName, onChange: this.handleChange, onKeyUp: onKeyUp, onKeyDown: onKeyDown, disabled: disabled, required: required ? true : undefined, value: value, id: id, name: name, defaultValue: defaultValue, placeholder: placeholder, type: type, readOnly: readOnly, rows: rows, 'aria-required': required, 'aria-invalid': error }, inputProps)), endAdornment ); } }]); return Input; }(_react2.default.Component); Input.propTypes = process.env.NODE_ENV !== "production" ? { /** * This property helps users to fill forms faster, especially on mobile devices. * The name can be confusing, as it's more like an autofill. * You can learn more about it here: * https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#autofill */ autoComplete: _propTypes2.default.string, /** * If `true`, the input will be focused during the first mount. */ autoFocus: _propTypes2.default.bool, /** * Useful to extend the style applied to components. */ classes: _propTypes2.default.object.isRequired, /** * The CSS class name of the wrapper element. */ className: _propTypes2.default.string, /** * The default input value, useful when not controlling the component. */ defaultValue: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number]), /** * If `true`, the input will be disabled. */ disabled: _propTypes2.default.bool, /** * If `true`, the input will not have an underline. */ disableUnderline: _propTypes2.default.bool, /** * End `InputAdornment` for this component. */ endAdornment: _propTypes2.default.node, /** * If `true`, the input will indicate an error. This is normally obtained via context from * FormControl. */ error: _propTypes2.default.bool, /** * If `true`, the input will take up the full width of its container. */ fullWidth: _propTypes2.default.bool, /** * The id of the `input` element. */ id: _propTypes2.default.string, /** * The component used for the native input. * Either a string to use a DOM element or a component. */ inputComponent: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.func]), /** * Properties applied to the `input` element. */ inputProps: _propTypes2.default.object, /** * Use that property to pass a ref callback to the native input component. */ inputRef: _propTypes2.default.func, /** * If `dense`, will adjust vertical spacing. This is normally obtained via context from * FormControl. */ margin: _propTypes2.default.oneOf(['dense', 'none']), /** * If `true`, a textarea element will be rendered. */ multiline: _propTypes2.default.bool, /** * Name attribute of the `input` element. */ name: _propTypes2.default.string, /** * @ignore */ onBlur: _propTypes2.default.func, /** * Callback fired when the value is changed. * * @param {object} event The event source of the callback */ onChange: _propTypes2.default.func, /** * TODO */ onClean: _propTypes2.default.func, /** * TODO */ onDirty: _propTypes2.default.func, /** * @ignore */ onFocus: _propTypes2.default.func, /** * @ignore */ onKeyDown: _propTypes2.default.func, /** * @ignore */ onKeyUp: _propTypes2.default.func, /** * The short hint displayed in the input before the user enters a value. */ placeholder: _propTypes2.default.string, /** * @ignore */ readOnly: _propTypes2.default.bool, /** * Number of rows to display when multiline option is set to true. */ rows: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number]), /** * Maximum number of rows to display when multiline option is set to true. */ rowsMax: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number]), /** * Start `InputAdornment` for this component. */ startAdornment: _propTypes2.default.node, /** * Type of the input element. It should be a valid HTML5 input type. */ type: _propTypes2.default.string, /** * The input value, required for a controlled component. */ value: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number, _propTypes2.default.arrayOf(_propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number]))]) } : {}; Input.muiName = 'Input'; Input.defaultProps = { disableUnderline: false, fullWidth: false, multiline: false, type: 'text' }; Input.contextTypes = { muiFormControl: _propTypes2.default.object }; Input.childContextTypes = { muiFormControl: _propTypes2.default.object }; exports.default = (0, _withStyles2.default)(styles, { name: 'MuiInput' })(Input);