generated from Tinkoff/angular-open-source-starter
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(kit):
Date
add max/min options (#70)
Co-authored-by: v.potekhin <v.potekhin@tinkoff.ru>
- Loading branch information
1 parent
19fda20
commit b708243
Showing
11 changed files
with
352 additions
and
1 deletion.
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
projects/demo-integrations/cypress/tests/kit/date/date-min-max.cy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import {DemoPath} from '@demo/routes'; | ||
|
||
describe('Date', () => { | ||
describe('Max date', () => { | ||
beforeEach(() => { | ||
cy.visit(`/${DemoPath.Date}/API?max=2020-05-05`); | ||
cy.get('#demo-content input') | ||
.should('be.visible') | ||
.first() | ||
.focus() | ||
.as('input'); | ||
}); | ||
|
||
it('Input less than max value', () => { | ||
cy.get('@input') | ||
.type('31122019') | ||
.should('have.value', '31.12.2019') | ||
.should('have.prop', 'selectionStart', '31.12.2019'.length) | ||
.should('have.prop', 'selectionEnd', '31.12.2019'.length); | ||
}); | ||
|
||
it('05.12.202| => type 5 => 05.05.2020 (max value)', () => { | ||
cy.get('@input') | ||
.type('0512202') | ||
.should('have.value', '05.12.202') | ||
.should('have.prop', 'selectionStart', '05.12.202'.length) | ||
.should('have.prop', 'selectionEnd', '05.12.202'.length) | ||
.type('5') | ||
.should('have.value', '05.05.2020') | ||
.should('have.prop', 'selectionStart', '05.05.2020'.length) | ||
.should('have.prop', 'selectionEnd', '05.05.2020'.length); | ||
}); | ||
|
||
it('0|3.05.2020 => type 7 => 05|.05.2020 (max value)', () => { | ||
cy.get('@input') | ||
.type('03052020') | ||
.type('{moveToStart}{rightArrow}') | ||
.type('7') | ||
.should('have.value', '05.05.2020') | ||
.should('have.prop', 'selectionStart', '05.'.length) | ||
.should('have.prop', 'selectionEnd', '05.'.length); | ||
}); | ||
|
||
it('03.0|5.2020 => type 7 => 05.05|.2020 (max value)', () => { | ||
cy.get('@input') | ||
.type('03052020') | ||
.type('{leftArrow}'.repeat('5.2020'.length)) | ||
.type('7') | ||
.should('have.value', '05.05.2020') | ||
.should('have.prop', 'selectionStart', '05.05.'.length) | ||
.should('have.prop', 'selectionEnd', '05.05.'.length); | ||
}); | ||
}); | ||
|
||
describe('Min date', () => { | ||
beforeEach(() => { | ||
cy.visit(`/${DemoPath.Date}/API?min=2020-05-05`); | ||
cy.get('#demo-content input') | ||
.should('be.visible') | ||
.first() | ||
.focus() | ||
.as('input'); | ||
}); | ||
|
||
it('Input more than min value', () => { | ||
cy.get('@input') | ||
.type('20102022') | ||
.should('have.value', '20.10.2022') | ||
.should('have.prop', 'selectionStart', '20.10.2022'.length) | ||
.should('have.prop', 'selectionEnd', '20.10.2022'.length); | ||
}); | ||
|
||
it('05.12.201| => type 9 => 05.05.2020 (min value)', () => { | ||
cy.get('@input') | ||
.type('0512201') | ||
.should('have.value', '05.12.201') | ||
.should('have.prop', 'selectionStart', '05.12.201'.length) | ||
.should('have.prop', 'selectionEnd', '05.12.201'.length) | ||
.type('9') | ||
.should('have.value', '05.05.2020') | ||
.should('have.prop', 'selectionStart', '05.05.2020'.length) | ||
.should('have.prop', 'selectionEnd', '05.05.2020'.length); | ||
}); | ||
|
||
it('0|7.05.2020 => type 2 => 05|.05.2020 (min value)', () => { | ||
cy.get('@input') | ||
.type('07052020') | ||
.type('{moveToStart}{rightArrow}') | ||
.type('2') | ||
.should('have.value', '05.05.2020') | ||
.should('have.prop', 'selectionStart', '05.'.length) | ||
.should('have.prop', 'selectionEnd', '05.'.length); | ||
}); | ||
|
||
it('03.0|6.2020 => type 2 => 05.05|.2020 (min value)', () => { | ||
cy.get('@input') | ||
.type('03062020') | ||
.type('{leftArrow}'.repeat('6.2020'.length)) | ||
.type('2') | ||
.should('have.value', '05.05.2020') | ||
.should('have.prop', 'selectionStart', '05.05.'.length) | ||
.should('have.prop', 'selectionEnd', '05.05.'.length); | ||
}); | ||
}); | ||
}); |
51 changes: 51 additions & 0 deletions
51
projects/demo-integrations/cypress/tests/kit/date/date-mode.cy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import {DemoPath} from '@demo/routes'; | ||
|
||
describe('Date', () => { | ||
describe('Mode', () => { | ||
describe('YMD', () => { | ||
beforeEach(() => { | ||
cy.visit(`/${DemoPath.Date}/API?mode=YMD`); | ||
cy.get('#demo-content input') | ||
.should('be.visible') | ||
.first() | ||
.focus() | ||
.as('input'); | ||
}); | ||
|
||
it(' "YMD" => 2019.12.31', () => { | ||
cy.get('@input') | ||
.type('20193') | ||
.should('have.value', '2019.03') | ||
.should('have.prop', 'selectionStart', '2019.03'.length) | ||
.should('have.prop', 'selectionEnd', '2019.03'.length) | ||
.type('22') | ||
.should('have.value', '2019.03.22') | ||
.should('have.prop', 'selectionStart', '2019.03.22'.length) | ||
.should('have.prop', 'selectionEnd', '2019.03.22'.length); | ||
}); | ||
}); | ||
|
||
describe('MDY', () => { | ||
beforeEach(() => { | ||
cy.visit(`/${DemoPath.Date}/API?mode=MDY`); | ||
cy.get('#demo-content input') | ||
.should('be.visible') | ||
.first() | ||
.focus() | ||
.as('input'); | ||
}); | ||
|
||
it(' "MDY" => 03.12.2020', () => { | ||
cy.get('@input') | ||
.type('312') | ||
.should('have.value', '03.12') | ||
.should('have.prop', 'selectionStart', '03.12'.length) | ||
.should('have.prop', 'selectionEnd', '03.12'.length) | ||
.type('2022') | ||
.should('have.value', '03.12.2022') | ||
.should('have.prop', 'selectionStart', '03.12.2022'.length) | ||
.should('have.prop', 'selectionEnd', '03.12.2022'.length); | ||
}); | ||
}); | ||
}); | ||
}); |
51 changes: 51 additions & 0 deletions
51
projects/demo-integrations/cypress/tests/kit/date/date-separator.cy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import {DemoPath} from '@demo/routes'; | ||
|
||
describe('Date', () => { | ||
describe('Separator', () => { | ||
describe('/', () => { | ||
beforeEach(() => { | ||
cy.visit(`/${DemoPath.Date}/API?separator=/`); | ||
cy.get('#demo-content input') | ||
.should('be.visible') | ||
.first() | ||
.focus() | ||
.as('input'); | ||
}); | ||
|
||
it(' "/" => 31/12/2019', () => { | ||
cy.get('@input') | ||
.type('3112') | ||
.should('have.value', '31/12') | ||
.should('have.prop', 'selectionStart', '31/12'.length) | ||
.should('have.prop', 'selectionEnd', '31/12'.length) | ||
.type('2019') | ||
.should('have.value', '31/12/2019') | ||
.should('have.prop', 'selectionStart', '31/12/2019'.length) | ||
.should('have.prop', 'selectionEnd', '31/12/2019'.length); | ||
}); | ||
}); | ||
|
||
describe('-', () => { | ||
beforeEach(() => { | ||
cy.visit(`/${DemoPath.Date}/API?separator=-`); | ||
cy.get('#demo-content input') | ||
.should('be.visible') | ||
.first() | ||
.focus() | ||
.as('input'); | ||
}); | ||
|
||
it(' "-" => 31-12-2019', () => { | ||
cy.get('@input') | ||
.type('3112') | ||
.should('have.value', '31-12') | ||
.should('have.prop', 'selectionStart', '31-12'.length) | ||
.should('have.prop', 'selectionEnd', '31-12'.length) | ||
.type('2019') | ||
.should('have.value', '31-12-2019') | ||
.should('have.prop', 'selectionStart', '31-12-2019'.length) | ||
.should('have.prop', 'selectionEnd', '31-12-2019'.length); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './max-validation-preprocessor'; | ||
export * from './min-max-postprocessor'; |
39 changes: 39 additions & 0 deletions
39
projects/kit/src/lib/masks/date/processors/min-max-postprocessor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import {MaskitoOptions} from '@maskito/core'; | ||
|
||
import {dateToSegments, parseDateString, segmentsToDate, toDateString} from '../utils'; | ||
|
||
export function createMinMaxValuePostprocessor( | ||
{ | ||
min = new Date('0001-01-01'), | ||
max = new Date('9999-12-31'), | ||
}: { | ||
min?: Date; | ||
max?: Date; | ||
}, | ||
fullDateMode: string, | ||
): NonNullable<MaskitoOptions['postprocessor']> { | ||
return elementState => { | ||
const {value, selection} = elementState; | ||
|
||
if (value.length < fullDateMode.length) { | ||
return elementState; | ||
} | ||
|
||
const parsedDate = parseDateString(value, fullDateMode); | ||
const date = segmentsToDate(parsedDate); | ||
|
||
if (date < min || date > max) { | ||
const finalValue = toDateString( | ||
dateToSegments(date < min ? min : max), | ||
fullDateMode, | ||
); | ||
|
||
return { | ||
selection, | ||
value: finalValue, | ||
}; | ||
} | ||
|
||
return elementState; | ||
}; | ||
} |
Oops, something went wrong.