Debian Codegraph @

node/@patternfly/react-core/dist/js/components/Slider/Slider.js

raw
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.Slider = void 0;
4 const tslib_1 = require("tslib");
5 const jsx_runtime_1 = require("react/jsx-runtime");
6 const react_1 = require("react");
7 const slider_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/Slider/slider"));
8 const react_styles_1 = require("@patternfly/react-styles");
9 const SliderStep_1 = require("./SliderStep");
10 const InputGroup_1 = require("../InputGroup");
11 const TextInput_1 = require("../TextInput");
12 const Tooltip_1 = require("../Tooltip");
13 const c_slider_value_1 = tslib_1.__importDefault(require('@patternfly/react-tokens/dist/js/c_slider_value'));
14 const c_slider__value_c_form_control_width_chars_1 = tslib_1.__importDefault(require('@patternfly/react-tokens/dist/js/c_slider__value_c_form_control_width_chars'));
15 const util_1 = require("../../helpers/util");
16 const getPercentage = (current, max) => (100 * current) / max;
17 const Slider = (_a) => {
18 var { className, value = 0, customSteps, areCustomStepsContinuous = false, isDisabled = false, isInputVisible = false, inputValue = 0, inputLabel, inputAriaLabel = 'Slider value input', thumbAriaLabel = 'Value', hasTooltipOverThumb = false, inputPosition = 'end', onChange, leftActions, startActions, rightActions, endActions, step = 1, min = 0, max = 100, showTicks = false, showBoundaries = true, 'aria-describedby': ariaDescribedby, 'aria-labelledby': ariaLabelledby } = _a, props = tslib_1.__rest(_a, ["className", "value", "customSteps", "areCustomStepsContinuous", "isDisabled", "isInputVisible", "inputValue", "inputLabel", "inputAriaLabel", "thumbAriaLabel", "hasTooltipOverThumb", "inputPosition", "onChange", "leftActions", "startActions", "rightActions", "endActions", "step", "min", "max", "showTicks", "showBoundaries", 'aria-describedby', 'aria-labelledby']);
19 const sliderRailRef = (0, react_1.useRef)(undefined);
20 const thumbRef = (0, react_1.useRef)(undefined);
21 const [localValue, setValue] = (0, react_1.useState)(value);
22 const [localInputValue, setLocalInputValue] = (0, react_1.useState)(inputValue);
23 let isRTL;
24 (0, react_1.useEffect)(() => {
25 isRTL = (0, util_1.getLanguageDirection)(sliderRailRef.current) === 'rtl';
26 });
27 (0, react_1.useEffect)(() => {
28 setValue(value);
29 }, [value]);
30 (0, react_1.useEffect)(() => {
31 setLocalInputValue(inputValue);
32 }, [inputValue]);
33 let diff = 0;
34 let snapValue;
35 // calculate style value percentage
36 const stylePercent = ((localValue - min) * 100) / (max - min);
37 const style = { [c_slider_value_1.default.name]: `${stylePercent}%` };
38 const widthChars = (0, react_1.useMemo)(() => localInputValue.toString().length, [localInputValue]);
39 const inputStyle = { [c_slider__value_c_form_control_width_chars_1.default.name]: widthChars };
40 const onChangeHandler = (_event, value) => {
41 setLocalInputValue(Number(value));
42 };
43 const handleKeyPressOnInput = (event) => {
44 if (event.key === 'Enter') {
45 event.preventDefault();
46 if (onChange) {
47 onChange(event, localValue, localInputValue, setLocalInputValue);
48 }
49 }
50 };
51 const onInputFocus = (e) => {
52 e.stopPropagation();
53 };
54 const onThumbClick = () => {
55 thumbRef.current.focus();
56 };
57 const onBlur = (event) => {
58 if (onChange) {
59 onChange(event, localValue, localInputValue, setLocalInputValue);
60 }
61 };
62 const findAriaTextValue = () => {
63 if (!areCustomStepsContinuous && customSteps) {
64 const matchingStep = customSteps.find((stepObj) => stepObj.value === localValue);
65 if (matchingStep) {
66 return matchingStep.label;
67 }
68 }
69 // For continuous steps default to showing 2 decimals in tooltip
70 // Consider making it configurable via a property
71 return Number(Number(localValue).toFixed(2)).toString();
72 };
73 const handleThumbDragEnd = () => {
74 document.removeEventListener('mousemove', callbackThumbMove);
75 document.removeEventListener('mouseup', callbackThumbUp);
76 document.removeEventListener('touchmove', callbackThumbMove);
77 document.removeEventListener('touchend', callbackThumbUp);
78 document.removeEventListener('touchcancel', callbackThumbUp);
79 };
80 const handleMouseDown = (e) => {
81 e.stopPropagation();
82 e.preventDefault();
83 if (isRTL) {
84 diff = thumbRef.current.getBoundingClientRect().right - e.clientX;
85 }
86 else {
87 diff = e.clientX - thumbRef.current.getBoundingClientRect().left;
88 }
89 document.addEventListener('mousemove', callbackThumbMove);
90 document.addEventListener('mouseup', callbackThumbUp);
91 };
92 const handleTouchStart = (e) => {
93 e.stopPropagation();
94 if (isRTL) {
95 diff = thumbRef.current.getBoundingClientRect().right - e.touches[0].clientX;
96 }
97 else {
98 diff = e.touches[0].clientX - thumbRef.current.getBoundingClientRect().left;
99 }
100 document.addEventListener('touchmove', callbackThumbMove, { passive: false });
101 document.addEventListener('touchend', callbackThumbUp);
102 document.addEventListener('touchcancel', callbackThumbUp);
103 };
104 const onSliderRailClick = (e) => {
105 handleThumbMove(e);
106 if (snapValue && !areCustomStepsContinuous) {
107 thumbRef.current.style.setProperty(c_slider_value_1.default.name, `${snapValue}%`);
108 setValue(snapValue);
109 if (onChange) {
110 onChange(e, snapValue);
111 }
112 }
113 };
114 const handleThumbMove = (e) => {
115 if (e.type === 'touchmove') {
116 e.preventDefault();
117 e.stopImmediatePropagation();
118 }
119 const clientPosition = e.touches && e.touches.length ? e.touches[0].clientX : e.clientX;
120 let newPosition;
121 if (isRTL) {
122 newPosition = sliderRailRef.current.getBoundingClientRect().right - clientPosition - diff;
123 }
124 else {
125 newPosition = clientPosition - diff - sliderRailRef.current.getBoundingClientRect().left;
126 }
127 const end = sliderRailRef.current.offsetWidth - thumbRef.current.offsetWidth;
128 const start = 0;
129 if (newPosition < start) {
130 newPosition = 0;
131 }
132 if (newPosition > end) {
133 newPosition = end;
134 }
135 const newPercentage = getPercentage(newPosition, end);
136 thumbRef.current.style.setProperty(c_slider_value_1.default.name, `${newPercentage}%`);
137 // convert percentage to value
138 const newValue = Math.round(((newPercentage * (max - min)) / 100 + min) * 100) / 100;
139 setValue(newValue);
140 if (!customSteps) {
141 // snap to new value if not custom steps
142 snapValue = Math.round((Math.round((newValue - min) / step) * step + min) * 100) / 100;
143 thumbRef.current.style.setProperty(c_slider_value_1.default.name, `${snapValue}%`);
144 setValue(snapValue);
145 }
146 /* If custom steps are discrete, snap to closest step value */
147 if (!areCustomStepsContinuous && customSteps) {
148 let percentage = newPercentage;
149 if (customSteps[customSteps.length - 1].value !== 100) {
150 percentage = (newPercentage * (max - min)) / 100 + min;
151 }
152 const stepIndex = customSteps.findIndex((stepObj) => stepObj.value >= percentage);
153 if (customSteps[stepIndex].value === percentage) {
154 snapValue = customSteps[stepIndex].value;
155 }
156 else {
157 const midpoint = (customSteps[stepIndex].value + customSteps[stepIndex - 1].value) / 2;
158 if (midpoint > percentage) {
159 snapValue = customSteps[stepIndex - 1].value;
160 }
161 else {
162 snapValue = customSteps[stepIndex].value;
163 }
164 }
165 setValue(snapValue);
166 }
167 // Call onchange callback
168 if (onChange) {
169 if (snapValue !== undefined) {
170 onChange(e, snapValue);
171 }
172 else {
173 onChange(e, newValue);
174 }
175 }
176 };
177 const callbackThumbMove = (0, react_1.useCallback)(handleThumbMove, [min, max, customSteps, onChange]);
178 const callbackThumbUp = (0, react_1.useCallback)(handleThumbDragEnd, [min, max, customSteps, onChange]);
179 const handleThumbKeys = (e) => {
180 const key = e.key;
181 if (key !== 'ArrowLeft' && key !== 'ArrowRight') {
182 return;
183 }
184 e.preventDefault();
185 let newValue = localValue;
186 if (!areCustomStepsContinuous && customSteps) {
187 const stepIndex = customSteps.findIndex((stepObj) => stepObj.value === localValue);
188 if (key === 'ArrowRight') {
189 if (isRTL) {
190 if (stepIndex - 1 >= 0) {
191 newValue = customSteps[stepIndex - 1].value;
192 }
193 }
194 else {
195 if (stepIndex + 1 < customSteps.length) {
196 {
197 newValue = customSteps[stepIndex + 1].value;
198 }
199 }
200 }
201 }
202 else if (key === 'ArrowLeft') {
203 if (isRTL) {
204 if (stepIndex + 1 < customSteps.length) {
205 {
206 newValue = customSteps[stepIndex + 1].value;
207 }
208 }
209 }
210 else {
211 if (stepIndex - 1 >= 0) {
212 newValue = customSteps[stepIndex - 1].value;
213 }
214 }
215 }
216 }
217 else {
218 if (key === 'ArrowRight') {
219 if (isRTL) {
220 newValue = localValue - step >= min ? localValue - step : min;
221 }
222 else {
223 newValue = localValue + step <= max ? localValue + step : max;
224 }
225 }
226 else if (key === 'ArrowLeft') {
227 if (isRTL) {
228 newValue = localValue + step <= max ? localValue + step : max;
229 }
230 else {
231 newValue = localValue - step >= min ? localValue - step : min;
232 }
233 }
234 }
235 if (newValue !== localValue) {
236 thumbRef.current.style.setProperty(c_slider_value_1.default.name, `${newValue}%`);
237 setValue(newValue);
238 if (onChange) {
239 onChange(e, newValue);
240 }
241 }
242 };
243 const displayInput = () => {
244 const textInput = ((0, jsx_runtime_1.jsx)(TextInput_1.TextInput, { isDisabled: isDisabled, type: "number", value: localInputValue, "aria-label": inputAriaLabel, onKeyDown: handleKeyPressOnInput, onChange: onChangeHandler, onClick: onInputFocus, onFocus: onInputFocus, onBlur: onBlur }));
245 if (inputLabel) {
246 return ((0, jsx_runtime_1.jsxs)(InputGroup_1.InputGroup, { children: [(0, jsx_runtime_1.jsx)(InputGroup_1.InputGroupItem, { isFill: true, children: textInput }), (0, jsx_runtime_1.jsx)(InputGroup_1.InputGroupText, { isDisabled: isDisabled, children: inputLabel })] }));
247 }
248 else {
249 return textInput;
250 }
251 };
252 const getStepValue = (val, min, max) => ((val - min) * 100) / (max - min);
253 const buildSteps = () => {
254 const builtSteps = [];
255 for (let i = min; i <= max; i = i + step) {
256 const stepValue = getStepValue(i, min, max);
257 // If boundaries but not ticks just generate the needed steps
258 // so that we don't pollute them DOM with empty divs
259 if (!showTicks && showBoundaries && i !== min && i !== max) {
260 continue;
261 }
262 builtSteps.push((0, jsx_runtime_1.jsx)(SliderStep_1.SliderStep, { value: stepValue, label: i.toString(), isTickHidden: !showTicks, isLabelHidden: (i === min || i === max) && showBoundaries ? false : true, isActive: i <= localValue }, i));
263 }
264 return builtSteps;
265 };
266 const thumbComponent = ((0, jsx_runtime_1.jsx)("div", { className: (0, react_styles_1.css)(slider_1.default.sliderThumb), ref: thumbRef, tabIndex: isDisabled ? -1 : 0, role: "slider", "aria-valuemin": customSteps ? customSteps[0].value : min, "aria-valuemax": customSteps ? customSteps[customSteps.length - 1].value : max, "aria-valuenow": localValue, "aria-valuetext": findAriaTextValue(), "aria-label": thumbAriaLabel, "aria-disabled": isDisabled, "aria-describedby": ariaDescribedby, "aria-labelledby": ariaLabelledby, onMouseDown: !isDisabled ? handleMouseDown : null, onTouchStart: !isDisabled ? handleTouchStart : null, onKeyDown: !isDisabled ? handleThumbKeys : null, onClick: !isDisabled ? onThumbClick : null }));
267 return ((0, jsx_runtime_1.jsxs)("div", Object.assign({ className: (0, react_styles_1.css)(slider_1.default.slider, className, isDisabled && slider_1.default.modifiers.disabled), style: Object.assign(Object.assign({}, style), inputStyle) }, props, { children: [(leftActions || startActions) && (0, jsx_runtime_1.jsx)("div", { className: (0, react_styles_1.css)(slider_1.default.sliderActions), children: leftActions || startActions }), (0, jsx_runtime_1.jsxs)("div", { className: (0, react_styles_1.css)(slider_1.default.sliderMain), children: [(0, jsx_runtime_1.jsx)("div", { className: (0, react_styles_1.css)(slider_1.default.sliderRail), ref: sliderRailRef, onClick: !isDisabled ? onSliderRailClick : null, children: (0, jsx_runtime_1.jsx)("div", { className: (0, react_styles_1.css)(slider_1.default.sliderRailTrack) }) }), customSteps && ((0, jsx_runtime_1.jsx)("div", { className: (0, react_styles_1.css)(slider_1.default.sliderSteps), "aria-hidden": "true", children: customSteps.map((stepObj) => {
268 const minValue = customSteps[0].value;
269 const maxValue = customSteps[customSteps.length - 1].value;
270 const stepValue = getStepValue(stepObj.value, minValue, maxValue);
271 return ((0, jsx_runtime_1.jsx)(SliderStep_1.SliderStep, { value: stepValue, label: stepObj.label, isLabelHidden: stepObj.isLabelHidden, isActive: stepObj.value <= localValue }, stepObj.value));
272 }) })), !customSteps && (showTicks || showBoundaries) && ((0, jsx_runtime_1.jsx)("div", { className: (0, react_styles_1.css)(slider_1.default.sliderSteps), "aria-hidden": "true", children: buildSteps() })), hasTooltipOverThumb ? ((0, jsx_runtime_1.jsx)(Tooltip_1.Tooltip, { className: (0, react_styles_1.css)('pf-v6-m-tabular-nums'), triggerRef: thumbRef, entryDelay: 0, content: findAriaTextValue(), children: thumbComponent })) : (thumbComponent), isInputVisible && inputPosition === 'aboveThumb' && ((0, jsx_runtime_1.jsx)("div", { className: (0, react_styles_1.css)(slider_1.default.sliderValue, slider_1.default.modifiers.floating), children: displayInput() }))] }), isInputVisible && (inputPosition === 'right' || inputPosition === 'end') && ((0, jsx_runtime_1.jsx)("div", { className: (0, react_styles_1.css)(slider_1.default.sliderValue), children: displayInput() })), (rightActions || endActions) && (0, jsx_runtime_1.jsx)("div", { className: (0, react_styles_1.css)(slider_1.default.sliderActions), children: rightActions || endActions })] })));
273 };
274 exports.Slider = Slider;
275 exports.Slider.displayName = 'Slider';
276 //# sourceMappingURL=Slider.js.map