@@ -17,12 +17,12 @@ interface Props {
1717export const IngredientFormField = React . memo ( function IngredientFormField ( props : Props ) {
1818 const [ ingredientQuery , setIngredientQuery ] = useState < string > ( props . ingredient . ingredient . name ) ;
1919 const [ unit , setUnit ] = useState < string > ( props . ingredient . unit ) ;
20- const [ amount , setAmount ] = useState < string > ( props . ingredient . amount === 0 ? '' : String ( props . ingredient . amount ) ) ;
20+ const [ amount , setAmount ] = useState < string > ( props . ingredient . amount === undefined || props . ingredient . amount === null ? '' : String ( props . ingredient . amount ) ) ;
2121
2222 useEffect ( ( ) => {
2323 setIngredientQuery ( props . ingredient . ingredient . name ) ;
2424 setUnit ( props . ingredient . unit ) ;
25- setAmount ( props . ingredient . amount === 0 ? '' : String ( props . ingredient . amount ) ) ;
25+ setAmount ( props . ingredient . amount === undefined || props . ingredient . amount === null ? '' : String ( props . ingredient . amount ) ) ;
2626 } , [ props . ingredient ] ) ;
2727
2828 const [ availableUnits , setAvailableUnits ] = useState < string [ ] > ( [ ] ) ;
@@ -38,10 +38,17 @@ export const IngredientFormField = React.memo(function IngredientFormField(props
3838
3939 const invokeIngredientUpdate = ( ingredientName : string , newAmount : string , newUnit : string ) => {
4040 const existingIngredient = availableIngredients . find ( ( ingredient ) => ingredient . name . toLowerCase ( ) === ingredientName . toLowerCase ( ) ) ;
41+
42+ let prasedAmount : number | null = parseFloat ( newAmount ) ;
43+ // Check if its a number
44+ if ( prasedAmount . toString ( ) !== newAmount || newAmount === '' ) {
45+ prasedAmount = null ;
46+ }
47+
4148 if ( existingIngredient ) {
42- props . onIngredientChange ( { ingredient : existingIngredient , amount : newAmount === '' ? 0 : parseFloat ( newAmount ) , unit : newUnit } , props . ingredientIndex ) ;
49+ props . onIngredientChange ( { ingredient : existingIngredient , amount : prasedAmount , unit : newUnit } , props . ingredientIndex ) ;
4350 } else {
44- props . onIngredientChange ( { ingredient : { name : ingredientName } , amount : newAmount === '' ? 0 : parseFloat ( newAmount ) , unit : newUnit } , props . ingredientIndex ) ;
51+ props . onIngredientChange ( { ingredient : { name : ingredientName } , amount : prasedAmount , unit : newUnit } , props . ingredientIndex ) ;
4552 }
4653 } ;
4754
@@ -61,7 +68,8 @@ export const IngredientFormField = React.memo(function IngredientFormField(props
6168 setAmount ( text ) ;
6269
6370 const prasedAmount = parseFloat ( text ) ;
64- if ( prasedAmount . toString ( ) === text ) {
71+ // Check if its a number
72+ if ( prasedAmount . toString ( ) === text || text === '' ) {
6573 invokeIngredientUpdate ( ingredientQuery , text , unit ) ;
6674 }
6775 } ;
@@ -75,43 +83,41 @@ export const IngredientFormField = React.memo(function IngredientFormField(props
7583 } , [ ] ) ;
7684
7785 return (
78- < >
79- < View style = { { borderWidth : 1 , borderColor : Colors . grey50 , padding : 10 , borderRadius : 16 } } >
80- < View style = { { flexDirection : 'row' , alignItems : 'center' } } >
81- < View style = { { flex : 1 , flexDirection : 'column' } } >
82- < View style = { { flex : 1 , flexDirection : 'row' } } >
83- < TextInput
84- mode = "outlined"
85- style = { { width : 100 } }
86- keyboardType = "numeric"
87- value = { ( amount ? amount . toString ( ) : '' ) }
88- label = { t ( 'screens.editRecipe.amount' ) }
89- onChangeText = { onAmountChange } />
90- < Spacer width = { 5 } />
91- < SelectionPopup
92- style = { { flex : 1 } }
93- label = { t ( 'screens.editRecipe.unit' ) }
94- value = { unit }
95- options = { availableUnits . map ( ( availableUnit , index ) => ( { key : index . toString ( ) , value : availableUnit } ) ) }
96- onValueChanged = { ( selectedOption ) => setUnit ( selectedOption . value ) }
97- />
98- </ View >
99- < Spacer height = { 5 } />
100- < View style = { { justifyContent : 'center' , flex : 1 } } >
101- < SelectionPopup
102- label = { t ( 'screens.editRecipe.ingredient' ) }
103- value = { ingredientQuery }
104- options = { availableIngredients . map ( ( ingredient ) => ( { key : ingredient . id ? ingredient . id . toString ( ) : '' , value : ingredient . name } ) ) }
105- onValueChanged = { ( selectedOption ) => setIngredient ( selectedOption . value ) }
106- allowAdditionalValues = { true }
107- />
108- </ View >
86+ < View style = { { borderWidth : 1 , borderColor : Colors . grey50 , padding : 10 , borderRadius : 16 } } >
87+ < View style = { { flexDirection : 'row' , alignItems : 'center' } } >
88+ < View style = { { flex : 1 , flexDirection : 'column' } } >
89+ < View style = { { flex : 1 , flexDirection : 'row' } } >
90+ < TextInput
91+ mode = "outlined"
92+ style = { { width : 100 } }
93+ keyboardType = "numeric"
94+ value = { ( amount !== undefined ? amount . toString ( ) : '' ) }
95+ label = { t ( 'screens.editRecipe.amount' ) }
96+ onChangeText = { onAmountChange } />
97+ < Spacer width = { 5 } />
98+ < SelectionPopup
99+ style = { { flex : 1 } }
100+ label = { t ( 'screens.editRecipe.unit' ) }
101+ value = { unit }
102+ options = { availableUnits . map ( ( availableUnit , index ) => ( { key : index . toString ( ) , value : availableUnit } ) ) }
103+ onValueChanged = { ( selectedOption ) => setUnit ( selectedOption . value ) }
104+ />
105+ </ View >
106+ < Spacer height = { 5 } />
107+ < View style = { { justifyContent : 'center' , flex : 1 } } >
108+ < SelectionPopup
109+ label = { t ( 'screens.editRecipe.ingredient' ) }
110+ value = { ingredientQuery }
111+ options = { availableIngredients . map ( ( ingredient ) => ( { key : ingredient . id ? ingredient . id . toString ( ) : '' , value : ingredient . name } ) ) }
112+ onValueChanged = { ( selectedOption ) => setIngredient ( selectedOption . value ) }
113+ allowAdditionalValues = { true }
114+ />
109115 </ View >
110- < IconButton
111- icon = "delete-outline"
112- onPress = { ( ) => props . onRemovePress ( props . ingredientIndex ) } />
113116 </ View >
117+ < IconButton
118+ icon = "delete-outline"
119+ onPress = { ( ) => props . onRemovePress ( props . ingredientIndex ) } />
114120 </ View >
115- </ >
121+ </ View >
116122 ) ;
117123} ) ;
0 commit comments