Skip to content

Commit

Permalink
feat(): handle pasted strings
Browse files Browse the repository at this point in the history
  • Loading branch information
wandroll committed Mar 8, 2024
1 parent 3540730 commit 7d5cd55
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions ember-amount-input/src/components/amount-input.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
readonly={{@readonly}}
{{on 'keydown' this.onKeyDown}}
{{on 'input' this.onInput}}
{{on 'paste' this.onPaste}}
{{on 'focusout' this.onFocusOut}}
/>
</div>
12 changes: 12 additions & 0 deletions ember-amount-input/src/components/amount-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,18 @@ export default class AmountInput extends Component<AmountInputSignature> {
return true;
}

@action
onPaste(event: ClipboardEvent): boolean {
const pastedValue = event.clipboardData?.getData('text');
const parsedValue = parseFloat(pastedValue?.replace(/\s/g, '') ?? '');

if (!isNaN(parsedValue)) {
this.args.update(parsedValue.toFixed(this.numberOfDecimal));
}

return true;
}

@action
onFocusOut(event: FocusEvent): boolean {
if (event.target instanceof HTMLInputElement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { setupRenderingTest } from 'ember-qunit';
import { blur, fillIn, render, typeIn } from '@ember/test-helpers';
import { hbs } from 'ember-cli-htmlbars';
import type { TestContext as TestContextBase } from '@ember/test-helpers';
import { simulateUserPasteValue } from './helpers/paste-value';

interface TestContext extends TestContextBase {
value: number | string;
Expand Down Expand Up @@ -105,7 +106,7 @@ module('Integration | Component | amount-input', function (hooks) {
await render<TestContext>(hbs`
<AmountInput
@value={{this.value}}
@update={{fn (mut this.value)}}
@update={{fn (mut this.value)}}
@readonly={{true}}
/>
`);
Expand Down Expand Up @@ -185,4 +186,40 @@ module('Integration | Component | amount-input', function (hooks) {

assert.dom('input').hasValue('61');
});

module('when user pastes a value with spaces', function () {
module('and the value contains spaces', function () {
test('calls update with the formatted value on blur without the spaces', async function (assert) {
await render<TestContext>(hbs`
<AmountInput
@value={{this.value}}
@update={{fn (mut this.value)}}
/>
`);

await simulateUserPasteValue('input', '1 061,00');

assert.dom('input').hasValue('1061.00');

await simulateUserPasteValue('input', ' 1 061');

assert.dom('input').hasValue('1061.00');
});
});

module('and the value is not a valid amount', function () {
test('calls update with an empty string value', async function (assert) {
await render<TestContext>(hbs`
<AmountInput
@value={{this.value}}
@update={{fn (mut this.value)}}
/>
`);

await simulateUserPasteValue('input', 'foo');

assert.dom('input').hasValue('');
});
});
});
});

0 comments on commit 7d5cd55

Please sign in to comment.