Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: bic, iban & vat return uppercase automaticly #106

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module(
await fillIn('[data-test-tpk-bic-input]', '12121212');
assert.strictEqual(changeset.get('bic'), '');
await fillIn('[data-test-tpk-bic-input]', 'aaaaaaaa');
assert.strictEqual(changeset.get('bic'), '');
assert.strictEqual(changeset.get('bic'), 'AAAAAAAA');
await fillIn('[data-test-tpk-bic-input]', 'SEBISSEB');
assert.strictEqual(changeset.get('bic'), 'SEBISSEB');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { fillIn, render } from '@ember/test-helpers';
import { ImmerChangeset } from 'ember-immer-changeset';

import { setupIntl } from 'ember-intl/test-support';
import TpkValidationIban from '@triptyk/ember-input-validation/components/prefabs/tpk-validation-iban';
import { assertTpkCssClassesExist } from '../generic-test-functions/assert-tpk-css-classes-exist';
Expand Down Expand Up @@ -45,13 +44,13 @@ module(

test('it lets you type BE IBAN and nicely format it', async function (assert) {
const changeset = await renderComponentAndReturnChangeset();
await fillIn('[data-test-tpk-input-input]', 'BE68539007547034');
await fillIn('[data-test-tpk-input-input]', 'be68539007547034');
assert.strictEqual(changeset.get('iban'), 'BE68 5390 0754 7034');
});

test('it lets you type LU IBAN and nicely format it', async function (assert) {
const changeset = await renderComponentAndReturnChangeset();
await fillIn('[data-test-tpk-input-input]', 'LU120010001234567891');
await fillIn('[data-test-tpk-input-input]', 'lu120010001234567891');
assert.strictEqual(changeset.get('iban'), 'LU12 0010 0012 3456 7891');
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import { maskSpecialCharDefinition } from "../../utils/mask-utils.ts";
import MandatoryLabelComponent from "./mandatory-label.gts";
import Component from "@glimmer/component";
import TpkValidationErrorsComponent from "./tpk-validation-errors.gts";
import { action } from "@ember/object";

export interface TpkValidationBicPrefabSignature
extends BaseValidationSignature {
Args: Omit<TpkValidationInputComponentSignature['Args'], 'type' | 'min' | 'max' | 'step' | 'mask' | 'maskOptions' | 'unmaskValue' | 'mask'>;
Args: Omit<TpkValidationInputComponentSignature['Args'], 'type' | 'min' | 'max' | 'step' | 'mask' | 'maskOptions' | 'unmaskValue' | 'mask'> & {
onChange?: (value: string, e: Event) => void;
};
Blocks: {
default: [];
};
Expand All @@ -20,12 +23,21 @@ export default class TpkValidationBicPrefabComponent extends Component<TpkValida
definitions: maskSpecialCharDefinition,
};

@action
onChange(value: string | number | Date | null, e: Event){
const valueAsString = (value as string).toUpperCase()
if(this.args.onChange){
return this.args.onChange(valueAsString, e);
}
return this.args.changeset.set(this.args.validationField, valueAsString);
}

<template>
<TpkValidationInputComponent
@label={{@label}}
@disabled={{@disabled}}
@type="text"
@onChange={{@onChange}}
@onChange={{this.onChange}}
@mandatory={{@mandatory}}
@placeholder={{@placeholder}}
@validationField={{@validationField}}
Expand All @@ -46,7 +58,7 @@ export default class TpkValidationBicPrefabComponent extends Component<TpkValida
@label={{@label}}
@mandatory={{V.mandatory}}/>
<V.Input
class="tpk-bic-input"
class="tpk-bic-input uppercase"
Brenion marked this conversation as resolved.
Show resolved Hide resolved
data-test-tpk-bic-input
/>
<TpkValidationErrorsComponent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { maskSpecialCharDefinition, getMaskForPrefixOrDefault } from "../../util
import TpkValidationErrorsComponent from './tpk-validation-errors.gts';
import MandatoryLabelComponent from "./mandatory-label.gts";
import Component from "@glimmer/component";
import { action } from "@ember/object";

export interface TpkValidationIBANPrefabSignature
extends BaseValidationSignature {
Args: Omit<TpkValidationInputComponentSignature['Args'], 'type' | 'min' | 'max' | 'step' | 'mask' | 'maskOptions' | 'unmaskValue' | 'mask'> & {
mandatory?: boolean;
onChange?: (value: string, e: Event) => void;
};
Blocks: {
default: [];
Expand All @@ -18,27 +20,27 @@ export interface TpkValidationIBANPrefabSignature

export default class TpkValidationIBANPrefabComponent extends Component<TpkValidationIBANPrefabSignature> {
ibanMaskByCountry = [{
mask: 'BE&& &&&& &&&& &&&&',
mask: '$$&& &&&& &&&& &&&&',
startsWith: 'BE',
definitions: maskSpecialCharDefinition,
lazy: false,
},{
mask: 'FR&& &&&& &&&& &&$$ $$$$ $$$$ $&&',
mask: '$$&& &&&& &&&& &&$$ $$$$ $$$$ $&&',
startsWith: 'FR',
lazy: false,
definitions: maskSpecialCharDefinition,
},{
mask: 'LU&& &&&$ $$$$ $$$$ $$$$',
mask: '$$&& &&&$ $$$$ $$$$ $$$$',
startsWith: 'LU',
definitions: maskSpecialCharDefinition,
lazy: false,
},{
mask: 'NL&& #### &&&& &&&& &&',
mask: '$$&& #### &&&& &&&& &&',
startsWith: 'NL',
definitions: maskSpecialCharDefinition,
lazy: false,
},{
mask: 'DE&& &&&& &&&& &&&& &&&& &&',
mask: '$$&& &&&& &&&& &&&& &&&& &&',
definitions: maskSpecialCharDefinition,
startsWith: 'DE',
lazy: false,
Expand All @@ -58,12 +60,21 @@ export default class TpkValidationIBANPrefabComponent extends Component<TpkValid
return this.args.disabled? '' : this.ibanMaskByCountry;
}

@action
onChange(value: string | number | Date | null, e: Event){
const valueAsString = (value as string).toUpperCase()
if(this.args.onChange){
return this.args.onChange(valueAsString, e);
}
return this.args.changeset.set(this.args.validationField, valueAsString);
}


<template>
<TpkValidationInputComponent
@label={{@label}}
@type="text"
@onChange={{@onChange}}
@onChange={{this.onChange}}
@validationField={{@validationField}}
@changeEvent={{@changeEvent}}
@changeset={{@changeset}}
Expand All @@ -84,7 +95,8 @@ export default class TpkValidationIBANPrefabComponent extends Component<TpkValid
@label={{@label}}
@mandatory={{V.mandatory}} />
<V.Input
class="tpk-iban-input"
class="tpk-iban-input uppercase"

data-test-tpk-iban-input
/>
<TpkValidationErrorsComponent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import TpkValidationErrorsComponent from './tpk-validation-errors.gts';
import MandatoryLabelComponent from './mandatory-label.gts';
import Component from '@glimmer/component';
import { action } from '@ember/object';

export interface TpkValidationVATPrefabSignature
extends BaseValidationSignature {
Expand All @@ -24,6 +25,7 @@ export interface TpkValidationVATPrefabSignature
| 'mask'
> & {
mandatory?: boolean;
onChange?: (value: string, e: Event) => void;
};
Blocks: {
default: [];
Expand All @@ -34,31 +36,31 @@ export interface TpkValidationVATPrefabSignature
export default class TpkValidationVATPrefabComponent extends Component<TpkValidationVATPrefabSignature> {
ibanMaskByCountry = [
{
mask: 'BE&&&&&&&&&&',
mask: '$$&&&&&&&&&&',
startsWith: 'BE',
definitions: maskSpecialCharDefinition,
lazy: false,
},
{
mask: 'FR$$&&&&&&&&&',
mask: '$$$$&&&&&&&&&',
startsWith: 'FR',
lazy: false,
definitions: maskSpecialCharDefinition,
},
{
mask: 'LU&&&&&&&&',
mask: '$$&&&&&&&&',
startsWith: 'LU',
definitions: maskSpecialCharDefinition,
lazy: false,
},
{
mask: 'NL&&&&&&&&&B&&',
mask: '$$&&&&&&&&&B&&',
startsWith: 'NL',
definitions: maskSpecialCharDefinition,
lazy: false,
},
{
mask: 'DE&&&&&&&&&',
mask: '$$&&&&&&&&&',
definitions: maskSpecialCharDefinition,
startsWith: 'DE',
lazy: false,
Expand All @@ -75,11 +77,20 @@ export default class TpkValidationVATPrefabComponent extends Component<TpkValida
dispatch: getMaskForPrefixOrDefault,
};

@action
onChange(value: string | number | Date | null, e: Event){
const valueAsString = (value as string).toUpperCase()
if(this.args.onChange){
return this.args.onChange(valueAsString, e);
}
return this.args.changeset.set(this.args.validationField, valueAsString);
}

<template>
<TpkValidationInputComponent
@label={{@label}}
@type='text'
@onChange={{@onChange}}
@onChange={{this.onChange}}
@validationField={{@validationField}}
@changeEvent={{@changeEvent}}
@disabled={{@disabled}}
Expand All @@ -102,7 +113,7 @@ export default class TpkValidationVATPrefabComponent extends Component<TpkValida
@label={{@label}}
@mandatory={{V.mandatory}}
/>
<V.Input class='tpk-vat-input' data-test-tpk-vat-input />
<V.Input class='tpk-vat-input uppercase' data-test-tpk-vat-input />
<TpkValidationErrorsComponent
class='tpk-validation-errors'
@errors={{V.errors}}
Expand Down
7 changes: 3 additions & 4 deletions packages/ember-input-validation/src/utils/mask-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,20 @@ export interface DynamicMasked {
}

export const maskSpecialCharDefinition = {
'#': /[A-Z]/,
'#': /[a-zA-Z]/,
'&': /[0-9]/,
'$': /[A-Z0-9]/,
'$': /[a-zA-Z0-9]/,
}

export function getMaskForPrefixOrDefault(_appended: string, dynamicMasked: DynamicMasked) {
const mask = dynamicMasked.compiledMasks.find(mask => {
return dynamicMasked.rawInputValue.slice(0, 2) === mask.startsWith;
return dynamicMasked.rawInputValue.slice(0, 2).toUpperCase() === mask.startsWith;
})

if (!mask) {
return dynamicMasked.compiledMasks.find(mask => {
return mask.default === true;
})
}

return mask;
}
Loading