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

Initial value should not be undefined #802

Open
punkpeye opened this issue Oct 13, 2024 · 5 comments
Open

Initial value should not be undefined #802

punkpeye opened this issue Oct 13, 2024 · 5 comments

Comments

@punkpeye
Copy link

Describe the bug and the expected behavior

const [form, fields] = useForm({
  defaultValue: {
    message: '',
  },
  lastResult: fetcher.data?.submission,
  onSubmit: (event, { action, formData, method }) => {
    event.preventDefault();

    fetcher.submit(formData, {
      action,
      method,
    });

    historyState.set('');

    messageInputControl.change('');

    setShowTip(false);
  },
});

const [inputFocused, setInputFocused] = useState<boolean>(false);

const formRef = useRef<HTMLFormElement>(null);

const messageInputControl = useInputControl(fields.message);

useEffect(() => {
  console.log('???', messageInputControl.value);
});

Conform version

v1.2.2

Steps to Reproduce the Bug or Issue

This prints:

??? undefined
??? undefined
??? undefined
???
???

The first couple of undefined value are unexpected, since the value of the input is known from the start (it is set in defaultValue).

What browsers are you seeing the problem on?

Chrome

Screenshots or Videos

No response

Additional context

No response

@punkpeye
Copy link
Author

Actually, this seems like a problem with fields.message – initialValue property is undefined initially, despite there being a default.

@punkpeye
Copy link
Author

Digging deeper, it looks like it is specific to empty string value.

If I set defaultValue to:

defaultValue: {
  message: 'foo',
},

it behaves as expected. It is only when message: '' that it breaks.

@edmundhung
Copy link
Owner

What you see is a side effect of conform normalising the default value for the ease of dirty checking. 😅

This means empty value and empty array are always transformed to undefined.

I do plan to fix this. But I am afraid some people might have already relied on such behaviour. So I am leaning towards fixing it in the next major version instead.

@lifeiscontent
Copy link
Contributor

@edmundhung any ideas on when that work might happen? just ran into this today 😅

@goamaan
Copy link

goamaan commented Nov 1, 2024

I'm facing this same problem with a checkbox/switch field being initialized with false but meta.initialValue is undefined still

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

No branches or pull requests

4 participants