preact-material-components
Version:
preact wrapper for "Material Components for the web"
121 lines (101 loc) • 4.2 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _preact = require("preact");
var _MaterialComponent = _interopRequireDefault(require("../MaterialComponent"));
var _slider = require("@material/slider");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
/**
* @prop disabled = false
*/
class Slider extends _MaterialComponent.default {
constructor() {
super();
this.componentName = 'slider';
this._mdcProps = ['discrete'];
this._onChange = this._onChange.bind(this);
this._onInput = this._onInput.bind(this);
}
_onChange(e) {
if (this.props.onChange) {
this.props.onChange(e);
}
}
_onInput(e) {
if (this.props.onInput) {
this.props.onInput(e);
}
}
componentDidMount() {
this.MDComponent = new _slider.MDCSlider(this.base);
this.MDComponent.listen('MDCSlider:change', this._onChange);
this.MDComponent.listen('MDCSlider:input', this._onInput);
this.setValue(); // set initial value programatically because of error if min is greater than initial max
}
componentWillUnmount() {
this.MDComponent.unlisten('MDCSlider:change', this._onChange);
this.MDComponent.unlisten('MDCSlider:input', this._onInput);
this.MDComponent.destroy && this.MDComponent.destroy();
}
getValue() {
return this.MDComponent.value;
}
setValue(props = this.props) {
const _props$disabled = props.disabled,
disabled = _props$disabled === void 0 ? false : _props$disabled,
_props$min = props.min,
min = _props$min === void 0 ? 0 : _props$min,
_props$max = props.max,
max = _props$max === void 0 ? 100 : _props$max,
value = props.value,
step = props.step;
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;
}
this.MDComponent.value = value;
this.MDComponent.disabled = disabled;
this.MDComponent.step = step;
}
}
materialDom(allprops) {
const _allprops$tabindex = allprops.tabindex,
tabindex = _allprops$tabindex === void 0 ? 0 : _allprops$tabindex,
props = _objectWithoutProperties(allprops, ["tabindex"]);
this.setValue(allprops);
return (0, _preact.h)("div", _extends({
tabindex: tabindex,
role: "slider",
"aria-label": "Select Value"
}, props), (0, _preact.h)("div", {
"class": "mdc-slider__track-container"
}, (0, _preact.h)("div", {
"class": "mdc-slider__track"
})), (0, _preact.h)("div", {
"class": "mdc-slider__thumb-container"
}, props.discrete && (0, _preact.h)("div", {
"class": "mdc-slider__pin"
}, (0, _preact.h)("span", {
"class": "mdc-slider__pin-value-marker"
})), (0, _preact.h)("svg", {
"class": "mdc-slider__thumb",
width: "21",
height: "21"
}, (0, _preact.h)("circle", {
cx: "10.5",
cy: "10.5",
r: "7.875"
})), (0, _preact.h)("div", {
"class": "mdc-slider__focus-ring"
})));
}
}
exports.default = Slider;