UNPKG

@react-awesome-query-builder/mui

Version:
119 lines (116 loc) 3.94 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 _config$settings = config.settings, defaultSliderWidth = _config$settings.defaultSliderWidth, renderSize = _config$settings.renderSize; var handleSliderChange = useCallback(function (_e, newValue) { setValue(newValue); }, [setValue]); var handleInputChange = useCallback(function (e) { var val = e.target.value; if (val === "" || val === null) val = undefined;else val = Number(val); setValue(val); }, [setValue]); var handleInputBlur = useCallback(function (e) { var val = e.target.value; if (val === "" || val === null) val = undefined;else val = Number(val); // TIP: Fix if typed value out of range in input if (val < min) { setValue(min); } else if (val > max) { setValue(max); } }, [setValue, min, 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 InputProps = useMemo(function () { return { readOnly: readonly }; }, [readonly]); var inputProps = useMemo(function () { return { min: min, max: max, step: step }; }, [min, max, step]); var InputCmp = /*#__PURE__*/React.createElement(TextField, _extends({ variant: "standard", type: "number", value: inputValue, placeholder: placeholder, InputProps: InputProps, inputProps: inputProps, disabled: readonly, onChange: handleInputChange, onBlur: handleInputBlur, size: renderSize }, 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: renderSize }, customSliderProps)); var stylesWrapper = { display: "inline-flex", alignItems: "center", flexWrap: "wrap" }; var stylesInputWrapper = { marginLeft: "5px" }; // todo: css var stylesSliderWrapper = { marginLeft: "5px", paddingLeft: "12px", marginBottom: muiMarks && "-24px", 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))); });