-
Notifications
You must be signed in to change notification settings - Fork 88
LF-4618 Fix remove error entry when click X on number input #3643
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: integration
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @Tbrid I think the defaultValue was placed as the last option was for the case where one arrives at the form with pre-filled values.
So one example is when you navigate to the next page of the form and then you go back in the form flow:
Screen.Recording.2025-01-16.at.7.19.10.PM.mov
One other thing about this field surprises me: it does not validate when blurred. It validates when another field is validated which makes sense. But it seems like it should also be validating on blur.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks looking good to me !
@@ -86,7 +86,7 @@ export default function NumberInput<T extends FieldValues>({ | |||
{...inputProps} | |||
className={className} | |||
error={fieldState.error?.message} | |||
onResetIconClick={reset} | |||
onResetIconClick={() => reset(defaultValue)} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
initialValue
is field.value || get(formState.defaultValues, name) || defaultValue
, so I don't think this would work unless defaultValue
is passed to this component.
Can we think of a way to use the correct initialValue
instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure what you mean? The defaultValue
is being passed as a prop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, the issue is that initialValue
is set as field.value || get(formState.defaultValues, name) || defaultValue
(line 64), but reset()
sets the value to defaultValue
.
LiteFarm/packages/webapp/src/components/Form/NumberInput/index.tsx
Lines 63 to 65 in 0f8a278
const { inputProps, reset, numericValue, increment, decrement } = useNumberInput({ | |
initialValue: field.value || get(formState.defaultValues, name) || defaultValue, | |
allowDecimal, |
When resetting, we expect the input to display initialValue
, but if field.value
or get(formState.defaultValues, name)
is used, reset()
won’t work as expected.
reset: () => update(initialValue ?? NaN)
in useNumberInput
seems correct, but the problem is that initialValue
changes. Can you think of a way to ensure reset()
always works when field.value
or get(formState.defaultValues, name)
is used as initialValue
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Tbrid did you get a chance to take a second look at this based on @SayakaOno's feedback
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I was caught up with my own stuff lately, I just did a test with commenting out defaultValue and so initialValue will be using the other params, and reset still works, so I think there isn't an issue here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for jumping back in — really appreciate it!
Hope this example helps clarify the issue a bit more.
Here’s a screen recording of the behaviour:
Screen.Recording.2025-04-16.at.9.59.46.AM.mov
In the QuantityApplicationRate
component, we're passing 100
as a defaultValue
, but the actual value that's used comes from react-hook-form
's value (set in the grandparent component) — in this case, it's 80
(field.value
in line 64 in this snippet above).
So when resetting, we should restore field.value
(80
), not 100
.
That said, I’m not entirely sure whether it should reset or just clear — the latter makes more sense to me. I’ll check with the team to confirm what’s intended.
For reference, here's the temporary change I made for the demo:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Tbrid
We discussed the behaviour and confirmed that clicking the clear button should clear the input, not restore the initial value.
Could you please revert the changes to reset
for now and use clear
instead? 🙏
The reset
function still needs to be fixed, so I’ve created a separate bug ticket for it.
@@ -86,7 +86,7 @@ export default function NumberInput<T extends FieldValues>({ | |||
{...inputProps} | |||
className={className} | |||
error={fieldState.error?.message} | |||
onResetIconClick={reset} | |||
onResetIconClick={() => reset(defaultValue)} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Tbrid
We discussed the behaviour and confirmed that clicking the clear button should clear the input, not restore the initial value.
Could you please revert the changes to reset
for now and use clear
instead? 🙏
The reset
function still needs to be fixed, so I’ve created a separate bug ticket for it.
@@ -199,7 +199,7 @@ export default function useNumberInput({ | |||
return { | |||
numericValue, | |||
inputProps, | |||
reset: () => update(initialValue ?? NaN), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be appreciated if you could add a comment to this line, like: // TODO: Fix before use (LF-4798)
@@ -128,6 +128,7 @@ export default function AddAnimalsFormCard({ | |||
label={t('common:COUNT')} | |||
className={styles.countInput} | |||
allowDecimal={false} | |||
defaultValue={1} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the changes! 🙏
It looks like the default values are already set in animalBasicsDefaultValues
in AddAnimalBasics
. Is there any other reason we’d need this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you so much!
Description
Bug: error value in animals count input not restored to default value when clicking "X".
Cause: default value not being used and logic of restoring default is wrong.
Fix: Change default value logic to be using prop default value instead of current value in the field and add default value prop to field input
Jira link: https://lite-farm.atlassian.net/browse/LF-4618
Type of change
How Has This Been Tested?
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration
Checklist: