@canutin/svelte-currency-input 0.7.2 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CurrencyInput.svelte +25 -6
- package/CurrencyInput.svelte.d.ts +9 -0
- package/README.md +34 -3
- package/package.json +1 -1
package/CurrencyInput.svelte
CHANGED
|
@@ -3,6 +3,12 @@ const DEFAULT_CURRENCY = 'USD';
|
|
|
3
3
|
const DEFAULT_NAME = 'total';
|
|
4
4
|
const DEFAULT_VALUE = 0;
|
|
5
5
|
const DEFAULT_FRACTION_DIGITS = 2;
|
|
6
|
+
const DEFAULT_CLASS_WRAPPER = 'currencyInput';
|
|
7
|
+
const DEFAULT_CLASS_UNFORMATTED = 'currencyInput__unformatted';
|
|
8
|
+
const DEFAULT_CLASS_FORMATTED = 'currencyInput__formatted';
|
|
9
|
+
const DEFAULT_CLASS_FORMATTED_POSITIVE = 'currencyInput__formatted--positive';
|
|
10
|
+
const DEFAULT_CLASS_FORMATTED_NEGATIVE = 'currencyInput__formatted--negative';
|
|
11
|
+
const DEFAULT_CLASS_FORMATTED_ZERO = 'currencyInput__formatted--zero';
|
|
6
12
|
export let value = DEFAULT_VALUE;
|
|
7
13
|
export let locale = DEFAULT_LOCALE;
|
|
8
14
|
export let currency = DEFAULT_CURRENCY;
|
|
@@ -12,6 +18,8 @@ export let disabled = false;
|
|
|
12
18
|
export let placeholder = DEFAULT_VALUE;
|
|
13
19
|
export let isNegativeAllowed = true;
|
|
14
20
|
export let fractionDigits = DEFAULT_FRACTION_DIGITS;
|
|
21
|
+
export let inputClasses = null;
|
|
22
|
+
export let onValueChange = () => { };
|
|
15
23
|
// Formats value as: e.g. $1,523.00 | -$1,523.00
|
|
16
24
|
const formatCurrency = (value, maximumFractionDigits, minimumFractionDigits) => {
|
|
17
25
|
return new Intl.NumberFormat(locale, {
|
|
@@ -86,6 +94,7 @@ const setUnformattedValue = (event) => {
|
|
|
86
94
|
}
|
|
87
95
|
}
|
|
88
96
|
}
|
|
97
|
+
onValueChange(value);
|
|
89
98
|
};
|
|
90
99
|
const setFormattedValue = () => {
|
|
91
100
|
// Previous caret position
|
|
@@ -110,14 +119,24 @@ $: isNegative = value < 0;
|
|
|
110
119
|
$: value, setFormattedValue();
|
|
111
120
|
</script>
|
|
112
121
|
|
|
113
|
-
<div class=
|
|
114
|
-
<input
|
|
122
|
+
<div class={inputClasses?.wrapper ?? DEFAULT_CLASS_WRAPPER}>
|
|
123
|
+
<input
|
|
124
|
+
class={inputClasses?.unformatted ?? DEFAULT_CLASS_UNFORMATTED}
|
|
125
|
+
type="hidden"
|
|
126
|
+
{name}
|
|
127
|
+
{disabled}
|
|
128
|
+
bind:value
|
|
129
|
+
/>
|
|
115
130
|
<input
|
|
116
131
|
class="
|
|
117
|
-
|
|
118
|
-
{isNegativeAllowed && !isZero && !isNegative
|
|
119
|
-
|
|
120
|
-
|
|
132
|
+
{inputClasses?.formatted ?? DEFAULT_CLASS_FORMATTED}
|
|
133
|
+
{isNegativeAllowed && !isZero && !isNegative
|
|
134
|
+
? inputClasses?.formattedPositive ?? DEFAULT_CLASS_FORMATTED_POSITIVE
|
|
135
|
+
: ''}
|
|
136
|
+
{isZero ? inputClasses?.formattedZero ?? DEFAULT_CLASS_FORMATTED_ZERO : ''}
|
|
137
|
+
{isNegativeAllowed && isNegative
|
|
138
|
+
? inputClasses?.formattedNegative ?? DEFAULT_CLASS_FORMATTED_NEGATIVE
|
|
139
|
+
: ''}
|
|
121
140
|
"
|
|
122
141
|
type="text"
|
|
123
142
|
inputmode="numeric"
|
|
@@ -10,6 +10,15 @@ declare const __propDef: {
|
|
|
10
10
|
placeholder?: number | null | undefined;
|
|
11
11
|
isNegativeAllowed?: boolean | undefined;
|
|
12
12
|
fractionDigits?: number | undefined;
|
|
13
|
+
inputClasses?: {
|
|
14
|
+
wrapper?: string | undefined;
|
|
15
|
+
unformatted?: string | undefined;
|
|
16
|
+
formatted?: string | undefined;
|
|
17
|
+
formattedPositive?: string | undefined;
|
|
18
|
+
formattedNegative?: string | undefined;
|
|
19
|
+
formattedZero?: string | undefined;
|
|
20
|
+
} | null | undefined;
|
|
21
|
+
onValueChange?: ((value: number) => any) | undefined;
|
|
13
22
|
};
|
|
14
23
|
events: {
|
|
15
24
|
[evt: string]: CustomEvent<any>;
|
package/README.md
CHANGED
|
@@ -15,7 +15,7 @@ A form input that converts numbers to localized currency formats as you type
|
|
|
15
15
|
- Formats **positive** and **negative** values
|
|
16
16
|
- Leverages [`Intl.NumberFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat) for **localizing** currency denominations and masking the input
|
|
17
17
|
- Simple [API](#api)
|
|
18
|
-
- Minimal
|
|
18
|
+
- Minimal default styling, easy to [customize](#styling)
|
|
19
19
|
|
|
20
20
|
## Usage
|
|
21
21
|
|
|
@@ -69,12 +69,43 @@ This is more or less what `<CurrencyInput />` looks like under the hood:
|
|
|
69
69
|
| placeholder | `number` `null` | `0` | Overrides the default placeholder. Setting the value to a `number` will display it as formatted. Setting it to `null` will not show a placeholder |
|
|
70
70
|
| isNegativeAllowed | `boolean` | `true` | If `false`, forces formatting only to positive values and ignores `--positive` and `--negative` styling modifiers |
|
|
71
71
|
| fractionDigits | `number` | `2` | Sets `maximumFractionDigits` in [`Intl.NumberFormat()` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#minimumfractiondigits) used for formatting the currency. Supported digits: `0` to `20` |
|
|
72
|
+
| inputClasses | `object` | [See below](#Styling) | Selectively overrides any class names passed |
|
|
72
73
|
|
|
73
74
|
## Styling
|
|
74
75
|
|
|
75
|
-
|
|
76
|
+
There are two ways of customizing the styling of the input:
|
|
77
|
+
1. Passing it your own CSS classes
|
|
78
|
+
2. Overriding the styles using the existing class names
|
|
79
|
+
|
|
80
|
+
You can **override all of the class names** by passing an object to `inputClasses` that has **one or more** of these properties:
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
interface InputClasses {
|
|
84
|
+
wrapper?: string; // <div> that contains the two <input> elements
|
|
85
|
+
unformatted?: string; // <input type="hidden"> that contains the unformatted value
|
|
86
|
+
formatted?: string; // <input type="text"> that contains the formatted value
|
|
87
|
+
formattedPositive?: string; // Class added when the formatted input is positive
|
|
88
|
+
formattedNegative?: string; // Class added when the formatted input is negative
|
|
89
|
+
formattedZero?: string; // Class added when the formatted input is zero
|
|
90
|
+
}
|
|
91
|
+
```
|
|
76
92
|
|
|
77
|
-
|
|
93
|
+
Usage (with [Tailwind CSS](https://tailwindcss.com/) as an example):
|
|
94
|
+
|
|
95
|
+
```svelte
|
|
96
|
+
<CurrencyInput name="total" value="{420.69}" inputClasses={
|
|
97
|
+
{
|
|
98
|
+
wrapper: "form-control block",
|
|
99
|
+
formatted: 'py-1.5 text-gray-700',
|
|
100
|
+
formattedPositive: 'text-green-700',
|
|
101
|
+
formattedNegative: 'text-red-700'
|
|
102
|
+
}
|
|
103
|
+
} />
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Alternatively you can **write your own CSS** by overriding the [default styles](https://github.com/canutin/svelte-currency-input/blob/main/src/lib/CurrencyInput.svelte) which use [BEM naming conventions](https://getbem.com/naming/). To do so apply your styles as shown below:
|
|
107
|
+
|
|
108
|
+
```svelte
|
|
78
109
|
<div class="my-currency-input">
|
|
79
110
|
<CurrencyInput name="total" value="{420.69}" />
|
|
80
111
|
</div>
|