UNPKG

@neo4j-ndl/react

Version:

React implementation of Neo4j Design System

76 lines 3.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); 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 = require("@neo4j-ndl/react"); const react_2 = require("react"); const MIN_VALUE = 0; const MAX_VALUE = 100; const STEP = 5; const INITIAL_VALUE = 50; const getValidationError = (input) => { const trimmed = input.trim(); if (!trimmed) { return { error: 'Value is required', isValid: false }; } const numValue = Number(trimmed); if (isNaN(numValue)) { return { error: 'Please enter a valid number', isValid: false }; } if (!Number.isInteger(numValue)) { return { error: 'Please enter a whole number', isValid: false }; } if (numValue < MIN_VALUE) { return { error: `Value must be at least ${MIN_VALUE}`, isValid: false }; } if (numValue > MAX_VALUE) { return { error: `Value must be at most ${MAX_VALUE}`, isValid: false }; } return { error: undefined, isValid: true }; }; const Component = () => { const [value, setValue] = (0, react_2.useState)(INITIAL_VALUE); const [inputValue, setInputValue] = (0, react_2.useState)(String(INITIAL_VALUE)); const { error, isValid } = getValidationError(inputValue); const hasError = !isValid; const handleInputChange = (0, react_2.useCallback)((e) => { const newInputValue = e.target.value; setInputValue(newInputValue); if (isValid) { setValue(Number(newInputValue)); } }, [setInputValue, setValue, isValid]); const handleSliderChange = (0, react_2.useCallback)((newValue) => { setValue(newValue); setInputValue(String(newValue)); }, [setInputValue, setValue]); return ((0, jsx_runtime_1.jsxs)("div", { className: "n-flex n-flex-col n-gap-token-16", children: [(0, jsx_runtime_1.jsx)(react_1.TextInput, { label: "Slider value", helpText: `Enter a value between ${MIN_VALUE} and ${MAX_VALUE}`, isRequired: true, errorText: error, htmlAttributes: { max: MAX_VALUE, min: MIN_VALUE, step: STEP, type: 'number', }, value: inputValue, onChange: handleInputChange }), (0, jsx_runtime_1.jsx)(react_1.Slider, { type: "single", value: value, onChange: handleSliderChange, minValue: MIN_VALUE, maxValue: MAX_VALUE, step: STEP, showSteps: true, showValues: true, isDisabled: hasError, inputProps: { 'aria-valuetext': `${value} out of ${MAX_VALUE}`, } })] })); }; exports.default = Component; //# sourceMappingURL=slider-single-controlled.story.js.map