UNPKG

@neo4j-ndl/react

Version:

React implementation of Neo4j Design System

142 lines 7.23 kB
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; }; import { jsx as _jsx, jsxs as _jsxs } from "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/>. */ import React, { useCallback, useEffect } from 'react'; import { mergeProps, useFocusRing, useSlider, useSliderThumb, VisuallyHidden, } from 'react-aria'; import { useSliderState } from 'react-stately'; import { classNames } from '../_common/defaultImports'; import { forwardRef } from '../helpers'; import { Tooltip } from '../tooltip'; export const Slider = 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.useRef(null); const castedValue = type === 'range' ? restProps.values : restProps.value; const castedOnChange = type === 'range' ? onChange : onChange; const state = 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 } = useSlider({ 'aria-label': 'slider' }, state, trackRef); const updatesSliderValue = 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]); 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 (_jsx(Component, Object.assign({ className: classNames('ndl-slider', className), style: style, role: "presentation", ref: ref }, groupProps, htmlAttributes, { children: _jsxs("div", Object.assign({}, trackProps, { ref: trackRef, className: classNames('ndl-track', { 'ndl-is-disabled': state.isDisabled, }), children: [showSteps && (_jsx(TrackMarks, { state: state, min: minValue, max: maxValue })), _jsx(FilledTrack, { state: state }), _jsx(Thumb, { index: 0, state: state, trackRef: trackRef, showValues: showValues, inputForwardedProps: inputProps }), type === 'range' && (_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 (_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 (_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 (_jsx("div", { className: 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.useRef(null); const { thumbProps, inputProps, isDragging } = useSliderThumb({ index, trackRef, inputRef, name: 'slider-thumb', }, state); const { focusProps, isFocusVisible } = 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 = mergeProps(inputProps, focusProps, // Omitting aria-valuetext double announcements, unless explicitly provided { 'aria-valuetext': '' }, inputForwardedProps); const value = mergedProps.value || mergedProps.value === 0 ? mergedProps.value : ''; return (_jsxs(Tooltip, { isDisabled: props.showValues !== true, isOpen: isDragging || undefined, placement: "top", type: "simple", children: [_jsx(Tooltip.Trigger, { hasButtonWrapper: true, children: _jsx("div", Object.assign({}, thumbProps, { className: classNames('ndl-thumb', { 'ndl-is-dragging': isDragging, 'ndl-focus': isFocusVisible, }), children: _jsx(VisuallyHidden, { children: _jsx("input", Object.assign({ ref: inputRef }, mergedProps)) }) })) }), _jsx(Tooltip.Content, { children: value })] })); } //# sourceMappingURL=Slider.js.map