From 0e13e1fe82275cb4f54edad84817ae1b6efce31d Mon Sep 17 00:00:00 2001 From: Nicolas Pieuchot Date: Fri, 18 Aug 2023 13:18:48 +0200 Subject: [PATCH] :bug: Last changes to fix number field in "alone" behaviour out of number selector. --- example/src/NumberField/NumberFieldPage.tsx | 38 +++++++++----- src/components/numberField/NumberField.tsx | 55 ++++++++++++++++----- 2 files changed, 69 insertions(+), 24 deletions(-) diff --git a/example/src/NumberField/NumberFieldPage.tsx b/example/src/NumberField/NumberFieldPage.tsx index bd6a9bf4..5c0acca6 100644 --- a/example/src/NumberField/NumberFieldPage.tsx +++ b/example/src/NumberField/NumberFieldPage.tsx @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useState } from 'react'; import { StyleSheet, View } from 'react-native'; import { NumberField, Screen, useTheme } from 'smartway-react-native-ui'; @@ -14,34 +14,46 @@ export const NumberFieldPage = () => { marginVertical: 10, }, }); + const value = '999'; return ( - - - - - - + + + + + + - + - - - + + + + + + {/* Example without value to hilight the last value on cleared case */} + ); diff --git a/src/components/numberField/NumberField.tsx b/src/components/numberField/NumberField.tsx index f4048028..d88d40ea 100644 --- a/src/components/numberField/NumberField.tsx +++ b/src/components/numberField/NumberField.tsx @@ -1,7 +1,6 @@ import React, { createRef, useEffect, useImperativeHandle, useState } from 'react'; import { StyleSheet, TextInput, TextInputBase, ViewStyle } from 'react-native'; import { useTheme } from '../../styles/themes'; -import { CardPage } from 'example/src/Card/CardPage'; type FieldBaseProps = React.ComponentProps; interface NumberFieldProps extends FieldBaseProps { @@ -12,7 +11,16 @@ interface NumberFieldProps extends FieldBaseProps { } export const NumberField = React.forwardRef( - ({ state = 'prefilled', size = 'm', ...props }: NumberFieldProps, ref) => { + ( + { + state = 'prefilled', + size = 'm', + minValue = 0, + maxValue = 999, + ...props + }: NumberFieldProps, + ref, + ) => { const theme = useTheme(); const [currentState, setCurrentState] = useState(state); @@ -22,6 +30,8 @@ export const NumberField = React.forwardRef( const [forcedState, setForcedState] = useState(true); const [firstContentChange, setFirstContentChange] = useState(true); const [firstValue, setFirstValue] = useState(); + const [value, setValue] = useState(props.value ?? ''); + const [lastValue, setLastValue] = useState(); useEffect(() => { if (forcedState) { @@ -34,14 +44,31 @@ export const NumberField = React.forwardRef( useEffect(() => { if (firstContentChange) { setFirstValue(props.value); + console.log('setFistvalueprops', props.value); setFirstContentChange(false); return; } setFilled(props.value !== firstValue); + if (cleanContent(props.value) !== '') setLastValue(value); checkContent(props.value); }, [props.value]); - const [lastValue, setLastValue] = useState(); + useEffect(() => { + if (firstContentChange) { + setFirstValue(value); + setFirstContentChange(false); + return; + } + setFilled(value !== firstValue); + if (cleanContent(value) !== '') { + setLastValue(value); + } + checkContent(value); + }, [value]); + + useEffect(() => { + console.log('lastval', lastValue); + }, [lastValue]); const stateStyle = () => { let borderColor = undefined; @@ -112,8 +139,8 @@ export const NumberField = React.forwardRef( const parsedValue = parseInt(cleanNumber); if (parsedValue !== undefined) { setError( - (props.minValue !== undefined && parsedValue < props.minValue) || - (props.maxValue !== undefined && parsedValue > props.maxValue), + (minValue !== undefined && parsedValue < minValue) || + (maxValue !== undefined && parsedValue > maxValue), ); } } @@ -122,7 +149,6 @@ export const NumberField = React.forwardRef( if (text !== undefined && text !== '') { const cleanNumber = text.replace(/[^0-9]/g, ''); const parsedValue = parseInt(cleanNumber); - console.log('>>>', parsedValue.toString()); return parsedValue.toString(); } return ''; @@ -130,10 +156,12 @@ export const NumberField = React.forwardRef( let onChangeText = (e: any) => { if (props?.onChangeText !== undefined) { props.onChangeText(e); + checkContent(props.value); + } else { + if (e == '') setValue(''); + else if (cleanContent(e) != '') setValue(cleanContent(e)); + checkContent(value); } - checkContent(props.value); - if (props.value !== '') setLastValue(props.value); - props.value = e; }; let onFocus = (e: any) => { setFocused(true); @@ -141,14 +169,19 @@ export const NumberField = React.forwardRef( }; let onBlur = (e: any) => { setFocused(false); - if (props.value === '' && firstValue !== undefined) onChangeText(firstValue); - else if (props.value === '' && lastValue !== undefined) onChangeText(lastValue); + console.log('vval>', value, '', firstValue, '<'); + if ((value === '' || props.value === '') && firstValue !== '') onChangeText(firstValue); + else if ((value === '' || props.value === '') && lastValue !== '') { + console.log(lastValue); + onChangeText(lastValue); + } if (props?.onBlur !== undefined) props.onBlur(e); }; return (