@alfalab/core-components-slider-input 8.3.6 → 9.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/Component.tsx CHANGED
@@ -2,6 +2,7 @@ import React, {
2
2
  ChangeEvent,
3
3
  cloneElement,
4
4
  ComponentType,
5
+ FocusEvent,
5
6
  forwardRef,
6
7
  Fragment,
7
8
  isValidElement,
@@ -13,6 +14,8 @@ import cn from 'classnames';
13
14
  import { Input as DefaultInput, InputProps } from '@alfalab/core-components-input';
14
15
  import { Slider, SliderProps } from '@alfalab/core-components-slider';
15
16
 
17
+ import { OnChangeType, OnInputChangeType } from './types/propTypes';
18
+
16
19
  import styles from './index.module.css';
17
20
 
18
21
  export type SliderInputProps = Omit<
@@ -29,6 +32,13 @@ export type SliderInputProps = Omit<
29
32
  */
30
33
  max?: number;
31
34
 
35
+ /**
36
+ * Предотвращает ввод числа если оно больше или меньше допустимого.
37
+ * При событии blur установится число по верхней границе, если оно больше допустимого, и наоборот - по нижней границе, если число меньше допустимого.
38
+ * @default false
39
+ */
40
+ lockLimit?: boolean;
41
+
32
42
  /**
33
43
  * Массив подписей к слайдеру
34
44
  */
@@ -94,15 +104,12 @@ export type SliderInputProps = Omit<
94
104
  /**
95
105
  * Обработчик изменения значения через слайдер или поле ввода
96
106
  */
97
- onChange?: (
98
- event: ChangeEvent<HTMLInputElement> | null,
99
- payload: { value: number | '' },
100
- ) => void;
107
+ onChange?: OnChangeType;
101
108
 
102
109
  /**
103
110
  * Обработчик ввода
104
111
  */
105
- onInputChange?: (event: ChangeEvent<HTMLInputElement>, payload: { value: number | '' }) => void;
112
+ onInputChange?: OnInputChangeType;
106
113
 
107
114
  /**
108
115
  * Обработчик изменения слайдера
@@ -149,6 +156,7 @@ export const SliderInput = forwardRef<HTMLInputElement, SliderInputProps>(
149
156
  min = 0,
150
157
  max = 100,
151
158
  step = 1,
159
+ lockLimit = false,
152
160
  block,
153
161
  steps = [],
154
162
  sliderValue = +value,
@@ -197,6 +205,39 @@ export const SliderInput = forwardRef<HTMLInputElement, SliderInputProps>(
197
205
  [getValidInputValue, onChange, onInputChange],
198
206
  );
199
207
 
208
+ const handleInputBlur = useCallback(
209
+ (event: FocusEvent) => {
210
+ const { value: inputValue } = event.target as HTMLInputElement;
211
+ const validValue = getValidInputValue(inputValue);
212
+
213
+ const getEventPayloadValue = (payload: number | '') => {
214
+ if (payload > max) {
215
+ return max;
216
+ }
217
+
218
+ if (payload < min) {
219
+ return min;
220
+ }
221
+
222
+ return '';
223
+ };
224
+
225
+ if (lockLimit) {
226
+ if (onChange) {
227
+ onChange(null, {
228
+ value: getEventPayloadValue(validValue),
229
+ });
230
+ }
231
+ if (onInputChange) {
232
+ onInputChange(null, {
233
+ value: getEventPayloadValue(validValue),
234
+ });
235
+ }
236
+ }
237
+ },
238
+ [getValidInputValue, lockLimit, max, min, onChange, onInputChange],
239
+ );
240
+
200
241
  return (
201
242
  <div
202
243
  className={cn(
@@ -218,6 +259,7 @@ export const SliderInput = forwardRef<HTMLInputElement, SliderInputProps>(
218
259
  ref={ref}
219
260
  value={value.toString()}
220
261
  onChange={handleInputChange}
262
+ onBlur={handleInputBlur}
221
263
  block={true}
222
264
  size={size}
223
265
  label={label}
@@ -0,0 +1,11 @@
1
+ import { ChangeEvent } from 'react';
2
+
3
+ export type OnChangeType = (
4
+ event: ChangeEvent<HTMLInputElement> | null,
5
+ payload: { value: number | '' },
6
+ ) => void;
7
+
8
+ export type OnInputChangeType = (
9
+ event: ChangeEvent<HTMLInputElement> | null,
10
+ payload: { value: number | '' },
11
+ ) => void;
@@ -0,0 +1,8 @@
1
+ import { ChangeEvent } from 'react';
2
+ type OnChangeType = (event: ChangeEvent<HTMLInputElement> | null, payload: {
3
+ value: number | '';
4
+ }) => void;
5
+ type OnInputChangeType = (event: ChangeEvent<HTMLInputElement> | null, payload: {
6
+ value: number | '';
7
+ }) => void;
8
+ export { OnChangeType, OnInputChangeType };
@@ -0,0 +1,2 @@
1
+ 'use strict';
2
+