preact-material-components
Version:
preact wrapper for "Material Components for the web"
109 lines • 4.42 kB
JavaScript
var __decorate = (this && this.__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 === "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 = (this && this.__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;
};
import { MDCSelect } from '@material/select/';
import { bind } from 'bind-decorator';
import { h } from 'preact';
import MaterialComponent from '../Base/MaterialComponent';
export class SelectOption extends MaterialComponent {
constructor() {
super(...arguments);
this.componentName = 'select-item';
this.mdcProps = [];
}
materialDom(props) {
return h("option", Object.assign({}, props), props.children);
}
}
export class Select extends MaterialComponent {
constructor() {
super(...arguments);
this.componentName = 'select';
this.mdcProps = ['disabled', 'box', 'outlined'];
}
componentDidMount() {
super.componentDidMount();
if (this.control) {
this.MDComponent = new MDCSelect(this.control);
this.MDComponent.listen('MDCSelect:change', this.onChanged);
}
this.updateSelection();
}
componentWillUnmount() {
super.componentWillUnmount();
if (this.MDComponent) {
this.MDComponent.unlisten('MDCSelect:change', this.onChanged);
this.MDComponent.destroy();
}
}
componentDidUpdate() {
this.updateSelection();
}
onChanged(e) {
if (this.MDComponent) {
e = e || {};
e.selectedIndex = e.selectedIndex || this.MDComponent.selectedIndex;
if (this.props.onChange) {
this.props.onChange(e);
}
}
}
updateSelection() {
if (this.MDComponent) {
if (this.props.selectedIndex) {
this.MDComponent.selectedIndex =
typeof this.props.selectedIndex === 'number'
? this.props.selectedIndex
: 0;
}
const 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');
}
}
}
}
materialDom(allprops) {
const { outlined, name: n } = allprops, props = __rest(allprops, ["outlined", "name"]);
// noinspection RequiredAttributes
return (h("div", Object.assign({ ref: this.setControlRef }, props),
h("select", { class: "mdc-select__native-control", name: n },
props.hintText && h("option", { value: "", disabled: true, selected: true }),
props.children),
props.hintText && (h("div", { class: "mdc-floating-label", ref: ref => {
this.labelRef = ref;
} }, props.hintText)),
!outlined && h("div", { class: "mdc-line-ripple" }),
outlined && (h("div", { class: "mdc-notched-outline" },
h("svg", null,
h("path", { class: "mdc-notched-outline__path" })))),
outlined && h("div", { class: "mdc-notched-outline__idle" })));
}
}
__decorate([
bind
], Select.prototype, "onChanged", null);
export default class default_1 extends Select {
}
default_1.Item = SelectOption;
//# sourceMappingURL=index.js.map