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

Dollar Input fixed to 2 decimal points #228

Open
robodna opened this issue Jul 18, 2024 · 3 comments
Open

Dollar Input fixed to 2 decimal points #228

robodna opened this issue Jul 18, 2024 · 3 comments
Assignees
Labels
question Further information is requested

Comments

@robodna
Copy link

robodna commented Jul 18, 2024

How do I create a transform or mask to ensure a dollar amount entered has 2 decimal points? So entering 10.5 would result in 10.50

@fabian-hiller
Copy link
Owner

fabian-hiller commented Jul 18, 2024

This could work:

<Field
  name="price"
  transform={toCustom(
    (value) => {
      // Just add decimal points
      return Number(input).toFixed(2);
      // Or format to currency string
      return Number(input).toLocaleString('en', { style: "currency", currency: "USD" });
    },
    { on: 'input' }
  )}
>
  {(field, props) => (
    <input
      {...props}
      type="text"
      placeholder="$0.00"
      value={field.value}
    />
  )}
</Field>

@fabian-hiller fabian-hiller self-assigned this Jul 18, 2024
@fabian-hiller fabian-hiller added the question Further information is requested label Jul 18, 2024
@robodna
Copy link
Author

robodna commented Jul 18, 2024

I tried this however the input box allows 123.12334 even if the transformed value is $123.12

Also, the <input is type="number" and not type="text". I was envisioning a type=number input with a 'mask' which only allows the user to enter a valid currency number. The server/backend returns a number for currency ( eg. 10.5 for 10.50 ). I was looking to just add a prefixed '$' to the input and not make it part of the value.

@fabian-hiller
Copy link
Owner

Not sure if I am following, but you can add a "$" before the text element, but that has nothing to do with Modular Forms. It's just the visual representation of your form field. Since Modular Forms is headless, there are no limits.

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