@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
168 lines • 8.85 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Slider = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
/**
*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const react_1 = __importStar(require("react"));
const react_aria_1 = require("react-aria");
const react_stately_1 = require("react-stately");
const defaultImports_1 = require("../_common/defaultImports");
const helpers_1 = require("../helpers");
const tooltip_1 = require("../tooltip");
exports.Slider = (0, helpers_1.forwardRef)(function SliderComponent(_a, ref) {
var { as, inputProps = {}, className, style, minValue = 0, maxValue = 100, isDisabled = false, showSteps = false, showValues = false, step = 1, type = 'single', onChange, htmlAttributes } = _a, restProps = __rest(_a, ["as", "inputProps", "className", "style", "minValue", "maxValue", "isDisabled", "showSteps", "showValues", "step", "type", "onChange", "htmlAttributes"]);
const trackRef = react_1.default.useRef(null);
const castedValue = type === 'range'
? restProps.values
: restProps.value;
const castedOnChange = type === 'range'
? onChange
: onChange;
const state = (0, react_stately_1.useSliderState)({
isDisabled,
minValue,
maxValue,
step,
defaultValue: castedValue,
numberFormatter: new Intl.NumberFormat('en-US'),
onChange: castedOnChange, // eslint-disable-line @typescript-eslint/no-explicit-any
});
const { setThumbValue } = state;
const { groupProps, trackProps } = (0, react_aria_1.useSlider)({ 'aria-label': 'slider' }, state, trackRef);
const updatesSliderValue = (0, react_1.useCallback)((newValue) => {
if (type === 'single' && typeof newValue === 'number') {
setThumbValue(0, newValue);
}
else if (type === 'range' && Array.isArray(newValue)) {
setThumbValue(0, newValue[0]);
setThumbValue(1, newValue[1]);
}
}, [type, setThumbValue]);
(0, react_1.useEffect)(() => {
updatesSliderValue(castedValue);
// Needed to make the slider work with controlled values
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [castedValue]);
const Component = as || 'div';
return ((0, jsx_runtime_1.jsx)(Component, Object.assign({ className: (0, defaultImports_1.classNames)('ndl-slider', className), style: style, role: "presentation", ref: ref }, groupProps, htmlAttributes, { children: (0, jsx_runtime_1.jsxs)("div", Object.assign({}, trackProps, { ref: trackRef, className: (0, defaultImports_1.classNames)('ndl-track', {
'ndl-is-disabled': state.isDisabled,
}), children: [showSteps && ((0, jsx_runtime_1.jsx)(TrackMarks, { state: state, min: minValue, max: maxValue })), (0, jsx_runtime_1.jsx)(FilledTrack, { state: state }), (0, jsx_runtime_1.jsx)(Thumb, { index: 0, state: state, trackRef: trackRef, showValues: showValues, inputForwardedProps: inputProps }), type === 'range' && ((0, jsx_runtime_1.jsx)(Thumb, { index: 1, state: state, trackRef: trackRef, showValues: showValues, inputForwardedProps: inputProps }))] })) })));
});
function FilledTrack({ state }) {
let width;
let left;
if (state.values.length === 1) {
width = state.getValuePercent(state.values[0]) * 100;
left = 0;
}
else {
width =
state.getValuePercent(state.values[1]) * 100 -
state.getValuePercent(state.values[0]) * 100;
left = state.getValuePercent(state.values[0]) * 100;
}
return ((0, jsx_runtime_1.jsx)("div", { className: "ndl-filled-track", style: {
left: `${left}%`,
width: `${width}%`,
} }));
}
function TrackMarks({ state, max, min, }) {
const markNo = Math.round((max - min) / state.step);
return ((0, jsx_runtime_1.jsx)("div", { role: "presentation", className: "ndl-track-marks", children: Array.from({ length: markNo }).map((_, index) => {
const percent = (index / markNo) * 100;
let isFilled;
if (state.values.length === 1) {
isFilled = state.getValuePercent(state.values[0]) * 100 > percent;
}
else {
isFilled =
state.getValuePercent(state.values[0]) * 100 < percent &&
state.getValuePercent(state.values[1]) * 100 > percent;
}
return ((0, jsx_runtime_1.jsx)("div", { className: (0, defaultImports_1.classNames)('ndl-track-mark', {
'ndl-on-active-track': isFilled,
}), style: {
left: `${(index / markNo) * 100}%`,
} }, `track-mark-${index}`));
}) }));
}
function Thumb(props) {
const { state, trackRef, index, inputForwardedProps } = props;
const inputRef = react_1.default.useRef(null);
const { thumbProps, inputProps, isDragging } = (0, react_aria_1.useSliderThumb)({
index,
trackRef,
inputRef,
name: 'slider-thumb',
}, state);
const { focusProps, isFocusVisible } = (0, react_aria_1.useFocusRing)();
/**
* Solve issue with:
* Warning: Received NaN for the `value` attribute. If this is expected, cast the value to a string.
* Receiving NaN for other attributes also, like `max`. Waiting for a fix in react-aria.
* @see https://github.com/adobe/react-spectrum/issues/5859
* @see https://github.com/adobe/react-spectrum/pull/5861
*/
const mergedProps = (0, react_aria_1.mergeProps)(inputProps, focusProps,
// Omitting aria-valuetext double announcements, unless explicitly provided
{ 'aria-valuetext': '' }, inputForwardedProps);
const value = mergedProps.value || mergedProps.value === 0 ? mergedProps.value : '';
return ((0, jsx_runtime_1.jsxs)(tooltip_1.Tooltip, { isDisabled: props.showValues !== true, isOpen: isDragging || undefined, placement: "top", type: "simple", children: [(0, jsx_runtime_1.jsx)(tooltip_1.Tooltip.Trigger, { hasButtonWrapper: true, children: (0, jsx_runtime_1.jsx)("div", Object.assign({}, thumbProps, { className: (0, defaultImports_1.classNames)('ndl-thumb', {
'ndl-is-dragging': isDragging,
'ndl-focus': isFocusVisible,
}), children: (0, jsx_runtime_1.jsx)(react_aria_1.VisuallyHidden, { children: (0, jsx_runtime_1.jsx)("input", Object.assign({ ref: inputRef }, mergedProps)) }) })) }), (0, jsx_runtime_1.jsx)(tooltip_1.Tooltip.Content, { children: value })] }));
}
//# sourceMappingURL=Slider.js.map