preact-material-components
Version:
preact wrapper for "Material Components for the web"
324 lines (265 loc) • 10.6 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.TextField = exports.TextFieldInput = exports.Label = exports.HelperText = void 0;
var _get2 = _interopRequireDefault(require("@babel/runtime/helpers/get"));
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 _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _textfield = require("@material/textfield");
var _preact = require("preact");
var _MaterialComponent4 = _interopRequireDefault(require("../Base/MaterialComponent"));
var _Icon = _interopRequireDefault(require("../Icon"));
var __rest = void 0 && (void 0).__rest || function (s, e) {
var t = {};
for (var p in s) {
if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
}
if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
}
return t;
};
var HelperText =
/*#__PURE__*/
function (_MaterialComponent) {
(0, _inherits2.default)(HelperText, _MaterialComponent);
function HelperText() {
var _this;
(0, _classCallCheck2.default)(this, HelperText);
_this = (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(HelperText).apply(this, arguments));
_this.componentName = 'text-field-helper-text';
_this.mdcProps = ['persistent', 'validation-msg'];
return _this;
}
(0, _createClass2.default)(HelperText, [{
key: "materialDom",
value: function materialDom(props) {
return (0, _preact.h)("p", Object.assign({}, props, {
"aria-hidden": "true"
}), props.children);
}
}]);
return HelperText;
}(_MaterialComponent4.default);
exports.HelperText = HelperText;
var Label =
/*#__PURE__*/
function (_MaterialComponent2) {
(0, _inherits2.default)(Label, _MaterialComponent2);
function Label() {
var _this2;
(0, _classCallCheck2.default)(this, Label);
_this2 = (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(Label).apply(this, arguments));
_this2.componentName = 'floating-label';
_this2.mdcProps = [];
return _this2;
}
(0, _createClass2.default)(Label, [{
key: "materialDom",
value: function materialDom(props) {
return (0, _preact.h)("label", Object.assign({}, props), props.children);
}
}]);
return Label;
}(_MaterialComponent4.default);
exports.Label = Label;
var TextFieldInput =
/*#__PURE__*/
function (_MaterialComponent3) {
(0, _inherits2.default)(TextFieldInput, _MaterialComponent3);
function TextFieldInput() {
var _this3;
(0, _classCallCheck2.default)(this, TextFieldInput);
_this3 = (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(TextFieldInput).apply(this, arguments));
_this3.state = {
jsComponentAttached: false
};
_this3.componentName = 'text-field';
_this3.mdcProps = ['fullwidth', 'textarea', 'dense', 'disabled', 'box', 'outlined'];
_this3.mdcNotifyProps = ['valid', 'disabled'];
return _this3;
}
(0, _createClass2.default)(TextFieldInput, [{
key: "componentDidMount",
value: function componentDidMount() {
var _this4 = this;
(0, _get2.default)((0, _getPrototypeOf2.default)(TextFieldInput.prototype), "componentDidMount", this).call(this);
this.setState({
jsComponentAttached: true
}, function () {
if (_this4.control) {
_this4.MDComponent = new _textfield.MDCTextField(_this4.control);
if (_this4.props.onInit) {
_this4.props.onInit(_this4.MDComponent);
}
if (_this4.props.value) {
_this4.MDComponent.value = _this4.props.value;
}
}
_this4.afterComponentDidMount();
});
}
}, {
key: "componentWillReceiveProps",
value: function componentWillReceiveProps(nextProps) {
(0, _get2.default)((0, _getPrototypeOf2.default)(TextFieldInput.prototype), "componentWillReceiveProps", this).call(this, nextProps);
if (this.MDComponent && nextProps.value && this.props.value !== nextProps.value && this.MDComponent.value !== nextProps.value) {
this.MDComponent.value = nextProps.value;
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
(0, _get2.default)((0, _getPrototypeOf2.default)(TextFieldInput.prototype), "componentWillUnmount", this).call(this);
if (this.MDComponent) {
this.MDComponent.destroy();
}
}
}, {
key: "getValue",
value: function getValue() {
return this.MDComponent ? this.MDComponent.value : null;
}
}, {
key: "materialDom",
value: function materialDom(allprops) {
var className = allprops.className,
outerStyle = allprops.outerStyle,
outlined = allprops.outlined,
props = __rest(allprops, ["className", "outerStyle", "outlined"]);
className = className || '';
if ('leadingIcon' in props) {
className += ' mdc-text-field--box mdc-text-field--with-leading-icon';
}
if ('trailingIcon' in props) {
className += ' mdc-text-field--box mdc-text-field--with-trailing-icon';
}
if ('value' in props && this.state.jsComponentAttached) {
className = [className, 'mdc-text-field--upgraded'].join(' ');
}
if (props.label && props.fullwidth) {
console.log('Passing a "label" prop is not supported when using a "fullwidth" text field.');
} // noinspection RequiredAttributes
return (0, _preact.h)("div", {
className: className,
ref: this.setControlRef,
style: outerStyle
}, props.leadingIcon ? (0, _preact.h)(_Icon.default, {
className: "mdc-text-field__icon"
}, props.leadingIcon) : null, props.textarea ? (0, _preact.h)("textarea", Object.assign({
className: "mdc-text-field__input"
}, props)) : (0, _preact.h)("input", Object.assign({
type: props.type || 'text',
className: "mdc-text-field__input",
placeholder: this.state.jsComponentAttached ? undefined : props.label + this.props.required ? '*' : ''
}, props)), props.label && this.state.jsComponentAttached && (0, _preact.h)(Label, {
for: props.id
}, props.label), props.trailingIcon ? (0, _preact.h)(_Icon.default, {
className: "mdc-text-field__icon"
}, props.trailingIcon) : null, props.textarea || outlined ? null : (0, _preact.h)("div", {
class: "mdc-line-ripple"
}), outlined ? (0, _preact.h)("div", {
class: "mdc-notched-outline"
}, (0, _preact.h)("svg", null, (0, _preact.h)("path", {
className: "mdc-notched-outline__path"
}))) : null, outlined ? (0, _preact.h)("div", {
className: "mdc-notched-outline__idle"
}) : null);
}
}, {
key: "buildClassName",
value: function buildClassName(props) {
var cn = (0, _get2.default)((0, _getPrototypeOf2.default)(TextFieldInput.prototype), "buildClassName", this).call(this, props);
if (this.MDComponent) {
cn += ' mdc-text-field--upgraded';
}
return cn;
}
}]);
return TextFieldInput;
}(_MaterialComponent4.default);
exports.TextFieldInput = TextFieldInput;
TextFieldInput.defaultProps = {
valid: true
};
var TextField =
/*#__PURE__*/
function (_Component) {
(0, _inherits2.default)(TextField, _Component);
function TextField() {
var _this5;
(0, _classCallCheck2.default)(this, TextField);
_this5 = (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(TextField).apply(this, arguments));
_this5.id = TextField.uid();
return _this5;
}
(0, _createClass2.default)(TextField, [{
key: "componentDidMount",
value: function componentDidMount() {
this.setState({
jsComponentAttached: true
});
}
}, {
key: "render",
value: function render(allprops) {
var _this6 = this;
var className = allprops.className,
outerStyle = allprops.outerStyle,
helperTextPersistent = allprops.helperTextPersistent,
helperTextValidationMsg = allprops.helperTextValidationMsg,
props = __rest(allprops, ["className", "outerStyle", "helperTextPersistent", "helperTextValidationMsg"]);
var showDiv = props.helperText;
if ((props.helperText || props.label) && !props.id) {
props.id = "tf-".concat(this.id);
} // Helper text
var helperTextProps = {
persistent: helperTextPersistent,
'validation-msg': helperTextValidationMsg
};
return showDiv ? (0, _preact.h)("div", {
className: className,
style: outerStyle
}, (0, _preact.h)(TextFieldInput, Object.assign({}, props, {
onInit: function onInit(MDComponent) {
_this6.MDComponent = MDComponent;
},
"aria-controls": props.helperText && "".concat(props.id, "-helper-text")
})), props.helperText && (0, _preact.h)(HelperText, Object.assign({
id: "".concat(props.id, "-helper-text")
}, helperTextProps), props.helperText)) : (0, _preact.h)(TextFieldInput, Object.assign({}, props, {
className: className,
outerStyle: outerStyle,
onInit: function onInit(MDComponent) {
_this6.MDComponent = MDComponent;
}
}));
}
}], [{
key: "uid",
value: function uid() {
return ++this.uidCounter;
}
}]);
return TextField;
}(_preact.Component);
exports.TextField = TextField;
TextField.uidCounter = 0;
var default_1 =
/*#__PURE__*/
function (_TextField) {
(0, _inherits2.default)(default_1, _TextField);
function default_1() {
(0, _classCallCheck2.default)(this, default_1);
return (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(default_1).apply(this, arguments));
}
return default_1;
}(TextField);
exports.default = default_1;
default_1.HelperText = HelperText;
//# sourceMappingURL=index.js.map