Skip to content
CodeAtlas UI

Inputs

MoneyInput

BRL currency field. The bound value is integer cents and the display is formatted as you type.

<script>
  import { MoneyInput } from "@codeatlasdev/ui/forms";
  let price = $state(123456); // R$ 1.234,56
</script>

<MoneyInput label="Price" bind:value={price} />

Import from the @codeatlasdev/ui/forms subpath. The value is always an integer number of cents, so there is no floating-point rounding. Digits shift in from the right: typing 123456 gives R$ 1.234,56. min and max clamp the value, and an out-of-range value set from outside is clamped back into value.

Default

Type 123456 to get R$ 1.234,56.

value = 123456 cents → R$ 1.234,56

Code
svelte
<script lang="ts">
  import { MoneyInput, centsToDisplay } from '@codeatlasdev/ui/forms';

  let price = $state(123456);
</script>

<div class="col">
  <MoneyInput label="Price" bind:value={price} />
  <p class="out">value = {price} cents → {centsToDisplay(price)}</p>
</div>

<style>
  .col {
    display: flex;
    flex-direction: column;
    gap: var(--ca-space-3);
    width: 100%;
    max-width: 400px;
  }

  .out {
    margin: 0;
    font-family: var(--ca-font-mono);
    font-size: var(--ca-font-size-caption);
    color: var(--ca-text-secondary);
  }
</style>

Min, max, negative, error and disabled

Deposit is clamped between R$ 10,00 and R$ 100,00. Include a minus sign to toggle the sign when allowNegative is set.

Between R$ 10,00 and R$ 100,00.

Code
svelte
<script lang="ts">
  import { MoneyInput } from '@codeatlasdev/ui/forms';

  let deposit = $state(5000);
  let adjustment = $state(-2500);
</script>

<div class="col">
  <MoneyInput
    label="Deposit"
    bind:value={deposit}
    min={1000}
    max={10000}
    description="Between R$ 10,00 and R$ 100,00."
  />
  <MoneyInput label="Adjustment" bind:value={adjustment} allowNegative />
  <MoneyInput label="Amount" value={0} error="Amount must be greater than zero." />
  <MoneyInput label="Locked" value={99900} disabled />
</div>

<style>
  .col {
    display: flex;
    flex-direction: column;
    gap: var(--ca-space-4);
    width: 100%;
    max-width: 400px;
  }
</style>

Helpers

Pure functions exported from @codeatlasdev/ui/forms.

centsToDisplay(123456) = R$ 1.234,56

parseInputToCents("R$ 1.234,56") = 123456

Code
svelte
<script lang="ts">
  import { centsToDisplay, parseInputToCents } from '@codeatlasdev/ui/forms';
</script>

<div class="col">
  <p class="out">centsToDisplay(123456) = {centsToDisplay(123456)}</p>
  <p class="out">parseInputToCents("R$ 1.234,56") = {parseInputToCents('R$ 1.234,56')}</p>
</div>

<style>
  .col {
    display: flex;
    flex-direction: column;
    gap: var(--ca-space-2);
  }

  .out {
    margin: 0;
    font-family: var(--ca-font-mono);
    font-size: var(--ca-font-size-caption);
    color: var(--ca-text-secondary);
  }
</style>

Props

PropTypeDefaultDescription
valuenumber0Bindable amount in integer cents.
minnumber–Minimum value in cents.
maxnumber–Maximum value in cents.
allowNegativebooleanfalseAccepts negative amounts. Type a minus sign to toggle.
labelstring–Label text.
placeholderstringcentsToDisplay(0)Input placeholder.
errorstring–Error message. Puts the field in the error state.
descriptionstring–Hint shown below the field.
disabledbooleanfalseDisables the input.
requiredbooleanfalseMarks the field as required.
readonlybooleanfalseMakes the input read-only.
size'sm' | 'md''md'Field height.
namestring–Native name, for form submission.
leading / trailingSnippet–Content before or after the input.
classstring–Extra classes on the root.
onchange(cents: number) => void–Called on every edit with the new cents value.
onblur(e: FocusEvent) => void–Called when the input loses focus. The display is re-formatted first.