moneyinput

package
v0.0.39 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 27, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

templ: version: v0.3.977

Index

Constants

View Source
const DecimalPath = "/parse/decimal"

DecimalPath is the standard handler path for decimal parsing.

View Source
const MoneyPath = "/parse/money"

MoneyPath is the standard handler path for money parsing.

Variables

This section is empty.

Functions

func DecimalHandler

func DecimalHandler() http.HandlerFunc

DecimalHandler returns an http.HandlerFunc that parses a numeric value (supporting shorthand like 5k, 1.5M) and patches the signals with the formatted result.

Mount at a dedicated path:

r.Get(moneyinput.DecimalPath, moneyinput.DecimalHandler())

func DecimalInput

func DecimalInput(props DecimalProps) templ.Component

DecimalInput renders a text input that parses numeric shorthand (5k, 1.5M, 1,234.56) via server-side Datastar SSE. The parsed and formatted result is displayed inline.

func DecimalRoute added in v0.0.31

func DecimalRoute() func(chi.Router)

DecimalRoute returns a RouteOption that registers the decimal parser handler.

func FormatAmount

func FormatAmount(val float64) string

FormatAmount formats a float64 with 2 decimal places and thousands separators.

func MoneyHandler

func MoneyHandler(allowedCurrencies ...string) http.HandlerFunc

MoneyHandler returns an http.HandlerFunc that parses a money value (e.g., "USD 5k", "100 EUR") and patches the signals with the formatted amount and detected currency.

If allowedCurrencies is provided, only those currencies are accepted.

Mount at a dedicated path:

r.Get(moneyinput.MoneyPath, moneyinput.MoneyHandler("USD", "EUR"))

func MoneyInput

func MoneyInput(props MoneyProps) templ.Component

MoneyInput renders a text input that parses monetary values with optional currency codes (e.g., "USD 5k", "100 EUR") via server-side Datastar SSE. The parsed amount and detected currency are displayed inline.

func MoneyRoute added in v0.0.31

func MoneyRoute(allowedCurrencies ...string) func(chi.Router)

MoneyRoute returns a RouteOption that registers the money parser handler. If allowedCurrencies is provided, only those currencies are accepted.

Types

type DecimalProps

type DecimalProps struct {
	// ID uniquely identifies this component instance. Required.
	ID string
	// Class adds additional CSS classes to the input element.
	Class string
	// Attributes adds arbitrary HTML attributes to the input element.
	Attributes templ.Attributes
	// Name is the form field name attribute.
	Name string
	// Placeholder is the input placeholder text.
	Placeholder string
	// Value is the initial input value.
	Value string
	// ParseURL is the endpoint path for server-side parsing.
	// The component appends "?id=<ID>" automatically.
	ParseURL string
	// DebounceMs is the debounce delay in milliseconds. Defaults to 500.
	DebounceMs int
}

DecimalProps configures a decimal input field.

type MoneyProps

type MoneyProps struct {
	// ID uniquely identifies this component instance. Required.
	ID string
	// Class adds additional CSS classes to the input element.
	Class string
	// Attributes adds arbitrary HTML attributes to the input element.
	Attributes templ.Attributes
	// Name is the form field name attribute.
	Name string
	// Placeholder is the input placeholder text.
	Placeholder string
	// Value is the initial input value.
	Value string
	// ParseURL is the endpoint path for server-side parsing.
	// The component appends "?id=<ID>" automatically.
	ParseURL string
	// DebounceMs is the debounce delay in milliseconds. Defaults to 500.
	DebounceMs int
}

MoneyProps configures a money input field with currency support.

type ParsedAmount

type ParsedAmount struct {
	Value float64
	Valid bool
	Error string
}

ParsedAmount holds the result of parsing a numeric string.

func ParseAmount

func ParseAmount(raw string) ParsedAmount

ParseAmount parses a string into a float64 amount. Handles plain numbers, thousands separators, and shorthand suffixes (k, M, B). Returns Valid=true with zero Value for empty input.

type ParsedMoney

type ParsedMoney struct {
	Value    float64
	Currency string
	Valid    bool
	Error    string
}

ParsedMoney holds the result of parsing a money string.

func ParseMoney

func ParseMoney(raw string, allowedCurrencies []string) ParsedMoney

ParseMoney parses a string that may contain a currency code and amount. Accepts "USD 100", "100 EUR", or plain "100". If allowedCurrencies is non-empty, the detected currency must be in the list.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL