@canutin/svelte-currency-input 0.2.3 → 0.3.1
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/.eslintignore +13 -0
- package/.eslintrc.cjs +20 -0
- package/.gitattributes +2 -0
- package/.github/workflows/release.yml +28 -0
- package/.github/workflows/tests.yml +23 -0
- package/.prettierignore +14 -0
- package/.prettierrc +8 -0
- package/README.md +1 -3
- package/{CurrencyInput.svelte → package/CurrencyInput.svelte} +0 -0
- package/{CurrencyInput.svelte.d.ts → package/CurrencyInput.svelte.d.ts} +0 -0
- package/package/LICENSE +21 -0
- package/package/README.md +118 -0
- package/{index.d.ts → package/index.d.ts} +0 -0
- package/{index.js → package/index.js} +0 -0
- package/package/package.json +51 -0
- package/package.json +56 -49
- package/playwright.config.ts +33 -0
- package/src/app.d.ts +11 -0
- package/src/app.html +12 -0
- package/src/lib/CurrencyInput.svelte +131 -0
- package/src/lib/index.ts +2 -0
- package/src/routes/+page.svelte +155 -0
- package/static/favicon.png +0 -0
- package/svelte.config.js +15 -0
- package/tests/svelte-currency-input.test.ts +190 -0
- package/tsconfig.json +17 -0
- package/vite.config.ts +8 -0
package/.eslintignore
ADDED
package/.eslintrc.cjs
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
root: true,
|
|
3
|
+
parser: '@typescript-eslint/parser',
|
|
4
|
+
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended', 'prettier'],
|
|
5
|
+
plugins: ['svelte3', '@typescript-eslint'],
|
|
6
|
+
ignorePatterns: ['*.cjs'],
|
|
7
|
+
overrides: [{ files: ['*.svelte'], processor: 'svelte3/svelte3' }],
|
|
8
|
+
settings: {
|
|
9
|
+
'svelte3/typescript': () => require('typescript')
|
|
10
|
+
},
|
|
11
|
+
parserOptions: {
|
|
12
|
+
sourceType: 'module',
|
|
13
|
+
ecmaVersion: 2020
|
|
14
|
+
},
|
|
15
|
+
env: {
|
|
16
|
+
browser: true,
|
|
17
|
+
es2017: true,
|
|
18
|
+
node: true
|
|
19
|
+
}
|
|
20
|
+
};
|
package/.gitattributes
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
name: Test, build & release
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main, master]
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
Publish:
|
|
9
|
+
name: Release
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
steps:
|
|
12
|
+
- uses: actions/checkout@v2
|
|
13
|
+
- uses: actions/setup-node@v2
|
|
14
|
+
with:
|
|
15
|
+
node-version: '16.x'
|
|
16
|
+
- name: Install dependencies
|
|
17
|
+
run: npm ci
|
|
18
|
+
- name: Install Playwright Browsers
|
|
19
|
+
run: npx playwright install --with-deps
|
|
20
|
+
- name: Run Playwright tests
|
|
21
|
+
run: npm test
|
|
22
|
+
env:
|
|
23
|
+
NODE_ENV: CI
|
|
24
|
+
- name: Run semantic release
|
|
25
|
+
run: npx semantic-release --branches main
|
|
26
|
+
env:
|
|
27
|
+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
28
|
+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
name: Integration tests
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
pull_request:
|
|
5
|
+
branches: [main, master]
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
Playwright:
|
|
9
|
+
timeout-minutes: 60
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
steps:
|
|
12
|
+
- uses: actions/checkout@v2
|
|
13
|
+
- uses: actions/setup-node@v2
|
|
14
|
+
with:
|
|
15
|
+
node-version: '16.x'
|
|
16
|
+
- name: Install dependencies
|
|
17
|
+
run: npm ci
|
|
18
|
+
- name: Install Playwright Browsers
|
|
19
|
+
run: npx playwright install --with-deps
|
|
20
|
+
- name: Run Playwright tests
|
|
21
|
+
run: npm test
|
|
22
|
+
env:
|
|
23
|
+
NODE_ENV: CI
|
package/.prettierignore
ADDED
package/.prettierrc
ADDED
package/README.md
CHANGED
|
@@ -5,9 +5,7 @@ A form input that converts numbers to localized currency formats as you type
|
|
|
5
5
|
[<img width="1059" alt="image" src="https://user-images.githubusercontent.com/1434675/190315136-c1d310ab-0ef1-441d-a80c-2b3727d74f59.png">](https://svelte.dev/repl/d8f7d22e5b384555b430f62b157ac503?version=3.50.1)
|
|
6
6
|
|
|
7
7
|
<p align="center">
|
|
8
|
-
<a href="https://svelte.dev/repl/d8f7d22e5b384555b430f62b157ac503?version=3.50.1" target="_blank">
|
|
9
|
-
Play with REPL
|
|
10
|
-
</a>
|
|
8
|
+
👩💻 Play with it on <a href="https://svelte.dev/repl/d8f7d22e5b384555b430f62b157ac503?version=3.50.1" target="_blank">REPL</a> — 💵 See it in a <a href="https://github.com/Canutin/desktop/blob/master/sveltekit/src/lib/components/FormCurrency.svelte" target="_blank">real project</a>!
|
|
11
9
|
</p>
|
|
12
10
|
|
|
13
11
|
---
|
|
File without changes
|
|
File without changes
|
package/package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Canutin
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# svelte-currency-input
|
|
2
|
+
|
|
3
|
+
A form input that converts numbers to localized currency formats as you type
|
|
4
|
+
|
|
5
|
+
[<img width="1059" alt="image" src="https://user-images.githubusercontent.com/1434675/190315136-c1d310ab-0ef1-441d-a80c-2b3727d74f59.png">](https://svelte.dev/repl/d8f7d22e5b384555b430f62b157ac503?version=3.50.1)
|
|
6
|
+
|
|
7
|
+
<p align="center">
|
|
8
|
+
👩💻 Play with it on <a href="https://svelte.dev/repl/d8f7d22e5b384555b430f62b157ac503?version=3.50.1" target="_blank">REPL</a> — 💵 See it in a <a href="https://github.com/Canutin/desktop/blob/master/sveltekit/src/lib/components/FormCurrency.svelte" target="_blank">real project</a>!
|
|
9
|
+
</p>
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## Features
|
|
14
|
+
|
|
15
|
+
- Formats **positive** and **negative** values
|
|
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
|
+
- 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)
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install svelte-currency-input --save
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
```html
|
|
27
|
+
<script lang="ts">
|
|
28
|
+
import CurrencyInput from '@canutin/svelte-currency-input';
|
|
29
|
+
</script>
|
|
30
|
+
|
|
31
|
+
<CurrencyInput name="total" value={-420.69} locale="nl-NL" currency="EUR" />
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## How it works
|
|
35
|
+
|
|
36
|
+
When the form is submitted you get _unformatted_ or _formatted_ values from two `<input />`'s.
|
|
37
|
+
This is more or less what `<CurrencyInput />` looks like under the hood:
|
|
38
|
+
|
|
39
|
+
```html
|
|
40
|
+
<div class="currencyInput">
|
|
41
|
+
<!-- Unformatted value -->
|
|
42
|
+
<input
|
|
43
|
+
class="currencyInput__unformatted"
|
|
44
|
+
type="hidden"
|
|
45
|
+
name="total"
|
|
46
|
+
value="-420.69"
|
|
47
|
+
/>
|
|
48
|
+
|
|
49
|
+
<!-- Formatted value -->
|
|
50
|
+
<input
|
|
51
|
+
class="currencyInput__formatted"
|
|
52
|
+
type="text"
|
|
53
|
+
name="formatted-total"
|
|
54
|
+
value="€ -420,69"
|
|
55
|
+
/>
|
|
56
|
+
</div>
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## API
|
|
60
|
+
|
|
61
|
+
| Option | Type | Default | Description |
|
|
62
|
+
| ----------------- | --------- | ----------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
63
|
+
| value | `number` | `undefined` | Initial value. If left `undefined` a formatted value of `0` is visible as a placeholder |
|
|
64
|
+
| locale | `string` | `en-US` | Overrides default locale. [Examples](https://gist.github.com/ncreated/9934896) |
|
|
65
|
+
| currency | `string` | `USD` | Overrides default currency. [Examples](https://github.com/datasets/currency-codes/blob/master/data/codes-all.csv) |
|
|
66
|
+
| name | `string` | `total` | Applies the name to the [input fields](#how-it-works) for _unformatted_ (e.g `[name=total]`) and _formatted_ (e.g. `[name=formatted-total]`) values |
|
|
67
|
+
| required | `boolean` | `false` | Marks the inputs as required |
|
|
68
|
+
| disabled | `boolean` | `false` | Marks the inputs as disabled |
|
|
69
|
+
| isNegativeAllowed | `boolean` | `true` | If `false`, forces formatting only to positive values and ignores `--positive` and `--negative` styling modifiers |
|
|
70
|
+
|
|
71
|
+
## Styling
|
|
72
|
+
|
|
73
|
+
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:
|
|
74
|
+
|
|
75
|
+
```html
|
|
76
|
+
<div class="my-currency-input">
|
|
77
|
+
<CurrencyInput name="total" value="{420.69}" />
|
|
78
|
+
</div>
|
|
79
|
+
|
|
80
|
+
<style>
|
|
81
|
+
/* Container */
|
|
82
|
+
div.my-currency-input :global(div.currencyInput) { /* ... */ }
|
|
83
|
+
|
|
84
|
+
/* Formatted input */
|
|
85
|
+
div.my-currency-input :global(input.currencyInput__formatted) { /* ... */ }
|
|
86
|
+
|
|
87
|
+
/* Formatted input when the it's disabled */
|
|
88
|
+
div.my-currency-input :global(input.currencyInput__formatted:disabled) { /* ... */ }
|
|
89
|
+
|
|
90
|
+
/* Formatted input when the value is zero */
|
|
91
|
+
div.my-currency-input :global(input.currencyInput__formatted--zero) { /* ... */ }
|
|
92
|
+
|
|
93
|
+
/* Formatted input when the value is positive */
|
|
94
|
+
div.my-currency-input :global(input.currencyInput__formatted--positive) { /* ... */ }
|
|
95
|
+
|
|
96
|
+
/* Formatted input when the value is negative */
|
|
97
|
+
div.my-currency-input :global(input.currencyInput__formatted--negative) { /* ... */ }
|
|
98
|
+
</style>
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Contributing
|
|
102
|
+
|
|
103
|
+
Here's ways in which you can contribute:
|
|
104
|
+
|
|
105
|
+
- Found a bug? Open a [new issue](https://github.com/canutin/svelte-currency-input/issues/new)
|
|
106
|
+
- Browse our [existing issues](https://github.com/canutin/svelte-currency-input/issues)
|
|
107
|
+
- Submit a [pull request](https://github.com/canutin/svelte-currency-input/pulls)
|
|
108
|
+
|
|
109
|
+
## Developing
|
|
110
|
+
|
|
111
|
+
This package was generated with [SvelteKit](https://kit.svelte.dev/). Install dependencies with `npm install`, then start a development server:
|
|
112
|
+
|
|
113
|
+
```bash
|
|
114
|
+
npm run dev
|
|
115
|
+
|
|
116
|
+
# or start the server and open the app in a new browser tab
|
|
117
|
+
npm run dev -- --open
|
|
118
|
+
```
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@canutin/svelte-currency-input",
|
|
3
|
+
"version": "0.0.0-development",
|
|
4
|
+
"exports": {
|
|
5
|
+
"./package.json": "./package.json",
|
|
6
|
+
".": "./index.js",
|
|
7
|
+
"./CurrencyInput.svelte": "./CurrencyInput.svelte"
|
|
8
|
+
},
|
|
9
|
+
"devDependencies": {
|
|
10
|
+
"@playwright/test": "^1.25.0",
|
|
11
|
+
"@sveltejs/adapter-auto": "next",
|
|
12
|
+
"@sveltejs/kit": "next",
|
|
13
|
+
"@sveltejs/package": "next",
|
|
14
|
+
"@typescript-eslint/eslint-plugin": "^5.27.0",
|
|
15
|
+
"@typescript-eslint/parser": "^5.27.0",
|
|
16
|
+
"eslint": "^8.16.0",
|
|
17
|
+
"eslint-config-prettier": "^8.3.0",
|
|
18
|
+
"eslint-plugin-svelte3": "^4.0.0",
|
|
19
|
+
"prettier": "^2.6.2",
|
|
20
|
+
"prettier-plugin-svelte": "^2.7.0",
|
|
21
|
+
"semantic-release": "^19.0.5",
|
|
22
|
+
"svelte": "^3.44.0",
|
|
23
|
+
"svelte-check": "^2.7.1",
|
|
24
|
+
"svelte-preprocess": "^4.10.6",
|
|
25
|
+
"tslib": "^2.3.1",
|
|
26
|
+
"typescript": "^4.7.4",
|
|
27
|
+
"vite": "^3.1.0"
|
|
28
|
+
},
|
|
29
|
+
"type": "module",
|
|
30
|
+
"description": "A form input that converts numbers to currencies as you type in localized formats",
|
|
31
|
+
"keywords": [
|
|
32
|
+
"svelte",
|
|
33
|
+
"currency",
|
|
34
|
+
"money",
|
|
35
|
+
"input",
|
|
36
|
+
"i18n",
|
|
37
|
+
"positive",
|
|
38
|
+
"negative"
|
|
39
|
+
],
|
|
40
|
+
"repository": {
|
|
41
|
+
"type": "git",
|
|
42
|
+
"url": "git+https://github.com/canutin/svelte-currency-input.git"
|
|
43
|
+
},
|
|
44
|
+
"author": "Fernando Maclen <hello@fernando.is>",
|
|
45
|
+
"license": "MIT",
|
|
46
|
+
"bugs": {
|
|
47
|
+
"url": "https://github.com/canutin/svelte-currency-input/issues"
|
|
48
|
+
},
|
|
49
|
+
"homepage": "https://github.com/canutin/svelte-currency-input#readme",
|
|
50
|
+
"svelte": "./index.js"
|
|
51
|
+
}
|
package/package.json
CHANGED
|
@@ -1,50 +1,57 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
2
|
+
"name": "@canutin/svelte-currency-input",
|
|
3
|
+
"version": "0.3.1",
|
|
4
|
+
"exports": {
|
|
5
|
+
".": "./index.js"
|
|
6
|
+
},
|
|
7
|
+
"scripts": {
|
|
8
|
+
"dev": "vite dev",
|
|
9
|
+
"build": "svelte-kit sync && svelte-package",
|
|
10
|
+
"test": "playwright test",
|
|
11
|
+
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
|
|
12
|
+
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
|
|
13
|
+
"lint": "prettier --check . && eslint .",
|
|
14
|
+
"format": "prettier --write ."
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@playwright/test": "^1.25.0",
|
|
18
|
+
"@sveltejs/adapter-auto": "next",
|
|
19
|
+
"@sveltejs/kit": "next",
|
|
20
|
+
"@sveltejs/package": "next",
|
|
21
|
+
"@typescript-eslint/eslint-plugin": "^5.27.0",
|
|
22
|
+
"@typescript-eslint/parser": "^5.27.0",
|
|
23
|
+
"eslint": "^8.16.0",
|
|
24
|
+
"eslint-config-prettier": "^8.3.0",
|
|
25
|
+
"eslint-plugin-svelte3": "^4.0.0",
|
|
26
|
+
"prettier": "^2.6.2",
|
|
27
|
+
"prettier-plugin-svelte": "^2.7.0",
|
|
28
|
+
"semantic-release": "^19.0.5",
|
|
29
|
+
"svelte": "^3.44.0",
|
|
30
|
+
"svelte-check": "^2.7.1",
|
|
31
|
+
"svelte-preprocess": "^4.10.6",
|
|
32
|
+
"tslib": "^2.3.1",
|
|
33
|
+
"typescript": "^4.7.4",
|
|
34
|
+
"vite": "^3.1.0"
|
|
35
|
+
},
|
|
36
|
+
"type": "module",
|
|
37
|
+
"description": "A form input that converts numbers to currencies as you type in localized formats",
|
|
38
|
+
"keywords": [
|
|
39
|
+
"svelte",
|
|
40
|
+
"currency",
|
|
41
|
+
"money",
|
|
42
|
+
"input",
|
|
43
|
+
"i18n",
|
|
44
|
+
"positive",
|
|
45
|
+
"negative"
|
|
46
|
+
],
|
|
47
|
+
"repository": {
|
|
48
|
+
"type": "git",
|
|
49
|
+
"url": "git+https://github.com/canutin/svelte-currency-input.git"
|
|
50
|
+
},
|
|
51
|
+
"author": "Fernando Maclen <hello@fernando.is>",
|
|
52
|
+
"license": "MIT",
|
|
53
|
+
"bugs": {
|
|
54
|
+
"url": "https://github.com/canutin/svelte-currency-input/issues"
|
|
55
|
+
},
|
|
56
|
+
"homepage": "https://github.com/canutin/svelte-currency-input#readme"
|
|
57
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { type PlaywrightTestConfig, devices } from '@playwright/test';
|
|
2
|
+
|
|
3
|
+
const isEnvCI = process.env.NODE_ENV === 'CI';
|
|
4
|
+
|
|
5
|
+
const enableMultipleBrowsers = [
|
|
6
|
+
{
|
|
7
|
+
name: 'chromium',
|
|
8
|
+
use: { ...devices['Desktop Chrome'] }
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
name: 'firefox',
|
|
12
|
+
use: { ...devices['Desktop Firefox'] }
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
name: 'webkit',
|
|
16
|
+
use: { ...devices['Desktop Safari'] }
|
|
17
|
+
}
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
const config: PlaywrightTestConfig = {
|
|
21
|
+
webServer: {
|
|
22
|
+
command: 'npm run dev',
|
|
23
|
+
port: 5173
|
|
24
|
+
},
|
|
25
|
+
retries: isEnvCI ? 3 : 0,
|
|
26
|
+
use: {
|
|
27
|
+
trace: isEnvCI ? 'off' : 'retain-on-failure',
|
|
28
|
+
screenshot: isEnvCI ? 'off' : 'only-on-failure'
|
|
29
|
+
},
|
|
30
|
+
projects: isEnvCI ? enableMultipleBrowsers : undefined
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export default config;
|
package/src/app.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/// <reference types="@sveltejs/kit" />
|
|
2
|
+
|
|
3
|
+
// See https://kit.svelte.dev/docs/types#app
|
|
4
|
+
// for information about these interfaces
|
|
5
|
+
// and what to do when importing types
|
|
6
|
+
declare namespace App {
|
|
7
|
+
// interface Locals {}
|
|
8
|
+
// interface PageData {}
|
|
9
|
+
// interface PageError {}
|
|
10
|
+
// interface Platform {}
|
|
11
|
+
}
|
package/src/app.html
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8" />
|
|
5
|
+
<link rel="icon" href="%sveltekit.assets%/favicon.png" />
|
|
6
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
7
|
+
%sveltekit.head%
|
|
8
|
+
</head>
|
|
9
|
+
<body>
|
|
10
|
+
<div>%sveltekit.body%</div>
|
|
11
|
+
</body>
|
|
12
|
+
</html>
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
const DEFAULT_LOCALE = 'en-US';
|
|
3
|
+
const DEFAULT_CURRENCY = 'USD';
|
|
4
|
+
const DEFAULT_NAME = 'total';
|
|
5
|
+
const DEFAULT_VALUE = 0;
|
|
6
|
+
|
|
7
|
+
export let value: number = DEFAULT_VALUE;
|
|
8
|
+
export let locale: string = DEFAULT_LOCALE;
|
|
9
|
+
export let currency: string = DEFAULT_CURRENCY;
|
|
10
|
+
export let name: string = DEFAULT_NAME;
|
|
11
|
+
export let required: boolean = false;
|
|
12
|
+
export let disabled: boolean = false;
|
|
13
|
+
export let isNegativeAllowed: boolean = true;
|
|
14
|
+
|
|
15
|
+
// Formats value as: e.g. $1,523.00 | -$1,523.00
|
|
16
|
+
const formatCurrency = (
|
|
17
|
+
value: number,
|
|
18
|
+
maximumFractionDigits?: number,
|
|
19
|
+
minimumFractionDigits?: number
|
|
20
|
+
) => {
|
|
21
|
+
return new Intl.NumberFormat(locale, {
|
|
22
|
+
currency: currency,
|
|
23
|
+
style: 'currency',
|
|
24
|
+
maximumFractionDigits: maximumFractionDigits || 0,
|
|
25
|
+
minimumFractionDigits: minimumFractionDigits || 0
|
|
26
|
+
}).format(value);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
// Checks if the key pressed is allowed
|
|
30
|
+
const handleKeyDown = (event: KeyboardEvent) => {
|
|
31
|
+
const isDeletion = event.key === 'Backspace' || event.key === 'Delete';
|
|
32
|
+
const isModifier = event.metaKey || event.altKey || event.ctrlKey;
|
|
33
|
+
const isArrowKey = event.key === 'ArrowLeft' || event.key === 'ArrowRight';
|
|
34
|
+
const isInvalidCharacter = !/^\d|,|\.|-$/g.test(event.key); // Keys that are not a digit, comma, period or minus sign
|
|
35
|
+
|
|
36
|
+
if (!isDeletion && !isModifier && !isArrowKey && isInvalidCharacter) event.preventDefault();
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// Updates `value` by stripping away the currency formatting
|
|
40
|
+
const setUnformattedValue = (event: KeyboardEvent) => {
|
|
41
|
+
// Don't format if the user is typing a `currencyDecimal` point
|
|
42
|
+
const currencyDecimal = new Intl.NumberFormat(locale).format(1.1).charAt(1); // '.' or ','
|
|
43
|
+
if (event.key === currencyDecimal) return;
|
|
44
|
+
|
|
45
|
+
// If `formattedValue` is ['$', '-$', "-"] we don't need to continue
|
|
46
|
+
const currencySymbol = formatCurrency(0, 0)
|
|
47
|
+
.replace('0', '') // e.g. '$0' > '$'
|
|
48
|
+
.replace(/\u00A0/, ''); // e.g '0 €' > '€'
|
|
49
|
+
const ignoreSymbols = [currencySymbol, `-${currencySymbol}`, '-'];
|
|
50
|
+
const strippedUnformattedValue = formattedValue.replace(' ', '');
|
|
51
|
+
if (ignoreSymbols.includes(strippedUnformattedValue)) return;
|
|
52
|
+
|
|
53
|
+
// Remove all characters that arent: numbers, commas, periods (or minus signs if `isNegativeAllowed`)
|
|
54
|
+
let unformattedValue = isNegativeAllowed
|
|
55
|
+
? formattedValue.replace(/[^0-9,.-]/g, '')
|
|
56
|
+
: formattedValue.replace(/[^0-9,.]/g, '');
|
|
57
|
+
|
|
58
|
+
// Reverse the value when minus is pressed
|
|
59
|
+
if (isNegativeAllowed && event.key === '-') value = value * -1;
|
|
60
|
+
|
|
61
|
+
// Finally set the value
|
|
62
|
+
if (Number.isNaN(parseFloat(unformattedValue))) {
|
|
63
|
+
value = 0;
|
|
64
|
+
} else {
|
|
65
|
+
// The order of the following operations is *critical*
|
|
66
|
+
const isDecimalComma = currencyDecimal === ','; // Remove currency formatting from `formattedValue` so we can assign it to `value`
|
|
67
|
+
unformattedValue = unformattedValue.replace(isDecimalComma ? /\./g : /\,/g, ''); // Remove all group symbols
|
|
68
|
+
if (isDecimalComma) unformattedValue = unformattedValue.replace(',', '.'); // If the decimal point is a comma, replace it with a period
|
|
69
|
+
value = parseFloat(unformattedValue);
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const setFormattedValue = () => {
|
|
74
|
+
formattedValue = isZero ? '' : formatCurrency(value, 2, 0);
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
let formattedValue = '';
|
|
78
|
+
$: isZero = value === 0;
|
|
79
|
+
$: isNegative = value < 0;
|
|
80
|
+
$: value, setFormattedValue();
|
|
81
|
+
|
|
82
|
+
const placeholder = formatCurrency(DEFAULT_VALUE, 2, 2); // e.g. '$0.00'
|
|
83
|
+
</script>
|
|
84
|
+
|
|
85
|
+
<div class="currencyInput">
|
|
86
|
+
<input class="currencyInput__unformatted" type="hidden" {name} {disabled} bind:value />
|
|
87
|
+
<input
|
|
88
|
+
class="
|
|
89
|
+
currencyInput__formatted
|
|
90
|
+
{isNegativeAllowed && !isZero && !isNegative && 'currencyInput__formatted--positive'}
|
|
91
|
+
{isZero && 'currencyInput__formatted--zero'}
|
|
92
|
+
{isNegativeAllowed && isNegative && 'currencyInput__formatted--negative'}
|
|
93
|
+
"
|
|
94
|
+
type="text"
|
|
95
|
+
inputmode="numeric"
|
|
96
|
+
name={`formatted-${name}`}
|
|
97
|
+
required={required && !isZero}
|
|
98
|
+
{placeholder}
|
|
99
|
+
{disabled}
|
|
100
|
+
bind:value={formattedValue}
|
|
101
|
+
on:keydown={handleKeyDown}
|
|
102
|
+
on:keyup={setUnformattedValue}
|
|
103
|
+
/>
|
|
104
|
+
</div>
|
|
105
|
+
|
|
106
|
+
<style>
|
|
107
|
+
input.currencyInput__formatted {
|
|
108
|
+
border: 1px solid #e2e2e2;
|
|
109
|
+
padding: 10px;
|
|
110
|
+
box-sizing: border-box;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
input.currencyInput__formatted--zero {
|
|
114
|
+
color: #333;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
input.currencyInput__formatted--positive {
|
|
118
|
+
color: #00a36f;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
input.currencyInput__formatted--negative {
|
|
122
|
+
color: #e75258;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
input.currencyInput__formatted:disabled {
|
|
126
|
+
color: #999;
|
|
127
|
+
background-color: #e2e2e2;
|
|
128
|
+
pointer-events: none;
|
|
129
|
+
cursor: default;
|
|
130
|
+
}
|
|
131
|
+
</style>
|
package/src/lib/index.ts
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import CurrencyInput from '$lib/CurrencyInput.svelte';
|
|
3
|
+
|
|
4
|
+
const locale = 'nl-NL';
|
|
5
|
+
const currency = 'EUR';
|
|
6
|
+
|
|
7
|
+
let output: string;
|
|
8
|
+
const handleSubmit = (event: Event) => {
|
|
9
|
+
event.preventDefault();
|
|
10
|
+
// Get the form data
|
|
11
|
+
const data = new FormData(event.target as HTMLFormElement);
|
|
12
|
+
// Pretty-print the data as JSON
|
|
13
|
+
output = JSON.stringify(Object.fromEntries(data.entries()), null, 2);
|
|
14
|
+
};
|
|
15
|
+
</script>
|
|
16
|
+
|
|
17
|
+
<form class="demoForm" on:submit={handleSubmit}>
|
|
18
|
+
<div class="demoForm__container">
|
|
19
|
+
<CurrencyInput name="total" value={-42069.69} />
|
|
20
|
+
<CurrencyInput name="rent" />
|
|
21
|
+
<CurrencyInput name="cashflow" value={5678.9} />
|
|
22
|
+
<CurrencyInput name="balance" value={1234.56} isNegativeAllowed={false} />
|
|
23
|
+
|
|
24
|
+
<CurrencyInput name="amount" value={5678.9} {locale} {currency} />
|
|
25
|
+
<CurrencyInput name="deficit" value={1234.56} isNegativeAllowed={false} {locale} {currency} />
|
|
26
|
+
<CurrencyInput name="loss" value={97532.95} disabled={true} {locale} {currency} />
|
|
27
|
+
<CurrencyInput name="cost" value={-42069.69} {locale} {currency} />
|
|
28
|
+
</div>
|
|
29
|
+
|
|
30
|
+
<nav class="demoForm__output">
|
|
31
|
+
<button type="submit" class="demoForm__submit">Submit form</button>
|
|
32
|
+
|
|
33
|
+
<pre class="demoForm__pre {!output && 'demoForm__pre--placeholder'}">{output
|
|
34
|
+
? output
|
|
35
|
+
: 'Submit form to see a JSON output of the values'}</pre>
|
|
36
|
+
</nav>
|
|
37
|
+
|
|
38
|
+
<nav class="demoForm__nav">
|
|
39
|
+
<a class="demoForm__a" href="https://github.com/canutin/svelte-currency-input" target="_blank"
|
|
40
|
+
>GitHub repository</a
|
|
41
|
+
>
|
|
42
|
+
<a
|
|
43
|
+
class="demoForm__a"
|
|
44
|
+
href="https://github.com/canutin/svelte-currency-input/issues"
|
|
45
|
+
target="_blank">Known issues</a
|
|
46
|
+
>
|
|
47
|
+
<a
|
|
48
|
+
class="demoForm__a"
|
|
49
|
+
href="https://github.com/canutin/svelte-currency-input#contributing"
|
|
50
|
+
target="_blank">Contribute</a
|
|
51
|
+
>
|
|
52
|
+
<a
|
|
53
|
+
class="demoForm__a"
|
|
54
|
+
href="https://www.npmjs.com/package/@canutin/svelte-currency-input"
|
|
55
|
+
target="_blank">NPM</a
|
|
56
|
+
>
|
|
57
|
+
</nav>
|
|
58
|
+
</form>
|
|
59
|
+
|
|
60
|
+
<style>
|
|
61
|
+
/* Overriding the styles of the <CurrencyInput /> component */
|
|
62
|
+
form.demoForm :global(input.currencyInput__formatted) {
|
|
63
|
+
width: 100%;
|
|
64
|
+
font-family: monospace;
|
|
65
|
+
font-size: 13px;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/* Styles for demo presentation (you can ignore these) */
|
|
69
|
+
:global(body) {
|
|
70
|
+
--gap: 64px;
|
|
71
|
+
|
|
72
|
+
font-family: sans-serif;
|
|
73
|
+
box-sizing: border-box;
|
|
74
|
+
height: 100vh;
|
|
75
|
+
margin: 0;
|
|
76
|
+
background-color: #eaeaea;
|
|
77
|
+
display: flex;
|
|
78
|
+
align-items: center;
|
|
79
|
+
justify-content: center;
|
|
80
|
+
place-items: center;
|
|
81
|
+
padding: var(--gap);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
form.demoForm {
|
|
85
|
+
display: flex;
|
|
86
|
+
flex-direction: column;
|
|
87
|
+
row-gap: var(--gap);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
div.demoForm__container {
|
|
91
|
+
display: grid;
|
|
92
|
+
grid-template-columns: repeat(4, 1fr);
|
|
93
|
+
align-items: center;
|
|
94
|
+
justify-content: center;
|
|
95
|
+
gap: calc(var(--gap) / 2);
|
|
96
|
+
height: max-content;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
nav.demoForm__nav {
|
|
100
|
+
font-size: 13px;
|
|
101
|
+
display: flex;
|
|
102
|
+
column-gap: 16px;
|
|
103
|
+
justify-content: center;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
a.demoForm__a {
|
|
107
|
+
color: #333;
|
|
108
|
+
text-decoration: none;
|
|
109
|
+
border-bottom-width: 1px;
|
|
110
|
+
border-bottom-color: #ccc;
|
|
111
|
+
border-bottom-style: solid;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
a.demoForm__a:visited {
|
|
115
|
+
color: #666;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
a.demoForm__a:hover {
|
|
119
|
+
color: #000;
|
|
120
|
+
border-bottom-color: transparent;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
nav.demoForm__output {
|
|
124
|
+
display: grid;
|
|
125
|
+
grid-template-columns: max-content auto;
|
|
126
|
+
column-gap: calc(var(--gap) / 2);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
pre.demoForm__pre {
|
|
130
|
+
background-color: #f4f4f4;
|
|
131
|
+
padding: 10px;
|
|
132
|
+
margin: 0;
|
|
133
|
+
color: #666;
|
|
134
|
+
box-sizing: border-box;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
pre.demoForm__pre--placeholder {
|
|
138
|
+
font-family: sans-serif;
|
|
139
|
+
font-size: 13px;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
button.demoForm__submit {
|
|
143
|
+
border: none;
|
|
144
|
+
background-color: #333;
|
|
145
|
+
color: #fff;
|
|
146
|
+
padding: 10px;
|
|
147
|
+
font-size: 13px;
|
|
148
|
+
cursor: pointer;
|
|
149
|
+
height: max-content;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
button.demoForm__submit:hover {
|
|
153
|
+
background-color: #000;
|
|
154
|
+
}
|
|
155
|
+
</style>
|
|
Binary file
|
package/svelte.config.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import adapter from '@sveltejs/adapter-auto';
|
|
2
|
+
import preprocess from 'svelte-preprocess';
|
|
3
|
+
|
|
4
|
+
/** @type {import('@sveltejs/kit').Config} */
|
|
5
|
+
const config = {
|
|
6
|
+
// Consult https://github.com/sveltejs/svelte-preprocess
|
|
7
|
+
// for more information about preprocessors
|
|
8
|
+
preprocess: preprocess(),
|
|
9
|
+
|
|
10
|
+
kit: {
|
|
11
|
+
adapter: adapter()
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export default config;
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { expect, test } from '@playwright/test';
|
|
2
|
+
|
|
3
|
+
test.describe('CurrencyInput', () => {
|
|
4
|
+
test('Default behavior is correct', async ({ page }) => {
|
|
5
|
+
await page.goto('/');
|
|
6
|
+
|
|
7
|
+
// Test field with "zero" value
|
|
8
|
+
const rentUnformattedInput = page.locator('.currencyInput__unformatted[name=rent]');
|
|
9
|
+
const rentFormattedInput = page.locator('.currencyInput__formatted[name="formatted-rent"]');
|
|
10
|
+
await expect(rentUnformattedInput).not.toBeDisabled();
|
|
11
|
+
await expect(rentUnformattedInput).toHaveAttribute('type', 'hidden');
|
|
12
|
+
await expect(rentUnformattedInput).toHaveValue('0');
|
|
13
|
+
await expect(rentFormattedInput).not.toBeDisabled();
|
|
14
|
+
await expect(rentFormattedInput).toHaveValue('');
|
|
15
|
+
await expect(rentFormattedInput).toHaveAttribute('type', 'text');
|
|
16
|
+
await expect(rentFormattedInput).toHaveAttribute('placeholder', '$0.00');
|
|
17
|
+
await expect(rentFormattedInput).not.toHaveClass(/currencyInput__formatted--positive/);
|
|
18
|
+
await expect(rentFormattedInput).not.toHaveClass(/currencyInput__formatted--negative/);
|
|
19
|
+
await expect(rentFormattedInput).toHaveClass(/currencyInput__formatted--zero/);
|
|
20
|
+
|
|
21
|
+
// Test field with "positive" value
|
|
22
|
+
const amountUnformattedInput = page.locator('.currencyInput__unformatted[name=amount]');
|
|
23
|
+
const amountFormattedInput = page.locator('.currencyInput__formatted[name="formatted-amount"]');
|
|
24
|
+
await expect(amountUnformattedInput).not.toBeDisabled();
|
|
25
|
+
await expect(amountUnformattedInput).toHaveAttribute('type', 'hidden');
|
|
26
|
+
await expect(amountUnformattedInput).toHaveValue('5678.9');
|
|
27
|
+
await expect(amountFormattedInput).not.toBeDisabled();
|
|
28
|
+
await expect(amountFormattedInput).toHaveValue('€ 5.678,9');
|
|
29
|
+
await expect(amountFormattedInput).toHaveAttribute('type', 'text');
|
|
30
|
+
await expect(amountFormattedInput).toHaveAttribute('placeholder', '€ 0,00');
|
|
31
|
+
await expect(amountFormattedInput).toHaveClass(/currencyInput__formatted--positive/);
|
|
32
|
+
await expect(amountFormattedInput).not.toHaveClass(/currencyInput__formatted--negative/);
|
|
33
|
+
await expect(amountFormattedInput).not.toHaveClass(/currencyInput__formatted--zero/);
|
|
34
|
+
|
|
35
|
+
// Test field with "negative" value
|
|
36
|
+
const totalUnformattedInput = page.locator('.currencyInput__unformatted[name=total]');
|
|
37
|
+
const totalFormattedInput = page.locator('.currencyInput__formatted[name="formatted-total"]');
|
|
38
|
+
await expect(amountUnformattedInput).not.toBeDisabled();
|
|
39
|
+
await expect(totalUnformattedInput).toHaveAttribute('type', 'hidden');
|
|
40
|
+
await expect(totalUnformattedInput).toHaveValue('-42069.69');
|
|
41
|
+
await expect(totalFormattedInput).not.toBeDisabled();
|
|
42
|
+
await expect(totalFormattedInput).toHaveValue('-$42,069.69');
|
|
43
|
+
await expect(totalFormattedInput).toHaveAttribute('type', 'text');
|
|
44
|
+
await expect(totalFormattedInput).toHaveAttribute('placeholder', '$0.00');
|
|
45
|
+
await expect(totalFormattedInput).not.toHaveClass(/currencyInput__formatted--positive/);
|
|
46
|
+
await expect(totalFormattedInput).toHaveClass(/currencyInput__formatted--negative/);
|
|
47
|
+
await expect(totalFormattedInput).not.toHaveClass(/currencyInput__formatted--zero/);
|
|
48
|
+
|
|
49
|
+
// Test field that is "disabled"
|
|
50
|
+
const lossUnformattedInput = page.locator('.currencyInput__unformatted[name=loss]');
|
|
51
|
+
const lossFormattedInput = page.locator('.currencyInput__formatted[name="formatted-loss"]');
|
|
52
|
+
await expect(lossUnformattedInput).toBeDisabled();
|
|
53
|
+
await expect(lossUnformattedInput).toHaveAttribute('type', 'hidden');
|
|
54
|
+
await expect(lossUnformattedInput).toHaveValue('97532.95');
|
|
55
|
+
await expect(lossFormattedInput).toHaveValue('€ 97.532,95');
|
|
56
|
+
await expect(lossFormattedInput).toBeDisabled();
|
|
57
|
+
await expect(lossFormattedInput).toHaveAttribute('type', 'text');
|
|
58
|
+
await expect(lossFormattedInput).toHaveAttribute('placeholder', '€ 0,00');
|
|
59
|
+
await expect(lossFormattedInput).toHaveClass(/currencyInput__formatted--positive/);
|
|
60
|
+
await expect(lossFormattedInput).not.toHaveClass(/currencyInput__formatted--negative/);
|
|
61
|
+
await expect(lossFormattedInput).not.toHaveClass(/currencyInput__formatted--zero/);
|
|
62
|
+
|
|
63
|
+
// Submitting a form returns the correct values
|
|
64
|
+
const demoSubmitForm = page.locator('button.demoForm__submit');
|
|
65
|
+
const demoOutput = page.locator('pre.demoForm__pre');
|
|
66
|
+
await demoSubmitForm.click();
|
|
67
|
+
expect(await demoOutput.textContent()).toMatch(
|
|
68
|
+
JSON.stringify(
|
|
69
|
+
{
|
|
70
|
+
total: '-42069.69',
|
|
71
|
+
'formatted-total': '-$42,069.69',
|
|
72
|
+
rent: '0',
|
|
73
|
+
'formatted-rent': '',
|
|
74
|
+
cashflow: '5678.9',
|
|
75
|
+
'formatted-cashflow': '$5,678.9',
|
|
76
|
+
balance: '1234.56',
|
|
77
|
+
'formatted-balance': '$1,234.56',
|
|
78
|
+
amount: '5678.9',
|
|
79
|
+
'formatted-amount': '€ 5.678,9',
|
|
80
|
+
deficit: '1234.56',
|
|
81
|
+
'formatted-deficit': '€ 1.234,56',
|
|
82
|
+
cost: '-42069.69',
|
|
83
|
+
'formatted-cost': '€ -42.069,69'
|
|
84
|
+
},
|
|
85
|
+
null,
|
|
86
|
+
2
|
|
87
|
+
)
|
|
88
|
+
);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test('Updating an input has the correct behavior', async ({ page }) => {
|
|
92
|
+
await page.goto('/');
|
|
93
|
+
|
|
94
|
+
const rentUnformattedInput = page.locator('.currencyInput__unformatted[name=rent]');
|
|
95
|
+
const rentFormattedInput = page.locator('.currencyInput__formatted[name="formatted-rent"]');
|
|
96
|
+
|
|
97
|
+
// Check the there is no value in the input
|
|
98
|
+
await expect(rentUnformattedInput).toHaveValue('0');
|
|
99
|
+
await expect(rentFormattedInput).toHaveValue('');
|
|
100
|
+
|
|
101
|
+
await rentFormattedInput.focus();
|
|
102
|
+
await page.keyboard.type('420.69');
|
|
103
|
+
await expect(rentFormattedInput).toHaveValue('$420.69');
|
|
104
|
+
await expect(rentUnformattedInput).toHaveValue('420.69');
|
|
105
|
+
await expect(rentFormattedInput).toHaveClass(/currencyInput__formatted--positive/);
|
|
106
|
+
await expect(rentFormattedInput).not.toHaveClass(/currencyInput__formatted--negative/);
|
|
107
|
+
await expect(rentFormattedInput).not.toHaveClass(/currencyInput__formatted--zero/);
|
|
108
|
+
|
|
109
|
+
// Use arrow keys to go back to the first character
|
|
110
|
+
for (let i = 0; i < '$420.69'.length; i++) await page.keyboard.press('ArrowLeft');
|
|
111
|
+
await page.keyboard.type('-');
|
|
112
|
+
await expect(rentFormattedInput).toHaveValue('-$420.69');
|
|
113
|
+
await expect(rentUnformattedInput).toHaveValue('-420.69');
|
|
114
|
+
await expect(rentFormattedInput).not.toHaveClass(/currencyInput__formatted--positive/);
|
|
115
|
+
await expect(rentFormattedInput).toHaveClass(/currencyInput__formatted--negative/);
|
|
116
|
+
await expect(rentFormattedInput).not.toHaveClass(/currencyInput__formatted--zero/);
|
|
117
|
+
|
|
118
|
+
// Use right arrow keys to position cusror at the end of the input
|
|
119
|
+
for (let i = 0; i < '$420.69'.length; i++) await page.keyboard.press('ArrowRight');
|
|
120
|
+
// Delete the number but keep the currency symbol and sign
|
|
121
|
+
for (let i = 1; i < '420.69'.length; i++) await page.keyboard.press('Backspace');
|
|
122
|
+
await expect(rentFormattedInput).toHaveValue('-$');
|
|
123
|
+
// FIXME: at this point the hidden value should be set to 0 but without formatting `rentFormattedInput`
|
|
124
|
+
await expect(rentUnformattedInput).toHaveValue('-4');
|
|
125
|
+
|
|
126
|
+
await page.keyboard.press('Backspace');
|
|
127
|
+
await expect(rentFormattedInput).toHaveValue('-');
|
|
128
|
+
// FIXME: at this point the hidden value should be set to 0 but without formatting `rentFormattedInput
|
|
129
|
+
await expect(rentUnformattedInput).toHaveValue('-4');
|
|
130
|
+
|
|
131
|
+
await page.keyboard.type('69.42');
|
|
132
|
+
await expect(rentFormattedInput).toHaveValue('-$69.42');
|
|
133
|
+
await expect(rentUnformattedInput).toHaveValue('-69.42');
|
|
134
|
+
|
|
135
|
+
for (let i = 0; i < '-$69.42'.length; i++) await page.keyboard.press('Backspace');
|
|
136
|
+
await expect(rentUnformattedInput).toHaveValue('0');
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
test("Incorrect characters can't be entered", async ({ page }) => {
|
|
140
|
+
await page.goto('/');
|
|
141
|
+
|
|
142
|
+
const isMacOs = process.platform === 'darwin';
|
|
143
|
+
const rentUnformattedInput = page.locator('.currencyInput__unformatted[name=rent]');
|
|
144
|
+
const rentFormattedInput = page.locator('.currencyInput__formatted[name="formatted-rent"]');
|
|
145
|
+
|
|
146
|
+
// Check the there is no value in the input
|
|
147
|
+
await expect(rentUnformattedInput).toHaveValue('0');
|
|
148
|
+
await expect(rentFormattedInput).toHaveValue('');
|
|
149
|
+
|
|
150
|
+
// Check typing letters doesn't do anything
|
|
151
|
+
await rentFormattedInput.focus();
|
|
152
|
+
await page.keyboard.type('abc');
|
|
153
|
+
await expect(rentUnformattedInput).toHaveValue('0');
|
|
154
|
+
await expect(rentFormattedInput).toHaveValue('');
|
|
155
|
+
|
|
156
|
+
// Check keyboard combinations don't do anything
|
|
157
|
+
await page.keyboard.press('Shift+A');
|
|
158
|
+
await expect(rentFormattedInput).toHaveValue('');
|
|
159
|
+
|
|
160
|
+
// Check keyboard shortcuts are allowed
|
|
161
|
+
await page.keyboard.type('420.69');
|
|
162
|
+
await expect(rentFormattedInput).toHaveValue('$420.69');
|
|
163
|
+
await expect(rentUnformattedInput).toHaveValue('420.69');
|
|
164
|
+
|
|
165
|
+
// Select all
|
|
166
|
+
isMacOs ? await page.keyboard.press('Meta+A') : await page.keyboard.press('Control+A');
|
|
167
|
+
|
|
168
|
+
// Check "Backspace" works
|
|
169
|
+
await page.keyboard.press('Backspace');
|
|
170
|
+
await expect(rentUnformattedInput).toHaveValue('0');
|
|
171
|
+
await expect(rentFormattedInput).toHaveValue('');
|
|
172
|
+
|
|
173
|
+
// Add data to the field again
|
|
174
|
+
await page.keyboard.type('-420.69');
|
|
175
|
+
await expect(rentFormattedInput).toHaveValue('-$420.69');
|
|
176
|
+
await expect(rentUnformattedInput).toHaveValue('-420.69');
|
|
177
|
+
|
|
178
|
+
// Select all
|
|
179
|
+
isMacOs ? await page.keyboard.press('Meta+A') : await page.keyboard.press('Control+A');
|
|
180
|
+
|
|
181
|
+
// Check "Delete" also works
|
|
182
|
+
await page.keyboard.press('Delete');
|
|
183
|
+
await expect(rentUnformattedInput).toHaveValue('0');
|
|
184
|
+
await expect(rentFormattedInput).toHaveValue('');
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
test.skip('Updating chained inputs have the correct behavior', async () => {
|
|
188
|
+
// TODO
|
|
189
|
+
});
|
|
190
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "./.svelte-kit/tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"allowJs": true,
|
|
5
|
+
"checkJs": true,
|
|
6
|
+
"esModuleInterop": true,
|
|
7
|
+
"forceConsistentCasingInFileNames": true,
|
|
8
|
+
"resolveJsonModule": true,
|
|
9
|
+
"skipLibCheck": true,
|
|
10
|
+
"sourceMap": true,
|
|
11
|
+
"strict": true
|
|
12
|
+
}
|
|
13
|
+
// Path aliases are handled by https://kit.svelte.dev/docs/configuration#alias
|
|
14
|
+
//
|
|
15
|
+
// If you want to overwrite includes/excludes, make sure to copy over the relevant includes/excludes
|
|
16
|
+
// from the referenced tsconfig.json - TypeScript does not merge them in
|
|
17
|
+
}
|