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

feat(kit): Number supports new configurable parameter minusSign #1118

Merged
merged 33 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
f23cef5
feat(kit): added support of custom minus sign for number
Mar 4, 2024
a187ea2
fix(demo): added minusSign property with default value CHAR_MINUS
Mar 4, 2024
c94d0d7
docs(demo): added component and mask for minusSign documentation
Mar 4, 2024
8cc911e
docs(demo): added minusSign example to docs page
Mar 4, 2024
a845be8
docs(demo): added initianl value for custom minus sign example
Mar 4, 2024
1ca1a35
docs(demo): added minus sign to api docs
Mar 4, 2024
b990735
test(kit): added unit tests for minus sign
Mar 4, 2024
2129290
test(demo-integrations): added e2e tests for number minus sign prop
Mar 4, 2024
b512ab0
fix(demo): incorrectly spelled word corrected
Mar 4, 2024
ab80b15
refactor(kit): replaced defenition of pseudoMinus
Mar 4, 2024
4ded341
docs(demo): changed minus sign of the example, and refactored example…
Mar 4, 2024
9c8856e
refactor(demo): added readonly modifer for options
Mar 4, 2024
c2f9bf0
refactor(demo): refactored mask.ts and component.ts to be as all othe…
Mar 4, 2024
e8ec10a
refactor(kit): added using constants in unit tests
Mar 4, 2024
9c0efc6
refactor(demo-integrations): added using constants in CT
Mar 4, 2024
9a06e71
test(demo-integrations): added using unicode constats
Mar 4, 2024
4fdce96
fix(kit): fixed trailing whitespace with not CHAR_MINUS as minus(i=mi…
Mar 5, 2024
ac55a1f
test(kit): refactored unit tests
Mar 5, 2024
b7dac00
test(demo-integrations): refactored tests for custom minus sign in nu…
Mar 6, 2024
6174d05
refactor(demo): swaped 5th and 6th examples
Mar 6, 2024
72200c1
docs(demo): changed documentation for minus sign
Mar 6, 2024
c8182e0
refactor(kit): removed minusSign from pseudoMinuses and type annotati…
Mar 6, 2024
1e3276e
docs(demo): changed import of CHAR_MINUS and deleted importing component
Mar 6, 2024
957f8e9
fix(demo): swaped example inputs
Mar 6, 2024
a477de0
docs(demo): added tuiLinkModule for number example
Mar 6, 2024
a6e5479
refactor(demo): changed width of minus example input
Mar 6, 2024
0fb5728
fix(kit): fixed error with parsing min-max value with custom minus
Mar 6, 2024
2a222bd
test(kit): added test with custom minus and min value
Mar 6, 2024
8a3b3f7
test(demo-integrations): added e2e tests for min value with custom minus
Mar 7, 2024
c1ea145
test(demo-integrations): refactored first block of tests, and added s…
Mar 7, 2024
9a158ee
test(demo-integrations): refactored encodeing chars for queryString
Mar 7, 2024
8ee9d04
fix(demo): fixed conflict in number-mask-doc.component.ts
Mar 7, 2024
c5cf0a0
chore: apply changes after linting [bot]
taiga-family-bot Mar 7, 2024
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
@@ -0,0 +1,45 @@
import {
CHAR_EM_DASH,
CHAR_EN_DASH,
CHAR_HYPHEN,
CHAR_JP_HYPHEN,
CHAR_MINUS,
} from 'projects/kit/src/lib/constants';

import {openNumberPage} from './utils';

describe('Properly using custom minus sign', () => {
const minuses: Array<{value: string; name: string; asQueryParam: string}> = [
{value: CHAR_HYPHEN, name: 'hyphen', asQueryParam: '-'},
{value: 'i', name: 'i', asQueryParam: 'i'},
];

const numbers = ['321', '2_432'];

const pseudoMinuses: Array<{value: string; name: string}> = [
{value: CHAR_HYPHEN, name: 'hyphen'},
{value: CHAR_EN_DASH, name: 'en-dash'},
{value: CHAR_EM_DASH, name: 'em-dash'},
{value: CHAR_JP_HYPHEN, name: 'japanese prolonged sound mark'},
{value: CHAR_MINUS, name: 'unicode minus sign'},
];

minuses.forEach(minus => {
if (minus.value === 'i') {
pseudoMinuses.push(minus);
}
KrollikRoddzer marked this conversation as resolved.
Show resolved Hide resolved

pseudoMinuses.forEach(pseudoMinus => {
numbers.forEach(number => {
it(`transforms ${pseudoMinus.name} into ${minus.name}`, () => {
openNumberPage(
`precision=Infinity&thousandSeparator=_&minusSign=${minus.asQueryParam}`,
);
cy.get('@input')
.type(`${pseudoMinus.value}${number}`)
.should('have.value', `${minus.value}${number}`);
});
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {Component} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {MaskitoDirective} from '@maskito/angular';
import {TuiTextfieldControllerModule} from '@taiga-ui/core';
import {TuiInputModule} from '@taiga-ui/kit';

import mask from './mask';

@Component({
standalone: true,
selector: 'number-mask-doc-example-5',
imports: [
TuiInputModule,
TuiTextfieldControllerModule,
MaskitoDirective,
FormsModule,
],
template: `
<tui-input
[tuiTextfieldLabelOutside]="true"
[(ngModel)]="value"
>
<input
tuiTextfield
[maskito]="options"
/>
</tui-input>
KrollikRoddzer marked this conversation as resolved.
Show resolved Hide resolved
`,
})
export class NumberMaskDocExample5 {
protected value = '-42';
protected readonly options = mask;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {maskitoNumberOptionsGenerator} from '@maskito/kit';

export default maskitoNumberOptionsGenerator({
minusSign: '-',
thousandSeparator: '',
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {getMaskitoOptions} from './mask';

@Component({
standalone: true,
selector: 'number-mask-doc-example-5',
selector: 'number-mask-doc-example-6',
imports: [
TuiLabelModule,
TuiInputModule,
Expand All @@ -42,7 +42,7 @@ import {getMaskitoOptions} from './mask';
`,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class NumberMaskDocExample5 {
export class NumberMaskDocExample6 {
protected value = '42';
protected decimalZeroPadding = this.value.includes('.');

Expand Down
18 changes: 14 additions & 4 deletions projects/demo/src/pages/kit/number/number-mask-doc.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
maskitoNumberOptionsGenerator,
maskitoRemoveOnBlurPlugin,
} from '@maskito/kit';
import {CHAR_MINUS} from '@maskito/kit/src/lib/constants';
import {TuiAddonDocModule, TuiDocExample} from '@taiga-ui/addon-doc';
import {TuiNotificationModule} from '@taiga-ui/core';
import {tuiInputCountOptionsProvider, TuiInputModule} from '@taiga-ui/kit';
Expand All @@ -17,7 +18,8 @@ import {NumberMaskDocExample1} from './examples/1-high-precision/component';
import {NumberMaskDocExample2} from './examples/2-separators/component';
import {NumberMaskDocExample3} from './examples/3-postfix/component';
import {NumberMaskDocExample4} from './examples/4-decimal-zero-padding/component';
import {NumberMaskDocExample5} from './examples/5-dynamic-decimal-zero-padding/component';
import {NumberMaskDocExample5} from './examples/5-custom-minus-sign/components';
import {NumberMaskDocExample6} from './examples/6-dynamic-decimal-zero-padding/component';

type GeneratorOptions = Required<
NonNullable<Parameters<typeof maskitoNumberOptionsGenerator>[0]>
Expand All @@ -37,6 +39,7 @@ type GeneratorOptions = Required<
NumberMaskDocExample3,
NumberMaskDocExample4,
NumberMaskDocExample5,
NumberMaskDocExample6,
],
templateUrl: './number-mask-doc.template.html',
changeDetection: ChangeDetectionStrategy.OnPush,
Expand Down Expand Up @@ -69,12 +72,18 @@ export class NumberMaskDocComponent implements GeneratorOptions {
),
};

protected readonly dynamicDecimalZeroPaddingExample5: TuiDocExample = {
protected readonly customMinusSignExample5: TuiDocExample = {
[DocExamplePrimaryTab.MaskitoOptions]: import(
'./examples/5-dynamic-decimal-zero-padding/mask.ts?raw'
'./examples/5-custom-minus-sign/mask.ts?raw'
),
};

protected readonly dynamicDecimalZeroPaddingExample6: TuiDocExample = {
[DocExamplePrimaryTab.MaskitoOptions]: import(
'./examples/6-dynamic-decimal-zero-padding/mask.ts?raw'
),
[DocExamplePrimaryTab.Angular]: import(
'./examples/5-dynamic-decimal-zero-padding/component.ts?raw'
'./examples/6-dynamic-decimal-zero-padding/component.ts?raw'
),
};

Expand All @@ -97,6 +106,7 @@ export class NumberMaskDocComponent implements GeneratorOptions {
public thousandSeparator = ' ';
public prefix = '';
public postfix = '';
public minusSign = CHAR_MINUS;

protected maskitoOptions: MaskitoOptions = this.calculateMask(this);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,39 @@
<number-mask-doc-example-4></number-mask-doc-example-4>
</tui-doc-example>

<tui-doc-example
id="minus-sign"
heading="Minus sign"
KrollikRoddzer marked this conversation as resolved.
Show resolved Hide resolved
[content]="customMinusSignExample5"
[description]="customMinusSignDescription"
>
<ng-template #customMinusSignDescription>
<p>
Use
<code>minusSign</code>
parameter to configure the character which indicates that a number is negative.
</p>
<p>
In this example
<a
href="https://symbl.cc/en/2010"
rel="noreferrer"
target="_blank"
tuiLink
KrollikRoddzer marked this conversation as resolved.
Show resolved Hide resolved
>
hyphen
</a>
is used as
<code>minusSign</code>
</p>
</ng-template>
<number-mask-doc-example-6></number-mask-doc-example-6>
KrollikRoddzer marked this conversation as resolved.
Show resolved Hide resolved
</tui-doc-example>

<tui-doc-example
id="dynamic-decimal-zero-padding"
heading="Dynamic decimal zero padding"
[content]="dynamicDecimalZeroPaddingExample5"
[content]="dynamicDecimalZeroPaddingExample6"
[description]="dynamicDecimalZeroPaddingDescription"
>
<ng-template #dynamicDecimalZeroPaddingDescription>
Expand Down Expand Up @@ -286,6 +315,27 @@
empty string (no postfix).
</p>
</ng-template>
<ng-template
documentationPropertyMode="input"
documentationPropertyName="minusSign"
documentationPropertyType="string"
[(documentationPropertyValue)]="minusSign"
(documentationPropertyValueChange)="updateOptions()"
>
A minus symbol.

<p>
<strong>Default:</strong>
<a
href="https://symbl.cc/en/2212"
rel="noreferrer"
target="_blank"
tuiLink
>
<code>\u2212</code>
</a>
</p>
</ng-template>
</tui-doc-documentation>
</ng-template>
</tui-doc-page>
13 changes: 11 additions & 2 deletions projects/kit/src/lib/masks/number/number-mask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export function maskitoNumberOptionsGenerator({
decimalZeroPadding = false,
prefix: unsafePrefix = '',
postfix = '',
minusSign = CHAR_MINUS,
KrollikRoddzer marked this conversation as resolved.
Show resolved Hide resolved
}: {
min?: number;
max?: number;
Expand All @@ -53,13 +54,18 @@ export function maskitoNumberOptionsGenerator({
thousandSeparator?: string;
prefix?: string;
postfix?: string;
minusSign?: string;
} = {}): Required<MaskitoOptions> {
const pseudoMinuses = [
CHAR_HYPHEN,
CHAR_EN_DASH,
CHAR_EM_DASH,
CHAR_JP_HYPHEN,
].filter(char => char !== thousandSeparator && char !== decimalSeparator);
CHAR_MINUS,
].filter(
char =>
char !== thousandSeparator && char !== decimalSeparator && char !== minusSign,
);
const validatedDecimalPseudoSeparators = validateDecimalPseudoSeparators({
decimalSeparator,
thousandSeparator,
Expand All @@ -79,6 +85,7 @@ export function maskitoNumberOptionsGenerator({
prefix,
postfix,
isNegativeAllowed: min < 0,
minusSign,
}),
preprocessors: [
createFullWidthToHalfWidthPreprocessor(),
Expand All @@ -88,10 +95,11 @@ export function maskitoNumberOptionsGenerator({
pseudoMinuses,
prefix,
postfix,
minusSign,
}),
createAffixesFilterPreprocessor({prefix, postfix}),
createPseudoCharactersPreprocessor({
validCharacter: CHAR_MINUS,
validCharacter: minusSign,
pseudoCharacters: pseudoMinuses,
prefix,
postfix,
Expand Down Expand Up @@ -134,6 +142,7 @@ export function maskitoNumberOptionsGenerator({
thousandSeparator,
prefix,
postfix,
minusSign,
}),
createDecimalZeroPaddingPostprocessor({
decimalSeparator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ export function createInitializationOnlyPreprocessor({
pseudoMinuses,
prefix,
postfix,
minusSign,
}: {
decimalSeparator: string;
decimalPseudoSeparators: readonly string[];
pseudoMinuses: readonly string[];
prefix: string;
postfix: string;
minusSign: string;
}): MaskitoPreprocessor {
let isInitializationPhase = true;
const cleanNumberMask = generateMaskExpression({
Expand All @@ -36,6 +38,7 @@ export function createInitializationOnlyPreprocessor({
thousandSeparator: '',
precision: Infinity,
isNegativeAllowed: true,
minusSign,
});

return ({elementState, data}) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {MaskitoPostprocessor} from '@maskito/core';

import {CHAR_MINUS} from '../../../constants';
import {extractAffixes, identity} from '../../../utils';

/**
Expand All @@ -12,11 +11,13 @@ export function createThousandSeparatorPostprocessor({
decimalSeparator,
prefix,
postfix,
minusSign,
}: {
thousandSeparator: string;
decimalSeparator: string;
prefix: string;
postfix: string;
minusSign: string;
}): MaskitoPostprocessor {
if (!thousandSeparator) {
return identity;
Expand All @@ -31,7 +32,7 @@ export function createThousandSeparatorPostprocessor({
});

const [integerPart, decimalPart = ''] = cleanValue
.replace(CHAR_MINUS, '')
.replace(minusSign, '')
.split(decimalSeparator);
const [initialFrom, initialTo] = selection;
let [from, to] = selection;
Expand Down Expand Up @@ -83,7 +84,7 @@ export function createThousandSeparatorPostprocessor({
return {
value:
extractedPrefix +
(cleanValue.includes(CHAR_MINUS) ? CHAR_MINUS : '') +
(cleanValue.includes(minusSign) ? minusSign : '') +
processedIntegerPart +
(cleanValue.includes(decimalSeparator) ? decimalSeparator : '') +
decimalPart +
Expand Down
Loading
Loading