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

Why create a new signal in a Field for Monetary amounts? #270

Open
qingzhoufeihu opened this issue Dec 2, 2024 · 3 comments
Open

Why create a new signal in a Field for Monetary amounts? #270

qingzhoufeihu opened this issue Dec 2, 2024 · 3 comments
Assignees
Labels
question Further information is requested

Comments

@qingzhoufeihu
Copy link

What are the differences between the two versions of the code below?

<Field
  name="price"
  type="number"
  transform={toCustom((value) => value && value * 100, {
    on: 'change',
  })}
>
  {(field, props) => {
    const value = useSignal<string | number>('');
    useSignalEffect(() => {
      if (!Number.isNaN(field.value.value)) {
        input.value =
          field.value.value === undefined ? '' : field.value.value / 100;
      }
    });
    return <input {...props} type="number" value={value.value} />;
  }}
</Field>
<Field
  name="price"
  type="number"
  transform={toCustom((value) => value && value * 100, {
    on: 'change',
  })}
>
  {(field, props) => {
    return <input {...props} type="number" value={field.value.value === undefined ? '' : field.value.value / 100} />;
  }}
</Field>
@fabian-hiller
Copy link
Owner

fabian-hiller commented Dec 4, 2024

The first code example keeps incomplete or invalid numeric input such as 234. as the state of the <input /> field, while the second code example resets it to an empty input. It is important to note that the behavior of frameworks may be different and this code example is likely ported from our SolidJS docs.

@fabian-hiller fabian-hiller self-assigned this Dec 4, 2024
@fabian-hiller fabian-hiller added the question Further information is requested label Dec 4, 2024
@qingzhoufeihu
Copy link
Author

The first code example keeps incomplete or invalid numeric input such as 234. as the state of the <input /> field, while the second code example resets it to an empty input. It is important to note that the behavior of frameworks may be different and this code example is likely ported from our SolidJS docs.

mmexport1733291427937.png

It comes from react doc in modular form

@fabian-hiller
Copy link
Owner

Yeah, I know. What I meant is that I originally wrote it for SolidJS and just copied it over to the React docs. Maybe the behavior of React is different here.

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

No branches or pull requests

2 participants