Debian Codegraph @

node/@patternfly/react-core/src/components/Slider/examples/SliderValueInput.tsx

raw
1 import { useState } from 'react';
2 import { Slider, SliderOnChangeEvent } from '@patternfly/react-core';
3
4 export const SliderValueInput: React.FunctionComponent = () => {
5 const [valueDiscrete, setValueDiscrete] = useState(62.5);
6 const [inputValueDiscrete, setInputValueDiscrete] = useState(5);
7 const [valuePercent, setValuePercent] = useState(50);
8 const [inputValuePercent, setInputValuePercent] = useState(50);
9 const [valueContinuous, setValueContinuous] = useState(50);
10 const [inputValueContinuous, setInputValueContinuous] = useState(50);
11
12 const stepsDiscrete = [
13 { value: 0, label: '0' },
14 { value: 12.5, label: '1', isLabelHidden: true },
15 { value: 25, label: '2' },
16 { value: 37.5, label: '3', isLabelHidden: true },
17 { value: 50, label: '4' },
18 { value: 62.5, label: '5', isLabelHidden: true },
19 { value: 75, label: '6' },
20 { value: 87.5, label: '7', isLabelHidden: true },
21 { value: 100, label: '8' }
22 ];
23 const stepsPercent = [
24 { value: 0, label: '0%' },
25 { value: 25, label: '25%', isLabelHidden: true },
26 { value: 50, label: '50%' },
27 { value: 75, label: '75%', isLabelHidden: true },
28 { value: 100, label: '100%' }
29 ];
30
31 const onChangeDiscrete = (
32 _event: SliderOnChangeEvent,
33 value: number,
34 inputValue: number,
35 setLocalInputValue: React.Dispatch<React.SetStateAction<number>>
36 ) => {
37 let newValue;
38 let newInputValue;
39
40 if (inputValue === undefined) {
41 const step = stepsDiscrete.find((step) => step.value === value);
42 newInputValue = step ? step.label : 0;
43 newInputValue = Number(newInputValue);
44 newValue = value;
45 } else {
46 const maxValue = Number(stepsDiscrete[stepsDiscrete.length - 1].label);
47 if (inputValue > maxValue) {
48 newValue = Number(stepsDiscrete[stepsDiscrete.length - 1].value);
49 newInputValue = maxValue;
50 setLocalInputValue(maxValue);
51 } else {
52 const minValue = Number(stepsDiscrete[0].label);
53 if (inputValue < minValue) {
54 newValue = Number(stepsDiscrete[0].value);
55 newInputValue = minValue;
56 setLocalInputValue(minValue);
57 } else {
58 const stepIndex = stepsDiscrete.findIndex((step) => Number(step.label) >= inputValue);
59 if (Number(stepsDiscrete[stepIndex].label) === inputValue) {
60 newValue = stepsDiscrete[stepIndex].value;
61 newInputValue = inputValue;
62 } else {
63 const midpoint = (Number(stepsDiscrete[stepIndex].label) + Number(stepsDiscrete[stepIndex - 1].label)) / 2;
64 if (midpoint > inputValue) {
65 newValue = stepsDiscrete[stepIndex - 1].value;
66 newInputValue = Number(stepsDiscrete[stepIndex - 1].label);
67 } else {
68 newValue = stepsDiscrete[stepIndex].value;
69 newInputValue = Number(stepsDiscrete[stepIndex].label);
70 }
71 }
72 }
73 }
74 }
75
76 setInputValueDiscrete(newInputValue);
77 setValueDiscrete(newValue);
78 };
79
80 const onChangePercent = (
81 _event: SliderOnChangeEvent,
82 value: number,
83 inputValue: number,
84 setLocalInputValue: React.Dispatch<React.SetStateAction<number>>
85 ) => {
86 let newValue;
87 let newInputValue;
88
89 if (inputValue === undefined) {
90 const step = stepsPercent.find((step) => step.value === value);
91 newInputValue = step ? step.label.slice(0, -1) : 0;
92 newInputValue = Number(newInputValue);
93 newValue = value;
94 } else {
95 const maxValue = Number(stepsPercent[stepsPercent.length - 1].label.slice(0, -1));
96 if (inputValue > maxValue) {
97 newValue = Number(stepsPercent[stepsPercent.length - 1].value);
98 newInputValue = maxValue;
99 setLocalInputValue(maxValue);
100 } else {
101 const minValue = Number(stepsPercent[0].label.slice(0, -1));
102 if (inputValue < minValue) {
103 newValue = minValue;
104 setLocalInputValue(minValue);
105 } else {
106 const stepIndex = stepsPercent.findIndex((step) => Number(step.label.slice(0, -1)) >= inputValue);
107 if (Number(stepsPercent[stepIndex].label.slice(0, -1)) === inputValue) {
108 newValue = stepsPercent[stepIndex].value;
109 newInputValue = inputValue;
110 } else {
111 const midpoint =
112 (Number(stepsPercent[stepIndex].label.slice(0, -1)) +
113 Number(stepsPercent[stepIndex - 1].label.slice(0, -1))) /
114 2;
115 if (midpoint > inputValue) {
116 newValue = stepsPercent[stepIndex - 1].value;
117 newInputValue = Number(stepsPercent[stepIndex - 1].label.slice(0, -1));
118 } else {
119 newValue = stepsPercent[stepIndex].value;
120 newInputValue = Number(stepsPercent[stepIndex].label.slice(0, -1));
121 }
122 }
123 }
124 }
125 }
126
127 setInputValuePercent(newInputValue);
128 setValuePercent(newValue);
129 };
130
131 const onChangeContinuous = (
132 _event: SliderOnChangeEvent,
133 value: number,
134 inputValue: number,
135 setLocalInputValue: React.Dispatch<React.SetStateAction<number>>
136 ) => {
137 let newValue;
138 if (inputValue === undefined) {
139 newValue = Math.floor(value);
140 } else {
141 if (inputValue > 100) {
142 newValue = 100;
143 setLocalInputValue(100);
144 } else if (inputValue < 0) {
145 newValue = 0;
146 setLocalInputValue(0);
147 } else {
148 newValue = Math.floor(inputValue);
149 }
150 }
151 setInputValueContinuous(newValue);
152 setValueContinuous(newValue);
153 };
154
155 return (
156 <>
157 <Slider
158 value={valueDiscrete}
159 isInputVisible
160 inputValue={inputValueDiscrete}
161 customSteps={stepsDiscrete}
162 onChange={onChangeDiscrete}
163 />
164 <br />
165 <Slider
166 value={valuePercent}
167 isInputVisible
168 inputValue={inputValuePercent}
169 inputLabel="%"
170 onChange={onChangePercent}
171 customSteps={stepsPercent}
172 />
173 <br />
174 <Slider
175 value={valueContinuous}
176 isInputVisible
177 inputValue={inputValueContinuous}
178 inputLabel="%"
179 onChange={onChangeContinuous}
180 />
181 </>
182 );
183 };