Skip to content

Commit cd52fad

Browse files
authored
fix(kit): Number mask throws an error on empty string in thousandSeparator (#176)
1 parent b321b37 commit cd52fad

File tree

7 files changed

+39
-14
lines changed

7 files changed

+39
-14
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
import 'cypress-real-events'; // https://github.com/cypress-io/cypress/issues/2839
22
import './command';
3+
4+
Cypress.on('window:before:load', win => {
5+
cy.spy(win.console, 'error');
6+
});
7+
8+
afterEach(() => {
9+
cy.window().then(win => expect(win.console.error).to.have.callCount(0));
10+
});

projects/demo-integrations/cypress/tests/kit/number/number-decimal-separator.cy.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import {DemoPath} from '@demo/path';
2-
31
import {openNumberPage} from './utils';
42

53
describe('Number | Decimal separator (symbol used to separate the integer part from the fractional part)', () => {
@@ -83,16 +81,9 @@ describe('Number | Decimal separator (symbol used to separate the integer part f
8381

8482
describe('Decimal separator is a dot', () => {
8583
beforeEach(() => {
86-
cy.visit(`/${DemoPath.Number}`);
87-
cy.get('#decimal-zero-padding input')
88-
.should('be.visible')
89-
.first()
90-
.focus()
91-
.clear()
92-
.as('input');
93-
94-
// TODO https://github.com/Tinkoff/taiga-ui/issues/3474
95-
// openNumberPage('decimalSeparator=.&precision=2');
84+
openNumberPage(
85+
'decimalSeparator=.&precision=2&decimalZeroPadding=true&decimalPseudoSeparators$=2',
86+
);
9687
});
9788

9889
it('accepts dot (as the last character)', () => {

projects/demo-integrations/cypress/tests/kit/number/number-thousand-separator.cy.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,18 @@ describe('Number | thousandSeparator', () => {
123123
.should('have.prop', 'selectionEnd', '100'.length);
124124
});
125125
});
126+
127+
it('allows to set empty string as thousand separator', () => {
128+
cy.get('tr')
129+
.contains('[thousandSeparator]')
130+
.parents('tr')
131+
.find('tui-primitive-textfield')
132+
.clear();
133+
134+
cy.get('@input')
135+
.type('1000000')
136+
.should('have.value', '1000000')
137+
.should('have.prop', 'selectionStart', '1000000'.length)
138+
.should('have.prop', 'selectionEnd', '1000000'.length);
139+
});
126140
});

projects/demo-integrations/cypress/tests/ssr/ssr.cy.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import {DemoPath} from '@demo/path';
22

33
describe('Server side rendering', () => {
4+
beforeEach(() => {
5+
// Just a workaround for correct work of global run-time error handler
6+
// See projects/demo-integrations/cypress/support/e2e.ts
7+
cy.visit(`/${DemoPath.WhatIsMaskito}`);
8+
});
9+
410
const baseUrl: string = Cypress.config('baseUrl') ?? '/';
511

612
it('should serve statics and favicon.ico', () => {

projects/demo/src/pages/kit/number/number-mask-doc.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class NumberMaskDocComponent implements GeneratorOptions {
3737

3838
maskitoOptions: MaskitoOptions = maskitoNumberOptionsGenerator(this);
3939

40-
readonly decimalPseudoSeparatorsOptions = [['.', 'б', 'ю'], ['.']];
40+
readonly decimalPseudoSeparatorsOptions = [['.', 'б', 'ю'], ['.'], [',']];
4141

4242
precision = 0;
4343
isNegativeAllowed = true;

projects/kit/src/lib/masks/number/processors/thousand-separator-postprocessor.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ export function createThousandSeparatorPostprocessor({
1313
thousandSeparator: string;
1414
decimalSeparator: string;
1515
}): NonNullable<MaskitoOptions['postprocessor']> {
16+
if (!thousandSeparator) {
17+
return elementState => elementState;
18+
}
19+
1620
return ({value, selection}) => {
1721
const [integerPart, decimalPart = ''] = value.split(decimalSeparator);
1822
const [initialFrom, initialTo] = selection;

projects/kit/src/lib/masks/number/utils/generate-mask-expression.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ export function generateMaskExpression({
1515
}): MaskitoOptions['mask'] {
1616
const digit = '\\d';
1717
const optionalMinus = isNegativeAllowed ? `\\${CHAR_MINUS}?` : '';
18-
const integerPart = `[${digit}\\${thousandSeparator}]*`;
18+
const integerPart = thousandSeparator
19+
? `[${digit}\\${thousandSeparator}]*`
20+
: `[${digit}]*`;
1921
const decimalPart = `(\\${decimalSeparator}${digit}{0,${precision}})?`;
2022

2123
return precision > 0

0 commit comments

Comments
 (0)