Skip to content

Commit

Permalink
💄 QuantitySelector - get input sources
Browse files Browse the repository at this point in the history
  • Loading branch information
ulricden committed Feb 1, 2024
1 parent c737e0e commit c5cf6ae
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions src/components/numberSelector/NumberSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import { FieldState, NumberField } from '../numberField/NumberField';
import type { IconName } from '../icons/IconProps';
import { NumberValidator } from '../numberField/NumberValidator';

type Source = 'keyboard' | 'minus_button' | 'plus_button';

export interface Props {
validator: NumberValidator;
initialValue: number | undefined;
value: number | undefined;
onEndEditing: (value: string | undefined) => void;
onKeyPress?: (
oldValue: string | undefined,
newValue: string | undefined,
) => void;
onEndEditing: (value: string | undefined, source: Source) => void;
style?: ViewStyle;
minusIcon?: IconName;
plusIcon?: IconName;
Expand Down Expand Up @@ -54,7 +52,6 @@ export const NumberSelector = ({
initialValue,
value,
onEndEditing,
onKeyPress,
style,
minusIcon = 'minus',
plusIcon = 'add',
Expand Down Expand Up @@ -115,15 +112,15 @@ export const NumberSelector = ({
if (addDisabled) return;
const newValue = computeIncrementedValue().toString();
onChangeText(newValue);
onEndEditing !== undefined && onEndEditing(newValue);
onEndEditing !== undefined && onEndEditing(newValue, 'plus_button');
};

const onMinus = () => {
Keyboard.dismiss();
if (minusDisabled) return;
const newValue = computeDecrementedValue().toString();
onChangeText(newValue);
onEndEditing !== undefined && onEndEditing(newValue);
onEndEditing !== undefined && onEndEditing(newValue, 'minus_button');
};

const onChangeText = (text: string) => {
Expand All @@ -132,9 +129,6 @@ export const NumberSelector = ({
text !== initialValue?.toString() ? 'filled' : 'filledWithDefault',
);
if (validator.validateFormat(text)) {
if (Keyboard.isVisible() && onKeyPress) {
onKeyPress(tempValue, text);
}
setTempValue(text);
setLastValidValue(getParsedValue());
setError(!validator.validateMinMax(text));
Expand Down Expand Up @@ -186,7 +180,7 @@ export const NumberSelector = ({
onFocus={onFocus}
onBlur={onBlur}
onEndEditing={(e) => {
onEndEditing(e.nativeEvent.text);
onEndEditing(e.nativeEvent.text, 'keyboard');
}}
size={size}
error={error}
Expand Down

0 comments on commit c5cf6ae

Please sign in to comment.