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): Time supports SS.MSS & MM.SS.MSS modes #1224

Merged
merged 11 commits into from
May 20, 2024
236 changes: 236 additions & 0 deletions projects/demo-integrations/src/tests/kit/time/time-mode.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,5 +412,241 @@ describe('Time', () => {
});
});
});

describe('MM.SS.MSS', () => {
beforeEach(() => {
cy.visit(`/${DemoPath.Time}/API?mode=MM.SS.MSS`);
cy.get('#demo-content input')
.should('be.visible')
.first()
.focus()
.clear()
.as('input');
});

describe('Typing new character overwrite character after cursor', () => {
it('new character is different from the next one', () => {
cy.get('@input')
.type('5959999')
.type('{moveToStart}')
.type('0')
.should('have.value', '09.59.999')
.should('have.prop', 'selectionStart', '0'.length)
.should('have.prop', 'selectionEnd', '0'.length)
.type('000')
.should('have.value', '00.00.999')
.should('have.prop', 'selectionStart', '00.00.'.length)
.should('have.prop', 'selectionEnd', '00.00.'.length)
.type('00')
.should('have.value', '00.00.009')
.should('have.prop', 'selectionStart', '00.00.00'.length)
.should('have.prop', 'selectionEnd', '00.00.00'.length);
});

it('moves cursor behind next character if new character is the same with the next one', () => {
cy.get('@input')
.type('59.59.999')
.type('{moveToStart}')
.type('{rightArrow}'.repeat('59.59.'.length))
.type('9')
.should('have.value', '59.59.999')
.should('have.prop', 'selectionStart', '59.59.9'.length)
.should('have.prop', 'selectionEnd', '59.59.9'.length);
});
});

it('Pad typed value with zero if digit exceeds the first digit of time segment', () => {
cy.get('@input')
.type('6')
.should('have.value', '06')
.should('have.prop', 'selectionStart', '06'.length)
.should('have.prop', 'selectionEnd', '06'.length)
.type('6')
.should('have.value', '06.06')
.should('have.prop', 'selectionStart', '06.06'.length)
.should('have.prop', 'selectionEnd', '06.06'.length)
.type('999')
.should('have.value', '06.06.999')
.should('have.prop', 'selectionStart', '06.06.999'.length)
.should('have.prop', 'selectionEnd', '06.06.999'.length);
});

describe('Select range and press new digit', () => {
it(
'|59|.59.999 => Type 2 => 2|0.59.999',
BROWSER_SUPPORTS_REAL_EVENTS,
() => {
cy.get('@input')
.type('5959999')
.should('have.value', '59.59.999')
.realPress([
...new Array('.59.999'.length).fill('ArrowLeft'),
'Shift',
...new Array('59'.length).fill('ArrowLeft'),
]);

cy.get('@input')
.type('2')
.should('have.value', '20.59.999')
.should('have.prop', 'selectionStart', '2'.length)
.should('have.prop', 'selectionEnd', '2'.length);
},
);

it(
'|59|.59.999 => Type 6 => 06.|59.999',
BROWSER_SUPPORTS_REAL_EVENTS,
() => {
cy.get('@input')
.type('5959999')
.should('have.value', '59.59.999')
.realPress([
...new Array('.59.999'.length).fill('ArrowLeft'),
'Shift',
...new Array('59'.length).fill('ArrowLeft'),
]);

cy.get('@input')
.type('6')
.should('have.value', '06.59.999')
.should('have.prop', 'selectionStart', '06.'.length)
.should('have.prop', 'selectionEnd', '06.'.length);
},
);
});

describe('accepts time segment separators typed by user', () => {
it('59 => Type . => 59.', () => {
cy.get('@input')
.type('59')
.should('have.value', '59')
.type('.')
.should('have.value', '59.')
.should('have.prop', 'selectionStart', '59.'.length)
.should('have.prop', 'selectionEnd', '59.'.length);
});

it('59.59 => Type . => 59:59.', () => {
cy.get('@input')
.type('5959')
.should('have.value', '59.59')
.type('.')
.should('have.value', '59.59.')
.should('have.prop', 'selectionStart', '59.59.'.length)
.should('have.prop', 'selectionEnd', '59.59.'.length);
});
});

it('type 5959999 => 59.59.999', () => {
cy.get('@input').type('5959999').should('have.value', '59.59.999');
});
});

describe('SS.MSS', () => {
beforeEach(() => {
cy.visit(`/${DemoPath.Time}/API?mode=SS.MSS`);
cy.get('#demo-content input')
.should('be.visible')
.first()
.focus()
.clear()
.as('input');
});

describe('Typing new character overwrite character after cursor', () => {
it('new character is different from the next one', () => {
cy.get('@input')
.type('59999')
.type('{moveToStart}')
.type('0')
.should('have.value', '09.999')
.should('have.prop', 'selectionStart', '0'.length)
.should('have.prop', 'selectionEnd', '0'.length)
.type('0')
.should('have.value', '00.999')
.should('have.prop', 'selectionStart', '00.'.length)
.should('have.prop', 'selectionEnd', '00.'.length)
.type('00')
.should('have.value', '00.009')
.should('have.prop', 'selectionStart', '00.00'.length)
.should('have.prop', 'selectionEnd', '00.00'.length);
});

it('moves cursor behind next character if new character is the same with the next one', () => {
cy.get('@input')
.type('59.999')
.type('{moveToStart}')
.type('{rightArrow}'.repeat('59.'.length))
.type('9')
.should('have.value', '59.999')
.should('have.prop', 'selectionStart', '59.9'.length)
.should('have.prop', 'selectionEnd', '59.9'.length);
});
});

it('Pad typed value with zero if digit exceeds the first digit of time segment', () => {
cy.get('@input')
.type('6')
.should('have.value', '06')
.should('have.prop', 'selectionStart', '06'.length)
.should('have.prop', 'selectionEnd', '06'.length)
.type('999')
.should('have.value', '06.999')
.should('have.prop', 'selectionStart', '06.999'.length)
.should('have.prop', 'selectionEnd', '06:999'.length);
});

describe('Select range and press new digit', () => {
it('|59|.999 => Type 2 => 2|0.999', BROWSER_SUPPORTS_REAL_EVENTS, () => {
cy.get('@input')
.type('59999')
.should('have.value', '59.999')
.realPress([
...new Array('.999'.length).fill('ArrowLeft'),
'Shift',
...new Array('59'.length).fill('ArrowLeft'),
]);

cy.get('@input')
.type('2')
.should('have.value', '20.999')
.should('have.prop', 'selectionStart', '2'.length)
.should('have.prop', 'selectionEnd', '2'.length);
});

it('|59|.999 => Type 6 => 06.|999', BROWSER_SUPPORTS_REAL_EVENTS, () => {
cy.get('@input')
.type('59999')
.should('have.value', '59.999')
.realPress([
...new Array('.999'.length).fill('ArrowLeft'),
'Shift',
...new Array('59'.length).fill('ArrowLeft'),
]);

cy.get('@input')
.type('6')
.should('have.value', '06.999')
.should('have.prop', 'selectionStart', '06.'.length)
.should('have.prop', 'selectionEnd', '06.'.length);
});
});

describe('accepts time segment separators typed by user', () => {
it('59 => Type . => 59.', () => {
cy.get('@input')
.type('59')
.should('have.value', '59')
.type('.')
.should('have.value', '59.')
.should('have.prop', 'selectionStart', '59.'.length)
.should('have.prop', 'selectionEnd', '59.'.length);
});
});

it('type 59999 => 59.999', () => {
cy.get('@input').type('59999').should('have.value', '59.999');
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,22 @@
</p>

<ul class="tui-list">
<li class="tui-list__item"><code>selectionStart</code></li>
<li class="tui-list__item"><code>selectionEnd</code></li>
<li class="tui-list__item"><code>setSelectionRange</code></li>
<li class="tui-list__item">
<code>selectionStart</code>
</li>
<li class="tui-list__item">
<code>selectionEnd</code>
</li>
<li class="tui-list__item">
<code>setSelectionRange</code>
</li>
</ul>

<p>
According to the
<a
href="https://html.spec.whatwg.org/multipage/input.html#concept-input-apply"
rel="noreferrer"
target="_blank"
tuiLink
>
Expand Down
2 changes: 2 additions & 0 deletions projects/demo/src/pages/kit/time/time-mask-doc.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export class TimeMaskDocComponent implements GeneratorOptions {
'HH:MM:SS',
'HH:MM:SS.MSS',
'HH',
'MM.SS.MSS',
'SS.MSS',
];

protected readonly timeSegmentMaxValuesOptions: Array<
Expand Down
1 change: 1 addition & 0 deletions projects/kit/src/lib/masks/date-time/date-time-mask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export function maskitoDateTimeOptionsGenerator({
dateModeTemplate,
dateSegmentsSeparator: dateSeparator,
dateTimeSeparator,
timeMode,
}),
],
postprocessors: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function createMinMaxDateTimePostprocessor({
dateTimeSeparator,
});
const parsedDate = parseDateString(dateString, dateModeTemplate);
const parsedTime = parseTimeString(timeString);
const parsedTime = parseTimeString(timeString, timeMode);

if (
!isDateTimeStringComplete(value, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {MaskitoPreprocessor} from '@maskito/core';

import {DEFAULT_TIME_SEGMENT_MAX_VALUES, TIME_FIXED_CHARACTERS} from '../../../constants';
import type {MaskitoTimeMode} from '../../../types';
import {escapeRegExp, validateDateString} from '../../../utils';
import {padTimeSegments, validateTimeString} from '../../../utils/time';
import {parseDateTimeString} from '../utils';
Expand All @@ -9,10 +10,12 @@ export function createValidDateTimePreprocessor({
dateModeTemplate,
dateSegmentsSeparator,
dateTimeSeparator,
timeMode,
}: {
dateModeTemplate: string;
dateSegmentsSeparator: string;
dateTimeSeparator: string;
timeMode: MaskitoTimeMode;
}): MaskitoPreprocessor {
const invalidCharsRegExp = new RegExp(
`[^\\d${TIME_FIXED_CHARACTERS.map(escapeRegExp).join('')}${escapeRegExp(
Expand Down Expand Up @@ -69,6 +72,7 @@ export function createValidDateTimePreprocessor({
paddedMaxValues,
offset: validatedValue.length + dateTimeSeparator.length,
selection: [from, to],
timeMode,
});

if (timeString && !validatedTimeString) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import type {MaskitoPreprocessor} from '@maskito/core';

import {TIME_FIXED_CHARACTERS} from '../../../constants';
import type {MaskitoTimeSegments} from '../../../types';
import type {MaskitoTimeMode, MaskitoTimeSegments} from '../../../types';
import {escapeRegExp} from '../../../utils';
import {padTimeSegments, validateTimeString} from '../../../utils/time';

export function createMaxValidationPreprocessor(
timeSegmentMaxValues: MaskitoTimeSegments<number>,
timeMode: MaskitoTimeMode,
): MaskitoPreprocessor {
const paddedMaxValues = padTimeSegments(timeSegmentMaxValues);
const invalidCharsRegExp = new RegExp(
Expand All @@ -26,6 +27,7 @@ export function createMaxValidationPreprocessor(
paddedMaxValues,
offset: 0,
selection,
timeMode,
});

return {
Expand All @@ -47,6 +49,7 @@ export function createMaxValidationPreprocessor(
paddedMaxValues,
offset: 0,
selection: [from, to],
timeMode,
});

if (newPossibleValue && !validatedTimeString) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import {DEFAULT_TIME_SEGMENT_MAX_VALUES} from '../../../../constants';
import {createMaxValidationPreprocessor} from '../max-validation-preprocessor';

describe('createMaxValidationPreprocessor', () => {
const processor = createMaxValidationPreprocessor(DEFAULT_TIME_SEGMENT_MAX_VALUES);
const processor = createMaxValidationPreprocessor(
DEFAULT_TIME_SEGMENT_MAX_VALUES,
'HH:MM:SS',
);

describe('Paste from clipboard', () => {
const process = (data: string): string =>
Expand Down
2 changes: 1 addition & 1 deletion projects/kit/src/lib/masks/time/time-mask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function maskitoTimeOptionsGenerator({
createFullWidthToHalfWidthPreprocessor(),
createColonConvertPreprocessor(),
createZeroPlaceholdersPreprocessor(),
createMaxValidationPreprocessor(enrichedTimeSegmentMaxValues),
createMaxValidationPreprocessor(enrichedTimeSegmentMaxValues, mode),
],
overwriteMode: 'replace',
};
Expand Down
8 changes: 7 additions & 1 deletion projects/kit/src/lib/types/time-mode.ts
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
export type MaskitoTimeMode = 'HH:MM:SS.MSS' | 'HH:MM:SS' | 'HH:MM' | 'HH';
export type MaskitoTimeMode =
| 'HH:MM:SS.MSS'
| 'HH:MM:SS'
| 'HH:MM'
| 'HH'
| 'MM.SS.MSS'
| 'SS.MSS';
Loading
Loading