UNPKG

@react-awesome-query-builder/mui

Version:
107 lines (105 loc) 3.53 kB
import _extends from "@babel/runtime/helpers/extends"; import _typeof from "@babel/runtime/helpers/typeof"; import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; var _excluded = ["width"]; import React, { useCallback, useMemo } from "react"; import Slider from "@mui/material/Slider"; import TextField from "@mui/material/TextField"; import FormControl from "@mui/material/FormControl"; export default (function (props) { var config = props.config, placeholder = props.placeholder, customProps = props.customProps, value = props.value, setValue = props.setValue, min = props.min, max = props.max, step = props.step, marks = props.marks, readonly = props.readonly; var defaultSliderWidth = config.settings.defaultSliderWidth; var handleSliderChange = useCallback(function (_e, newValue) { setValue(newValue); }, []); var handleInputChange = function handleInputChange(e) { var val = e.target.value; if (val === "" || val === null) val = undefined;else val = Number(val); setValue(val); }; var handleInputBlur = function handleInputBlur() { // TIP: Fix if typed value out of range in input if (value < min) { setValue(min); } else if (value > max) { setValue(max); } }; var _ref = customProps || {}, width = _ref.width, rest = _objectWithoutProperties(_ref, _excluded); var customInputProps = rest.input || {}; var customSliderProps = rest.slider || rest; // TIP: Can't pass undefined to MUI, cause it means uncontrolled component use. // For empty value input needs "", slider needs null or 0 var inputValue = typeof value === "number" ? value : ""; var sliderValue = typeof value === "number" ? value : null; // marks example: { 0: "0%", 100: React.createElement('strong', null, "100%") } var muiMarks = useMemo(function () { return marks ? Object.keys(marks).map(function (v) { return { value: Number(v), label: _typeof(marks[v]) === "object" || typeof marks[v] === "undefined" ? marks[v] : /*#__PURE__*/React.createElement("p", null, marks[v]) }; }) : false; }, [marks]); var InputCmp = /*#__PURE__*/React.createElement(TextField, _extends({ variant: "standard", type: "number", value: inputValue, placeholder: placeholder, InputProps: { readOnly: readonly }, inputProps: { min: min, max: max, step: step }, disabled: readonly, onChange: handleInputChange, onBlur: handleInputBlur, size: "small" }, customInputProps)); var SliderCmp = /*#__PURE__*/React.createElement(Slider, _extends({ value: sliderValue, onChange: handleSliderChange, disabled: readonly, min: min, max: max, step: step, marks: muiMarks, valueLabelDisplay: "auto", size: "small" }, customSliderProps)); var stylesWrapper = { display: "inline-flex", alignItems: "center", flexWrap: "wrap" }; var stylesInputWrapper = { marginLeft: "5px" }; var stylesSliderWrapper = { marginLeft: "5px", paddingLeft: "12px", marginBottom: muiMarks && "-16px", width: width || defaultSliderWidth }; return /*#__PURE__*/React.createElement(FormControl, null, /*#__PURE__*/React.createElement("div", { style: stylesWrapper }, /*#__PURE__*/React.createElement("div", { style: stylesInputWrapper }, InputCmp), /*#__PURE__*/React.createElement("div", { style: stylesSliderWrapper }, SliderCmp))); });