Skip to content
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

LF-4618 Fix remove error entry when click X on number input #3643

Open
wants to merge 3 commits into
base: integration
Choose a base branch
from

Conversation

Tbrid
Copy link
Collaborator

@Tbrid Tbrid commented Jan 13, 2025

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

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

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

  • Passes test case
  • UI components visually reviewed on desktop view
  • UI components visually reviewed on mobile view
  • Other (please explain)

Checklist:

  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • The precommit and linting ran successfully
  • I have added or updated language tags for text that's part of the UI
  • I have added "MISSING" for all new language tags to languages I don't speak
  • I have added the GNU General Public License to all new files

@Tbrid Tbrid requested review from a team as code owners January 13, 2025 18:26
@Tbrid Tbrid requested review from antsgar and Duncan-Brain and removed request for a team January 13, 2025 18:26
Copy link
Collaborator

@Duncan-Brain Duncan-Brain left a 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.

@Tbrid Tbrid requested a review from Duncan-Brain January 20, 2025 17:21
Copy link
Collaborator

@Duncan-Brain Duncan-Brain left a 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)}
Copy link
Collaborator

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?

Copy link
Collaborator Author

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.

Copy link
Collaborator

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.

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants