@canutin/svelte-currency-input 0.7.2 → 0.8.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.
@@ -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,7 @@ 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;
15
22
  // Formats value as: e.g. $1,523.00 | -$1,523.00
16
23
  const formatCurrency = (value, maximumFractionDigits, minimumFractionDigits) => {
17
24
  return new Intl.NumberFormat(locale, {
@@ -110,14 +117,24 @@ $: isNegative = value < 0;
110
117
  $: value, setFormattedValue();
111
118
  </script>
112
119
 
113
- <div class="currencyInput">
114
- <input class="currencyInput__unformatted" type="hidden" {name} {disabled} bind:value />
120
+ <div class={inputClasses?.wrapper ?? DEFAULT_CLASS_WRAPPER}>
121
+ <input
122
+ class={inputClasses?.unformatted ?? DEFAULT_CLASS_UNFORMATTED}
123
+ type="hidden"
124
+ {name}
125
+ {disabled}
126
+ bind:value
127
+ />
115
128
  <input
116
129
  class="
117
- currencyInput__formatted
118
- {isNegativeAllowed && !isZero && !isNegative && 'currencyInput__formatted--positive'}
119
- {isZero && 'currencyInput__formatted--zero'}
120
- {isNegativeAllowed && isNegative && 'currencyInput__formatted--negative'}
130
+ {inputClasses?.formatted ?? DEFAULT_CLASS_FORMATTED}
131
+ {isNegativeAllowed && !isZero && !isNegative
132
+ ? inputClasses?.formattedPositive ?? DEFAULT_CLASS_FORMATTED_POSITIVE
133
+ : ''}
134
+ {isZero ? inputClasses?.formattedZero ?? DEFAULT_CLASS_FORMATTED_ZERO : ''}
135
+ {isNegativeAllowed && isNegative
136
+ ? inputClasses?.formattedNegative ?? DEFAULT_CLASS_FORMATTED_NEGATIVE
137
+ : ''}
121
138
  "
122
139
  type="text"
123
140
  inputmode="numeric"
@@ -10,6 +10,14 @@ 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;
13
21
  };
14
22
  events: {
15
23
  [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 [default styling](https://github.com/canutin/svelte-currency-input/blob/main/src/lib/CurrencyInput.svelte#L88-L118), easy to [customize](#styling)
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
- The [default styles](https://github.com/canutin/svelte-currency-input/blob/main/src/lib/CurrencyInput.svelte#L88-L118) use [BEM naming conventions](https://getbem.com/naming/). To override the default styles apply your styles as shown below:
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
- ```html
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>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@canutin/svelte-currency-input",
3
- "version": "0.7.2",
3
+ "version": "0.8.0",
4
4
  "exports": {
5
5
  "./package.json": "./package.json",
6
6
  ".": "./index.js",