From d90484214da76f4c73ad925eef5fe391a154c499 Mon Sep 17 00:00:00 2001 From: Rasul Date: Thu, 20 Jul 2023 17:35:24 +0300 Subject: [PATCH] feat(kit): `DateRange` add configurable parameter `rangeSeparator` (#376) --- .../date-range-custom-range-separator.cy.ts | 20 ++++++++++++ .../date-range-mask-doc.component.ts | 19 ++++++++--- .../date-range/date-range-mask-doc.module.ts | 2 ++ .../date-range-mask-doc.template.html | 32 ++++++++++++++++++- .../examples/4-range-separator/component.ts | 28 ++++++++++++++++ .../examples/4-range-separator/mask.ts | 6 ++++ .../lib/masks/date-range/date-range-mask.ts | 14 ++++---- 7 files changed, 108 insertions(+), 13 deletions(-) create mode 100644 projects/demo-integrations/cypress/tests/kit/date-range/date-range-custom-range-separator.cy.ts create mode 100644 projects/demo/src/pages/kit/date-range/examples/4-range-separator/component.ts create mode 100644 projects/demo/src/pages/kit/date-range/examples/4-range-separator/mask.ts diff --git a/projects/demo-integrations/cypress/tests/kit/date-range/date-range-custom-range-separator.cy.ts b/projects/demo-integrations/cypress/tests/kit/date-range/date-range-custom-range-separator.cy.ts new file mode 100644 index 000000000..519cdc39c --- /dev/null +++ b/projects/demo-integrations/cypress/tests/kit/date-range/date-range-custom-range-separator.cy.ts @@ -0,0 +1,20 @@ +import {DemoPath} from '@demo/constants'; + +describe('DateRange | CustomRangeSeparator', () => { + describe(' ~ ', () => { + beforeEach(() => { + cy.visit(`/${DemoPath.DateRange}/API?rangeSeparator=~`); + cy.get('#demo-content input') + .should('be.visible') + .first() + .focus() + .as('input'); + }); + + it('14.12.1997~09.07.2015', () => { + cy.get('@input') + .type('14121997972015') + .should('have.value', '14.12.1997~09.07.2015'); + }); + }); +}); diff --git a/projects/demo/src/pages/kit/date-range/date-range-mask-doc.component.ts b/projects/demo/src/pages/kit/date-range/date-range-mask-doc.component.ts index 7e2153360..5d49f7c9a 100644 --- a/projects/demo/src/pages/kit/date-range/date-range-mask-doc.component.ts +++ b/projects/demo/src/pages/kit/date-range/date-range-mask-doc.component.ts @@ -8,7 +8,7 @@ import { MaskitoDateSegments, } from '@maskito/kit'; import {TuiDocExample} from '@taiga-ui/addon-doc'; -import {CHAR_EN_DASH, CHAR_NO_BREAK_SPACE, tuiPure} from '@taiga-ui/cdk'; +import {tuiPure} from '@taiga-ui/cdk'; type GeneratorOptions = Required< NonNullable[0]> @@ -36,6 +36,12 @@ export class DateRangeMaskDocComponent implements GeneratorOptions { ), }; + readonly customRangeExample4: TuiDocExample = { + [DocExamplePrimaryTab.MaskitoOptions]: import( + './examples/4-range-separator/mask.ts?raw' + ), + }; + apiPageControl = new FormControl(''); readonly modeOptions: MaskitoDateMode[] = [`dd/mm/yyyy`, `mm/dd/yyyy`, `yyyy/mm/dd`]; @@ -59,14 +65,17 @@ export class DateRangeMaskDocComponent implements GeneratorOptions { max = new Date(this.maxStr); minLength: Partial> = {}; maxLength: Partial> = {}; + rangeSeparator = ' – '; maskitoOptions: MaskitoOptions = maskitoDateRangeOptionsGenerator(this); @tuiPure - getPlaceholder(mode: MaskitoDateMode, separator: string): string { - const datesSep = `${CHAR_NO_BREAK_SPACE}${CHAR_EN_DASH}${CHAR_NO_BREAK_SPACE}`; - - return `${mode.replace(/\//g, separator)}${datesSep}${mode.replace( + getPlaceholder( + mode: MaskitoDateMode, + separator: string, + rangeSeparator: string, + ): string { + return `${mode.replace(/\//g, separator)}${rangeSeparator}${mode.replace( /\//g, separator, )}`; diff --git a/projects/demo/src/pages/kit/date-range/date-range-mask-doc.module.ts b/projects/demo/src/pages/kit/date-range/date-range-mask-doc.module.ts index 198bddb28..aa4477972 100644 --- a/projects/demo/src/pages/kit/date-range/date-range-mask-doc.module.ts +++ b/projects/demo/src/pages/kit/date-range/date-range-mask-doc.module.ts @@ -11,6 +11,7 @@ import {DateRangeMaskDocComponent} from './date-range-mask-doc.component'; import {DateRangeMaskDocExample1} from './examples/1-date-localization/component'; import {DateRangeMaskDocExample2} from './examples/2-min-max/component'; import {DateRangeMaskDocExample3} from './examples/3-min-max-length/component'; +import {DateRangeMaskDocExample4} from './examples/4-range-separator/component'; @NgModule({ imports: [ @@ -30,6 +31,7 @@ import {DateRangeMaskDocExample3} from './examples/3-min-max-length/component'; DateRangeMaskDocExample1, DateRangeMaskDocExample2, DateRangeMaskDocExample3, + DateRangeMaskDocExample4, ], exports: [DateRangeMaskDocComponent], }) diff --git a/projects/demo/src/pages/kit/date-range/date-range-mask-doc.template.html b/projects/demo/src/pages/kit/date-range/date-range-mask-doc.template.html index 6dc0ad51a..d826cf611 100644 --- a/projects/demo/src/pages/kit/date-range/date-range-mask-doc.template.html +++ b/projects/demo/src/pages/kit/date-range/date-range-mask-doc.template.html @@ -64,6 +64,21 @@ + + + + Use + rangeSeparator + parameter to customize separator between dates of the date + range. + + + @@ -71,7 +86,9 @@ Enter dates @@ -150,6 +167,19 @@ > Maximal length of the range + + Separator between dates of the date range. +

+ Default: +  –  +

+
diff --git a/projects/demo/src/pages/kit/date-range/examples/4-range-separator/component.ts b/projects/demo/src/pages/kit/date-range/examples/4-range-separator/component.ts new file mode 100644 index 000000000..2f2c85f84 --- /dev/null +++ b/projects/demo/src/pages/kit/date-range/examples/4-range-separator/component.ts @@ -0,0 +1,28 @@ +import {ChangeDetectionStrategy, Component} from '@angular/core'; + +import mask from './mask'; + +@Component({ + selector: 'date-range-mask-doc-example-4', + template: ` + + + + `, + changeDetection: ChangeDetectionStrategy.OnPush, +}) +export class DateRangeMaskDocExample4 { + value = '01.01.2023 ~ 05.01.2023'; + readonly filler = 'dd.mm.yyyy ~ dd.mm.yyyy'; + readonly mask = mask; +} diff --git a/projects/demo/src/pages/kit/date-range/examples/4-range-separator/mask.ts b/projects/demo/src/pages/kit/date-range/examples/4-range-separator/mask.ts new file mode 100644 index 000000000..adddbd445 --- /dev/null +++ b/projects/demo/src/pages/kit/date-range/examples/4-range-separator/mask.ts @@ -0,0 +1,6 @@ +import {maskitoDateRangeOptionsGenerator} from '@maskito/kit'; + +export default maskitoDateRangeOptionsGenerator({ + mode: 'dd/mm/yyyy', + rangeSeparator: ' ~ ', +}); diff --git a/projects/kit/src/lib/masks/date-range/date-range-mask.ts b/projects/kit/src/lib/masks/date-range/date-range-mask.ts index c9390e282..6e02dee2c 100644 --- a/projects/kit/src/lib/masks/date-range/date-range-mask.ts +++ b/projects/kit/src/lib/masks/date-range/date-range-mask.ts @@ -10,8 +10,6 @@ import {MaskitoDateMode, MaskitoDateSegments} from '../../types'; import {createMinMaxRangeLengthPostprocessor} from './processors/min-max-range-length-postprocessor'; import {createSwapDatesPostprocessor} from './processors/swap-dates-postprocessor'; -const DATE_RANGE_SEPARATOR = `${CHAR_NO_BREAK_SPACE}${CHAR_EN_DASH}${CHAR_NO_BREAK_SPACE}`; - export function maskitoDateRangeOptionsGenerator({ mode, separator = '.', @@ -19,6 +17,7 @@ export function maskitoDateRangeOptionsGenerator({ max, minLength, maxLength, + rangeSeparator = `${CHAR_NO_BREAK_SPACE}${CHAR_EN_DASH}${CHAR_NO_BREAK_SPACE}`, }: { mode: MaskitoDateMode; separator?: string; @@ -26,6 +25,7 @@ export function maskitoDateRangeOptionsGenerator({ max?: Date; minLength?: Partial>; maxLength?: Partial>; + rangeSeparator?: string; }): Required { const dateModeTemplate = mode.split('/').join(separator); const dateMask = Array.from(dateModeTemplate).map(char => @@ -34,14 +34,14 @@ export function maskitoDateRangeOptionsGenerator({ return { ...MASKITO_DEFAULT_OPTIONS, - mask: [...dateMask, ...Array.from(DATE_RANGE_SEPARATOR), ...dateMask], + mask: [...dateMask, ...Array.from(rangeSeparator), ...dateMask], overwriteMode: 'replace', preprocessors: [ createZeroPlaceholdersPreprocessor(), createValidDatePreprocessor({ dateModeTemplate, dateSegmentsSeparator: separator, - datesSeparator: DATE_RANGE_SEPARATOR, + datesSeparator: rangeSeparator, }), ], postprocessors: [ @@ -49,7 +49,7 @@ export function maskitoDateRangeOptionsGenerator({ min, max, dateModeTemplate, - datesSeparator: DATE_RANGE_SEPARATOR, + datesSeparator: rangeSeparator, dateSegmentSeparator: separator, }), createMinMaxRangeLengthPostprocessor({ @@ -57,11 +57,11 @@ export function maskitoDateRangeOptionsGenerator({ minLength, maxLength, max, - datesSeparator: DATE_RANGE_SEPARATOR, + datesSeparator: rangeSeparator, }), createSwapDatesPostprocessor({ dateModeTemplate, - datesSeparator: DATE_RANGE_SEPARATOR, + datesSeparator: rangeSeparator, }), ], };