preact-material-components
Version:
preact wrapper for "Material Components for the web"
101 lines • 4.14 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 { MDCSlider } from '@material/slider';
import { bind } from 'bind-decorator';
import { h } from 'preact';
import MaterialComponent from '../Base/MaterialComponent';
export class Slider extends MaterialComponent {
constructor() {
super(...arguments);
this.componentName = 'slider';
this.mdcProps = ['discrete'];
}
componentDidMount() {
super.componentDidMount();
if (this.control) {
this.MDComponent = new MDCSlider(this.control);
this.MDComponent.listen('MDCSlider:change', this.onChange);
this.MDComponent.listen('MDCSlider:input', this.onInput);
}
this.setValue(this.props.value); // set initial value programatically because of error if min is greater than initial max
}
componentWillUnmount() {
super.componentWillUnmount();
if (this.MDComponent) {
this.MDComponent.unlisten('MDCSlider:change', this.onChange);
this.MDComponent.unlisten('MDCSlider:input', this.onInput);
this.MDComponent.destroy();
}
}
getValue() {
if (this.MDComponent) {
return this.MDComponent.value;
}
}
setValue(value) {
const { disabled = false, min = 0, max = 100, step } = this.props;
if (this.MDComponent) {
if (min > this.MDComponent.max) {
this.MDComponent.max = max;
this.MDComponent.min = min;
}
else {
this.MDComponent.min = min;
this.MDComponent.max = max;
}
if (value) {
this.MDComponent.value = value;
}
this.MDComponent.disabled = disabled;
if (step) {
this.MDComponent.step = step;
}
}
}
onChange(e) {
if (this.props.onChange) {
this.props.onChange(e);
}
}
onInput(e) {
if (this.props.onInput) {
this.props.onInput(e);
}
}
materialDom(allprops) {
const { tabindex: tabIndex = 0 } = allprops, props = __rest(allprops, ["tabindex"]);
this.setValue(allprops);
return (h("div", Object.assign({ tabIndex: tabIndex, role: "slider", "aria-label": "Select Value", ref: this.setControlRef }, props),
h("div", { class: "mdc-slider__track-container" },
h("div", { class: "mdc-slider__track" })),
h("div", { class: "mdc-slider__thumb-container" },
props.discrete && (h("div", { class: "mdc-slider__pin" },
h("span", { class: "mdc-slider__pin-value-marker" }))),
h("svg", { class: "mdc-slider__thumb", width: "21", height: "21" },
h("circle", { cx: "10.5", cy: "10.5", r: "7.875" })),
h("div", { class: "mdc-slider__focus-ring" }))));
}
}
__decorate([
bind
], Slider.prototype, "onChange", null);
__decorate([
bind
], Slider.prototype, "onInput", null);
export default Slider;
//# sourceMappingURL=index.js.map