Skip to content

Commit

Permalink
feat: add autocomplete attribute (#51)
Browse files Browse the repository at this point in the history
* feat: add autocomplete attribute

Fixes #49

* docs: add link to MDN docs

* test: add autocomplete="off" assertion

* chore: update GH actions to v3

* chore: update dependencies

* chore: update Svelte deps

* Revert "chore: update Svelte deps"

This reverts commit e346e33.

* Revert "chore: update dependencies"

This reverts commit 328b1aa.

* test: all passing locally

* chore: cleanup syntax
  • Loading branch information
fmaclen authored Aug 26, 2023
1 parent 33cc93a commit 86e2b1a
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 40 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ jobs:
name: Publish
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '16.x'
- uses: actions/checkout@v3
- uses: actions/setup-node@v3

- name: Install dependencies
run: npm ci
- name: Build package with SvelteKit
Expand Down
7 changes: 3 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ jobs:
timeout-minutes: 60
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '16.x'
- uses: actions/checkout@v3
- uses: actions/setup-node@v3

- name: Install dependencies
run: npm ci
- name: Install Playwright Browsers
Expand Down
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,20 @@ This is more or less what `<CurrencyInput />` looks like under the hood:

## API

| Option | Type | Default | Description |
| ----------------- | --------------- | ----------- | ----------- |
| value | `number` | `undefined` | Initial value. If left `undefined` a formatted value of `0` is visible as a placeholder |
| locale | `string` | `en-US` | Overrides default locale. [Examples](https://gist.github.com/ncreated/9934896) |
| currency | `string` | `USD` | Overrides default currency. [Examples](https://www.xe.com/symbols/) |
| 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 |
| required | `boolean` | `false` | Marks the inputs as required |
| disabled | `boolean` | `false` | Marks the inputs as disabled |
| 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 |
| isNegativeAllowed | `boolean` | `true` | If `false`, forces formatting only to positive values and ignores `--positive` and `--negative` styling modifiers |
| 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` |
| inputClasses | `object` | [See below](#Styling) | Selectively overrides any class names passed |
| onValueChange | `Callback` | `undefined` | Runs a callback function after the value changes |
Option | Type | Default | Description |
----------------- | --------------- | ----------- | ----------- |
value | `number` | `undefined` | Initial value. If left `undefined` a formatted value of `0` is visible as a placeholder |
locale | `string` | `en-US` | Overrides default locale. [Examples](https://gist.github.com/ncreated/9934896) |
currency | `string` | `USD` | Overrides default currency. [Examples](https://www.xe.com/symbols/) |
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 |
required | `boolean` | `false` | Marks the inputs as required |
disabled | `boolean` | `false` | Marks the inputs as disabled |
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 |
autocomplete | `string` | `undefined` | Sets the autocomplete attribute. Accepts any valid HTML [autocomplete attribute values](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/autocomplete#values) |
isNegativeAllowed | `boolean` | `true` | If `false`, forces formatting only to positive values and ignores `--positive` and `--negative` styling modifiers |
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` |
inputClasses | `object` | [See below](#Styling) | Selectively overrides any class names passed |
onValueChange | `Callback` | `undefined` | Runs a callback function after the value changes |

## Styling

Expand Down
40 changes: 22 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"format": "prettier --write ."
},
"devDependencies": {
"@playwright/test": "^1.25.0",
"@playwright/test": "^1.37.1",
"@sveltejs/adapter-auto": "next",
"@sveltejs/kit": "next",
"@sveltejs/package": "next",
Expand Down
2 changes: 2 additions & 0 deletions src/lib/CurrencyInput.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
export let required: boolean = false;
export let disabled: boolean = false;
export let placeholder: number | null = DEFAULT_VALUE;
export let autocomplete: string | null | undefined = undefined;
export let isNegativeAllowed: boolean = true;
export let fractionDigits: number = DEFAULT_FRACTION_DIGITS;
export let inputClasses: InputClasses | null = null;
Expand Down Expand Up @@ -181,6 +182,7 @@
name={`formatted-${name}`}
required={required && !isZero}
placeholder={formattedPlaceholder}
{autocomplete}
{disabled}
bind:value={formattedValue}
on:keydown={handleKeyDown}
Expand Down
1 change: 1 addition & 0 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
value={unchangedValue}
isNegativeAllowed={false}
placeholder={null}
autocomplete="off"
locale="es-AR"
currency="ARS"
onValueChange={(value) => {
Expand Down
5 changes: 5 additions & 0 deletions tests/svelte-currency-input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,11 @@ test.describe('CurrencyInput', () => {
await page.keyboard.press('Backspace');
});

test('Autocomplete attribute can be set', async ({ page }) => {
const pesosFormattedInput = page.locator('.currencyInput__formatted[name="formatted-pesos"]');
await expect(pesosFormattedInput).toHaveAttribute('autocomplete', 'off');
});

test.skip('Updating chained inputs have the correct behavior', async () => {
// TODO
});
Expand Down

0 comments on commit 86e2b1a

Please sign in to comment.