preact-material-components
Version:
preact wrapper for "Material Components for the web"
213 lines (172 loc) • 6.97 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.Select = exports.SelectOption = 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 _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
var _select = require("@material/select/");
var _bindDecorator = require("bind-decorator");
var _preact = require("preact");
var _MaterialComponent3 = _interopRequireDefault(require("../Base/MaterialComponent"));
var __decorate = void 0 && (void 0).__decorate || function (decorators, target, key, desc) {
var c = arguments.length,
r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc,
d;
if ((typeof Reflect === "undefined" ? "undefined" : (0, _typeof2.default)(Reflect)) === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);else for (var i = decorators.length - 1; i >= 0; i--) {
if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
}
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
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 SelectOption =
/*#__PURE__*/
function (_MaterialComponent) {
(0, _inherits2.default)(SelectOption, _MaterialComponent);
function SelectOption() {
var _this;
(0, _classCallCheck2.default)(this, SelectOption);
_this = (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(SelectOption).apply(this, arguments));
_this.componentName = 'select-item';
_this.mdcProps = [];
return _this;
}
(0, _createClass2.default)(SelectOption, [{
key: "materialDom",
value: function materialDom(props) {
return (0, _preact.h)("option", Object.assign({}, props), props.children);
}
}]);
return SelectOption;
}(_MaterialComponent3.default);
exports.SelectOption = SelectOption;
var Select =
/*#__PURE__*/
function (_MaterialComponent2) {
(0, _inherits2.default)(Select, _MaterialComponent2);
function Select() {
var _this2;
(0, _classCallCheck2.default)(this, Select);
_this2 = (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(Select).apply(this, arguments));
_this2.componentName = 'select';
_this2.mdcProps = ['disabled', 'box', 'outlined'];
return _this2;
}
(0, _createClass2.default)(Select, [{
key: "componentDidMount",
value: function componentDidMount() {
(0, _get2.default)((0, _getPrototypeOf2.default)(Select.prototype), "componentDidMount", this).call(this);
if (this.control) {
this.MDComponent = new _select.MDCSelect(this.control);
this.MDComponent.listen('MDCSelect:change', this.onChanged);
}
this.updateSelection();
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
(0, _get2.default)((0, _getPrototypeOf2.default)(Select.prototype), "componentWillUnmount", this).call(this);
if (this.MDComponent) {
this.MDComponent.unlisten('MDCSelect:change', this.onChanged);
this.MDComponent.destroy();
}
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate() {
this.updateSelection();
}
}, {
key: "onChanged",
value: function onChanged(e) {
if (this.MDComponent) {
e = e || {};
e.selectedIndex = e.selectedIndex || this.MDComponent.selectedIndex;
if (this.props.onChange) {
this.props.onChange(e);
}
}
}
}, {
key: "updateSelection",
value: function updateSelection() {
if (this.MDComponent) {
if (this.props.selectedIndex) {
this.MDComponent.selectedIndex = typeof this.props.selectedIndex === 'number' ? this.props.selectedIndex : 0;
}
var selectedIndex = this.MDComponent.selectedIndex;
if (selectedIndex === 0) {
if (this.labelRef) {
this.labelRef.classList.remove('mdc-floating-label--float-above');
}
} else {
if (this.labelRef) {
this.labelRef.classList.add('mdc-floating-label--float-above');
}
}
}
}
}, {
key: "materialDom",
value: function materialDom(allprops) {
var _this3 = this;
var outlined = allprops.outlined,
n = allprops.name,
props = __rest(allprops, ["outlined", "name"]); // noinspection RequiredAttributes
return (0, _preact.h)("div", Object.assign({
ref: this.setControlRef
}, props), (0, _preact.h)("select", {
class: "mdc-select__native-control",
name: n
}, props.hintText && (0, _preact.h)("option", {
value: "",
disabled: true,
selected: true
}), props.children), props.hintText && (0, _preact.h)("div", {
class: "mdc-floating-label",
ref: function ref(_ref) {
_this3.labelRef = _ref;
}
}, props.hintText), !outlined && (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", {
class: "mdc-notched-outline__path"
}))), outlined && (0, _preact.h)("div", {
class: "mdc-notched-outline__idle"
}));
}
}]);
return Select;
}(_MaterialComponent3.default);
exports.Select = Select;
__decorate([_bindDecorator.bind], Select.prototype, "onChanged", null);
var default_1 =
/*#__PURE__*/
function (_Select) {
(0, _inherits2.default)(default_1, _Select);
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;
}(Select);
exports.default = default_1;
default_1.Item = SelectOption;
//# sourceMappingURL=index.js.map