Skip to content

Commit

Permalink
feat(kit): Date supports dd/mm and mm/dd modes (#1939)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrey Belokopytov <[email protected]>
Co-authored-by: Nikita Barsukov <[email protected]>
  • Loading branch information
3 people authored Feb 3, 2025
1 parent df0c3de commit bc290af
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 1 deletion.
102 changes: 102 additions & 0 deletions projects/demo-integrations/src/tests/kit/date/date-mode.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,5 +203,107 @@ describe('Date', () => {
.should('have.prop', 'selectionEnd', '2025'.length);
});
});

describe('dd/mm', () => {
beforeEach(() => {
cy.visit(`/${DemoPath.Date}/API?mode=dd%2Fmm`);
cy.get('#demo-content input')
.should('be.visible')
.first()
.focus()
.as('input');
});

it('"dd/mm" => 14.07', () => {
cy.get('@input')
.type('1407')
.should('have.value', '14.07')
.should('have.prop', 'selectionStart', '14.07'.length)
.should('have.prop', 'selectionEnd', '14.07'.length);
});

it('"dd/mm" => 01.01', () => {
cy.get('@input')
.type('0101')
.should('have.value', '01.01')
.should('have.prop', 'selectionStart', '01.11'.length)
.should('have.prop', 'selectionEnd', '01.11'.length);
});

it('"dd/mm" => 05.11', () => {
cy.get('@input')
.type('511')
.should('have.value', '05.11')
.should('have.prop', 'selectionStart', '05.11'.length)
.should('have.prop', 'selectionEnd', '05.11'.length);
});

it('"dd/mm" => 05.05', () => {
cy.get('@input')
.type('55')
.should('have.value', '05.05')
.should('have.prop', 'selectionStart', '05.05'.length)
.should('have.prop', 'selectionEnd', '05.05'.length);
});

it('dd/mm" => 01.05', () => {
cy.get('@input')
.type('3104')
.should('have.value', '01.05')
.should('have.prop', 'selectionStart', '01.05'.length)
.should('have.prop', 'selectionEnd', '01.05'.length);
});
});

describe('mm/dd', () => {
beforeEach(() => {
cy.visit(`/${DemoPath.Date}/API?mode=mm%2Fdd`);
cy.get('#demo-content input')
.should('be.visible')
.first()
.focus()
.as('input');
});

it('"mm/dd" => 02.29', () => {
cy.get('@input')
.type('0229')
.should('have.value', '02.29')
.should('have.prop', 'selectionStart', '02.29'.length)
.should('have.prop', 'selectionEnd', '02.29'.length);
});

it('"mm/dd" => 01.01', () => {
cy.get('@input')
.type('0101')
.should('have.value', '01.01')
.should('have.prop', 'selectionStart', '01.01'.length)
.should('have.prop', 'selectionEnd', '01.01'.length);
});

it('"mm/dd" => 09.12', () => {
cy.get('@input')
.type('912')
.should('have.value', '09.12')
.should('have.prop', 'selectionStart', '09.12'.length)
.should('have.prop', 'selectionEnd', '09.12'.length);
});

it('"mm/dd" => 09.09', () => {
cy.get('@input')
.type('99')
.should('have.value', '09.09')
.should('have.prop', 'selectionStart', '09.09'.length)
.should('have.prop', 'selectionEnd', '09.09'.length);
});

it('dd/mm" => 05.01', () => {
cy.get('@input')
.type('431')
.should('have.value', '05.01')
.should('have.prop', 'selectionStart', '05.01'.length)
.should('have.prop', 'selectionEnd', '05.01'.length);
});
});
});
});
2 changes: 2 additions & 0 deletions projects/demo/src/pages/kit/date/date-mask-doc.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export default class DateMaskDocComponent implements GeneratorOptions {
'dd/mm/yyyy',
'mm/dd/yyyy',
'yyyy/mm/dd',
'dd/mm',
'mm/dd',
'mm/yy',
'mm/yyyy',
'yyyy/mm',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
} from '../utils';
import {raiseSegmentValueToMin} from '../utils/date/raise-segment-value-to-min';

const LEAP_YEAR = '1972';

export function createMinMaxDatePostprocessor({
dateModeTemplate,
min = DEFAULT_MIN_DATE,
Expand Down Expand Up @@ -48,7 +50,8 @@ export function createMinMaxDatePostprocessor({
continue;
}

const date = segmentsToDate(parsedDate);
const date = segmentsToDate({year: LEAP_YEAR, ...parsedDate});

const clampedDate = clamp(date, min, max);

validatedValue += toDateString(dateToSegments(clampedDate), {
Expand Down
2 changes: 2 additions & 0 deletions projects/kit/src/lib/types/date-mode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export type MaskitoDateMode =
| 'dd/mm'
| 'dd/mm/yyyy'
| 'mm/dd'
| 'mm/dd/yyyy'
| 'mm/yy'
| 'mm/yyyy'
Expand Down

0 comments on commit bc290af

Please sign in to comment.