Skip to content

Commit

Permalink
Merge pull request #1032 from MrMarble/patch-1
Browse files Browse the repository at this point in the history
Make parse/format required if custom pattern is used.
  • Loading branch information
uNmAnNeR authored Apr 22, 2024
2 parents 126ad2a + 5b50692 commit 2ff5a8f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
14 changes: 10 additions & 4 deletions packages/imask/src/masked/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@ type DateOptionsKeys =
export
type DateValue = Date | null;

const DefaultPattern = 'd{.}`m{.}`Y'

// Make format and parse required when pattern is provided
type RequiredDateOptions = ({ pattern?: never | typeof DefaultPattern; format?: MaskedDate["format"]; parse?: MaskedDate["parse"] } |
{ pattern: MaskedDate["pattern"]; format: MaskedDate["format"]; parse: MaskedDate["parse"] })

export
type MaskedDateOptions =
Omit<MaskedPatternOptions<DateValue>, 'mask'> &
Partial<Pick<MaskedDate, DateOptionsKeys>> &
{ mask?: string | DateMaskType }
;
{ mask?: string | DateMaskType } &
RequiredDateOptions;

/** Date mask */
export default
Expand All @@ -52,7 +58,7 @@ class MaskedDate extends MaskedPattern<DateValue> {
static DEFAULTS = {
...MaskedPattern.DEFAULTS,
mask: Date,
pattern: 'd{.}`m{.}`Y',
pattern: DefaultPattern,
format: (date: DateValue, masked: Masked): string => {
if (!date) return '';

Expand Down Expand Up @@ -95,7 +101,7 @@ class MaskedDate extends MaskedPattern<DateValue> {
}));
}

override updateOptions (opts: Partial<MaskedDateOptions>) {
override updateOptions (opts: Partial<MaskedDateOptions> & RequiredDateOptions) {
super.updateOptions(opts as Partial<MaskedPatternOptions<DateValue>>);
}

Expand Down
1 change: 1 addition & 0 deletions packages/imask/test/masked/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('MaskedDate', function () {
describe('#isDateExist', function () {
masked.updateOptions({
pattern: MaskedDate.DEFAULTS.pattern + '.',
parse: MaskedDate.DEFAULTS.parse,
lazy: false,
format: function (date) {
return MaskedDate.DEFAULTS.format(date, masked) + '.';
Expand Down

0 comments on commit 2ff5a8f

Please sign in to comment.