312 lines
8.9 KiB
JavaScript
312 lines
8.9 KiB
JavaScript
"use strict";
|
|
|
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.default = exports.styles = void 0;
|
|
|
|
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
|
|
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
|
|
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
|
|
|
|
var _getPrototypeOf = _interopRequireDefault(require("@babel/runtime/core-js/object/get-prototype-of"));
|
|
|
|
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
|
|
|
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
|
|
|
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
|
|
|
|
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
|
|
|
|
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
|
|
|
|
var _react = _interopRequireDefault(require("react"));
|
|
|
|
var _propTypes = _interopRequireDefault(require("prop-types"));
|
|
|
|
var _classnames = _interopRequireDefault(require("classnames"));
|
|
|
|
var _withStyles = _interopRequireDefault(require("../styles/withStyles"));
|
|
|
|
var _Input = require("../Input/Input");
|
|
|
|
var _helpers = require("../utils/helpers");
|
|
|
|
var _reactHelpers = require("../utils/reactHelpers");
|
|
|
|
var 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%'
|
|
}
|
|
};
|
|
};
|
|
/**
|
|
* Provides context such as filled/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
|
|
*/
|
|
|
|
|
|
exports.styles = styles;
|
|
|
|
var FormControl =
|
|
/*#__PURE__*/
|
|
function (_React$Component) {
|
|
(0, _inherits2.default)(FormControl, _React$Component);
|
|
|
|
function FormControl(props, context) {
|
|
var _this;
|
|
|
|
(0, _classCallCheck2.default)(this, FormControl);
|
|
_this = (0, _possibleConstructorReturn2.default)(this, (FormControl.__proto__ || (0, _getPrototypeOf.default)(FormControl)).call(this, props, context)); // We need to iterate through the children and find the Input in order
|
|
// to fully support server side rendering.
|
|
|
|
Object.defineProperty((0, _assertThisInitialized2.default)(_this), "state", {
|
|
configurable: true,
|
|
enumerable: true,
|
|
writable: true,
|
|
value: {
|
|
adornedStart: false,
|
|
filled: false,
|
|
focused: false
|
|
}
|
|
});
|
|
Object.defineProperty((0, _assertThisInitialized2.default)(_this), "handleFocus", {
|
|
configurable: true,
|
|
enumerable: true,
|
|
writable: true,
|
|
value: function value(event) {
|
|
if (_this.props.onFocus) {
|
|
_this.props.onFocus(event);
|
|
}
|
|
|
|
_this.setState(function (state) {
|
|
return !state.focused ? {
|
|
focused: true
|
|
} : null;
|
|
});
|
|
}
|
|
});
|
|
Object.defineProperty((0, _assertThisInitialized2.default)(_this), "handleBlur", {
|
|
configurable: true,
|
|
enumerable: true,
|
|
writable: true,
|
|
value: function value(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);
|
|
}
|
|
|
|
_this.setState(function (state) {
|
|
return state.focused ? {
|
|
focused: false
|
|
} : null;
|
|
});
|
|
}
|
|
});
|
|
Object.defineProperty((0, _assertThisInitialized2.default)(_this), "handleDirty", {
|
|
configurable: true,
|
|
enumerable: true,
|
|
writable: true,
|
|
value: function value() {
|
|
if (!_this.state.filled) {
|
|
_this.setState({
|
|
filled: true
|
|
});
|
|
}
|
|
}
|
|
});
|
|
Object.defineProperty((0, _assertThisInitialized2.default)(_this), "handleClean", {
|
|
configurable: true,
|
|
enumerable: true,
|
|
writable: true,
|
|
value: function value() {
|
|
if (_this.state.filled) {
|
|
_this.setState({
|
|
filled: false
|
|
});
|
|
}
|
|
}
|
|
});
|
|
var children = _this.props.children;
|
|
|
|
if (children) {
|
|
_react.default.Children.forEach(children, function (child) {
|
|
if (!(0, _reactHelpers.isMuiElement)(child, ['Input', 'Select'])) {
|
|
return;
|
|
}
|
|
|
|
if ((0, _Input.isFilled)(child.props, true)) {
|
|
_this.state.filled = true;
|
|
}
|
|
|
|
var input = (0, _reactHelpers.isMuiElement)(child, ['Select']) ? child.props.input : child;
|
|
|
|
if (input && (0, _Input.isAdornedStart)(input.props)) {
|
|
_this.state.adornedStart = true;
|
|
}
|
|
});
|
|
}
|
|
|
|
return _this;
|
|
}
|
|
|
|
(0, _createClass2.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,
|
|
filled = _state.filled,
|
|
focused = _state.focused;
|
|
return {
|
|
muiFormControl: {
|
|
adornedStart: adornedStart,
|
|
disabled: disabled,
|
|
error: error,
|
|
filled: filled,
|
|
focused: focused,
|
|
margin: margin,
|
|
onBlur: this.handleBlur,
|
|
onEmpty: this.handleClean,
|
|
onFilled: this.handleDirty,
|
|
onFocus: this.handleFocus,
|
|
required: required
|
|
}
|
|
};
|
|
}
|
|
}, {
|
|
key: "render",
|
|
value: function render() {
|
|
var _classNames;
|
|
|
|
var _props2 = this.props,
|
|
classes = _props2.classes,
|
|
className = _props2.className,
|
|
Component = _props2.component,
|
|
disabled = _props2.disabled,
|
|
error = _props2.error,
|
|
fullWidth = _props2.fullWidth,
|
|
margin = _props2.margin,
|
|
required = _props2.required,
|
|
other = (0, _objectWithoutProperties2.default)(_props2, ["classes", "className", "component", "disabled", "error", "fullWidth", "margin", "required"]);
|
|
return _react.default.createElement(Component, (0, _extends2.default)({
|
|
className: (0, _classnames.default)(classes.root, (_classNames = {}, (0, _defineProperty2.default)(_classNames, classes["margin".concat((0, _helpers.capitalize)(margin))], margin !== 'none'), (0, _defineProperty2.default)(_classNames, classes.fullWidth, fullWidth), _classNames), className)
|
|
}, other, {
|
|
onFocus: this.handleFocus,
|
|
onBlur: this.handleBlur
|
|
}));
|
|
}
|
|
}]);
|
|
return FormControl;
|
|
}(_react.default.Component);
|
|
|
|
FormControl.propTypes = process.env.NODE_ENV !== "production" ? {
|
|
/**
|
|
* The contents of the form control.
|
|
*/
|
|
children: _propTypes.default.node,
|
|
|
|
/**
|
|
* Useful to extend the style applied to components.
|
|
*/
|
|
classes: _propTypes.default.object.isRequired,
|
|
|
|
/**
|
|
* @ignore
|
|
*/
|
|
className: _propTypes.default.string,
|
|
|
|
/**
|
|
* The component used for the root node.
|
|
* Either a string to use a DOM element or a component.
|
|
*/
|
|
component: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.func]),
|
|
|
|
/**
|
|
* If `true`, the label, input and helper text should be displayed in a disabled state.
|
|
*/
|
|
disabled: _propTypes.default.bool,
|
|
|
|
/**
|
|
* If `true`, the label should be displayed in an error state.
|
|
*/
|
|
error: _propTypes.default.bool,
|
|
|
|
/**
|
|
* If `true`, the component will take up the full width of its container.
|
|
*/
|
|
fullWidth: _propTypes.default.bool,
|
|
|
|
/**
|
|
* If `dense` or `normal`, will adjust vertical spacing of this and contained components.
|
|
*/
|
|
margin: _propTypes.default.oneOf(['none', 'dense', 'normal']),
|
|
|
|
/**
|
|
* @ignore
|
|
*/
|
|
onBlur: _propTypes.default.func,
|
|
|
|
/**
|
|
* @ignore
|
|
*/
|
|
onFocus: _propTypes.default.func,
|
|
|
|
/**
|
|
* If `true`, the label will indicate that the input is required.
|
|
*/
|
|
required: _propTypes.default.bool
|
|
} : {};
|
|
FormControl.defaultProps = {
|
|
component: 'div',
|
|
disabled: false,
|
|
error: false,
|
|
fullWidth: false,
|
|
margin: 'none',
|
|
required: false
|
|
};
|
|
FormControl.childContextTypes = {
|
|
muiFormControl: _propTypes.default.object
|
|
};
|
|
|
|
var _default = (0, _withStyles.default)(styles, {
|
|
name: 'MuiFormControl'
|
|
})(FormControl);
|
|
|
|
exports.default = _default; |