@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
124 lines (123 loc) • 3.46 kB
JavaScript
"use client";
import React, { useCallback, useContext, useMemo, useRef } from 'react';
import FieldBlock from "../../FieldBlock/index.js";
import { useFieldProps } from "../../hooks/index.js";
import { getFormattedNumber } from "../../../../components/slider/SliderHelpers.js";
import Slider from "../../../../components/Slider.js";
import { pickSpacingProps } from "../../../../components/flex/utils.js";
import DataContext from "../../DataContext/Context.js";
import useDataValue from "../../hooks/useDataValue.js";
import { useTranslation as useSharedTranslation } from "../../../../shared/index.js";
function SliderComponent(props) {
const dataContextRef = useRef();
dataContextRef.current = useContext(DataContext);
const {
Slider: {
addTitle: addTitleLabel,
subtractTitle: subtractTitleLabel
}
} = useSharedTranslation();
const {
getSourceValue
} = useDataValue();
const getValues = useCallback(source => {
if (Array.isArray(source)) {
return source.map(s => getSourceValue(s) || 0);
}
return getSourceValue(source) || 0;
}, [getSourceValue]);
const preparedProps = {
...props,
step: getSourceValue(props.step),
min: getSourceValue(props.min),
max: getSourceValue(props.max),
provideAdditionalArgs: v => ({
value: v
})
};
const {
id,
path,
itemPath,
value,
step = 1,
min = 0,
max = 100,
width = 'stretch',
hasError,
disabled,
vertical,
reverse,
hideButtons,
multiThumbBehavior = 'swap',
thumbTitle,
subtractTitle = subtractTitleLabel,
addTitle = addTitleLabel,
numberFormat,
tooltip,
alwaysShowTooltip,
extensions,
handleChange,
handleFocus,
handleBlur,
setDisplayValue
} = useFieldProps(preparedProps, {
omitMultiplePathWarning: true
});
const sliderValue = Array.isArray(props.paths) ? getValues(props.paths) : value;
useMemo(() => {
if ((path || itemPath) && numberFormat) {
var _ref;
const {
number
} = getFormattedNumber((_ref = sliderValue) !== null && _ref !== void 0 ? _ref : 0, numberFormat);
setDisplayValue(number);
}
}, [itemPath, numberFormat, path, setDisplayValue, sliderValue]);
const handleLocalChange = useCallback(({
value
}) => {
if (Array.isArray(props.paths) && Array.isArray(value)) {
value.forEach((value, i) => {
dataContextRef.current.updateDataValue(props.paths[i], value);
});
}
if (!Array.isArray(props.paths)) {
handleChange === null || handleChange === void 0 || handleChange(value);
}
}, [handleChange, props.paths]);
const fieldBlockProps = {
id: id,
forId: `${id}-slider-thumb-0`,
width,
...pickSpacingProps(props)
};
const sliderProps = {
id: `${id}-slider`,
value: sliderValue,
step,
min,
max,
disabled,
status: hasError ? 'error' : undefined,
on_change: handleLocalChange,
on_drag_start: handleFocus,
on_drag_end: handleBlur,
vertical,
reverse,
hideButtons,
multiThumbBehavior,
thumbTitle,
subtractTitle,
addTitle,
numberFormat,
tooltip,
alwaysShowTooltip,
extensions,
stretch: true
};
return React.createElement(FieldBlock, fieldBlockProps, React.createElement(Slider, sliderProps));
}
export default SliderComponent;
SliderComponent._supportsSpacingProps = true;
//# sourceMappingURL=Slider.js.map