UNPKG

material-ui-cordova

Version:

React components that implement Google's Material Design.

301 lines (241 loc) 9.4 kB
'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); 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 _Input = require('../Input/Input'); var _reactHelpers = require('../utils/reactHelpers'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var babelPluginFlowReactPropTypes_proptype_Node = require('react').babelPluginFlowReactPropTypes_proptype_Node || require('prop-types').any; var babelPluginFlowReactPropTypes_proptype_ElementType = require('react').babelPluginFlowReactPropTypes_proptype_ElementType || require('prop-types').any; var styles = exports.styles = function styles(theme) { return { root: { display: 'inline-flex', flexDirection: 'column', position: 'relative', // Reset fieldset default style minWidth: 0, padding: 0, margin: 0, border: 0 }, marginNormal: { marginTop: theme.spacing.unit * 2, marginBottom: theme.spacing.unit }, marginDense: { marginTop: theme.spacing.unit, marginBottom: theme.spacing.unit / 2 }, fullWidth: { width: '100%' } }; }; var babelPluginFlowReactPropTypes_proptype_Margin = require('prop-types').oneOf(['none', 'dense', 'normal']); var babelPluginFlowReactPropTypes_proptype_Props = { /** * The contents of the form control. */ children: typeof babelPluginFlowReactPropTypes_proptype_Node === 'function' ? babelPluginFlowReactPropTypes_proptype_Node : require('prop-types').shape(babelPluginFlowReactPropTypes_proptype_Node), /** * Useful to extend the style applied to components. */ classes: require('prop-types').object, /** * @ignore */ className: require('prop-types').string, /** * The component used for the root node. * Either a string to use a DOM element or a component. */ component: typeof babelPluginFlowReactPropTypes_proptype_ElementType === 'function' ? babelPluginFlowReactPropTypes_proptype_ElementType.isRequired ? babelPluginFlowReactPropTypes_proptype_ElementType.isRequired : babelPluginFlowReactPropTypes_proptype_ElementType : require('prop-types').shape(babelPluginFlowReactPropTypes_proptype_ElementType).isRequired, /** * If `true`, the label, input and helper text should be displayed in a disabled state. */ disabled: require('prop-types').bool.isRequired, /** * If `true`, the label should be displayed in an error state. */ error: require('prop-types').bool.isRequired, /** * If `true`, the component, as well as its children, * will take up the full width of its container. */ fullWidth: require('prop-types').bool.isRequired, /** * If `dense` or `normal`, will adjust vertical spacing of this and contained components. */ margin: require('prop-types').oneOf(['none', 'dense', 'normal']).isRequired, /** * @ignore */ onBlur: require('prop-types').func, /** * @ignore */ onFocus: require('prop-types').func, /** * If `true`, the label will indicate that the input is required. */ required: require('prop-types').bool.isRequired }; /** * Provides context such as dirty/focused/error/required for form inputs. * Relying on the context provides high flexibilty and ensures that the state always stay * consitent across the children of the `FormControl`. * This context is used by the following components: * - FormLabel * - FormHelperText * - Input * - InputLabel */ var FormControl = function (_React$Component) { (0, _inherits3.default)(FormControl, _React$Component); function FormControl(props, context) { (0, _classCallCheck3.default)(this, FormControl); // We need to iterate through the children and find the Input in order // to fully support server side rendering. var _this = (0, _possibleConstructorReturn3.default)(this, (FormControl.__proto__ || (0, _getPrototypeOf2.default)(FormControl)).call(this, props, context)); _this.state = { adornedStart: false, dirty: false, focused: false }; _this.handleFocus = function (event) { if (_this.props.onFocus) { _this.props.onFocus(event); } if (!_this.state.focused) { _this.setState({ focused: true }); } }; _this.handleBlur = function (event) { // The event might be undefined. // For instance, a child component might call this hook // when an input is disabled but still having the focus. if (_this.props.onBlur && event) { _this.props.onBlur(event); } if (_this.state.focused) { _this.setState({ focused: false }); } }; _this.handleDirty = function () { if (!_this.state.dirty) { _this.setState({ dirty: true }); } }; _this.handleClean = function () { if (_this.state.dirty) { _this.setState({ dirty: false }); } }; var children = _this.props.children; if (children) { _react2.default.Children.forEach(children, function (child) { if ((0, _reactHelpers.isMuiElement)(child, ['Input', 'Select']) && (0, _Input.isDirty)(child.props, true)) { _this.state.dirty = true; } if ((0, _reactHelpers.isMuiElement)(child, ['Input']) && (0, _Input.isAdornedStart)(child.props)) { _this.state.adornedStart = true; } }); } return _this; } (0, _createClass3.default)(FormControl, [{ key: 'getChildContext', value: function getChildContext() { var _props = this.props, disabled = _props.disabled, error = _props.error, required = _props.required, margin = _props.margin; var _state = this.state, adornedStart = _state.adornedStart, dirty = _state.dirty, focused = _state.focused; return { muiFormControl: { adornedStart: adornedStart, dirty: dirty, disabled: disabled, error: error, focused: focused, margin: margin, required: required, onDirty: this.handleDirty, onClean: this.handleClean, onFocus: this.handleFocus, onBlur: this.handleBlur } }; } }, { key: 'render', value: function render() { var _classNames; var _props2 = this.props, children = _props2.children, classes = _props2.classes, className = _props2.className, _props2$component = _props2.component, ComponentProp = _props2$component === undefined ? 'div' : _props2$component, disabled = _props2.disabled, error = _props2.error, fullWidth = _props2.fullWidth, margin = _props2.margin, other = (0, _objectWithoutProperties3.default)(_props2, ['children', 'classes', 'className', 'component', 'disabled', 'error', 'fullWidth', 'margin']); return _react2.default.createElement( ComponentProp, (0, _extends3.default)({ className: (0, _classnames2.default)(classes.root, (_classNames = {}, (0, _defineProperty3.default)(_classNames, classes.marginNormal, margin === 'normal'), (0, _defineProperty3.default)(_classNames, classes.marginDense, margin === 'dense'), (0, _defineProperty3.default)(_classNames, classes.fullWidth, fullWidth), _classNames), className) }, other, { onFocus: this.handleFocus, onBlur: this.handleBlur }), children ); } }]); return FormControl; }(_react2.default.Component); FormControl.defaultProps = { component: 'div', disabled: false, error: false, fullWidth: false, margin: 'none', required: false }; FormControl.childContextTypes = { muiFormControl: _propTypes2.default.object.isRequired }; exports.default = (0, _withStyles2.default)(styles, { name: 'MuiFormControl' })(FormControl);