-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(module:time-picker): input value change not work (#5770)
- Loading branch information
Showing
14 changed files
with
340 additions
and
184 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { registerLocaleData } from '@angular/common'; | ||
import zh from '@angular/common/locales/zh'; | ||
import { Injector, LOCALE_ID } from '@angular/core'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { NgTimeParser, TimeResult } from './time-parser'; | ||
|
||
describe('Parse time with angular format', () => { | ||
let injector: Injector; | ||
let localeId: string; | ||
let parser: NgTimeParser; | ||
let result: TimeResult | null; | ||
let time: Date; | ||
|
||
describe('default locale', () => { | ||
beforeEach(() => { | ||
injector = TestBed.configureTestingModule({}); | ||
localeId = injector.get(LOCALE_ID); | ||
}); | ||
|
||
it('should parse hh:mm:ss a', () => { | ||
parser = new NgTimeParser('hh:mm:ss a', localeId); | ||
result = parser.getTimeResult('12:30:22 AM'); | ||
expect(result?.hour).toBe(12); | ||
expect(result?.minute).toBe(30); | ||
expect(result?.second).toBe(22); | ||
expect(result?.period).toBe(0); | ||
|
||
result = parser.getTimeResult('12:30:22 PM'); | ||
expect(result?.period).toBe(1); | ||
|
||
time = parser.toDate('12:30:22 PM'); | ||
expect(time.getHours()).toBe(12); | ||
expect(time.getMinutes()).toBe(30); | ||
expect(time.getSeconds()).toBe(22); | ||
|
||
time = parser.toDate('05:30:22 PM'); | ||
expect(time.getHours()).toBe(17); | ||
}); | ||
|
||
it('should parse hh:mm:ss aaaaa', () => { | ||
parser = new NgTimeParser('hh:mm:ss aaaaa', localeId); | ||
result = parser.getTimeResult('12:30:22 a'); | ||
expect(result?.hour).toBe(12); | ||
expect(result?.minute).toBe(30); | ||
expect(result?.second).toBe(22); | ||
expect(result?.period).toBe(0); | ||
|
||
result = parser.getTimeResult('12:30:22 p'); | ||
expect(result?.period).toBe(1); | ||
}); | ||
|
||
it('should parse mm(ss) HH', () => { | ||
parser = new NgTimeParser('mm(ss) HH', localeId); | ||
result = parser.getTimeResult('30(22) 12'); | ||
expect(result?.period).toBe(null); | ||
|
||
time = parser.toDate('30(22) 12'); | ||
expect(time.getHours()).toBe(12); | ||
expect(time.getMinutes()).toBe(30); | ||
expect(time.getSeconds()).toBe(22); | ||
}); | ||
|
||
it('should parse ss + mm', () => { | ||
parser = new NgTimeParser('ss + mm', localeId); | ||
time = parser.toDate('10 + 42'); | ||
const now = new Date(); | ||
expect(time.getHours()).toBe(now.getHours()); | ||
expect(time.getMinutes()).toBe(42); | ||
expect(time.getSeconds()).toBe(10); | ||
}); | ||
}); | ||
|
||
describe('zh locale', () => { | ||
registerLocaleData(zh); | ||
beforeEach(() => { | ||
injector = TestBed.configureTestingModule({ | ||
providers: [{ provide: LOCALE_ID, useValue: 'zh-CN' }] | ||
}); | ||
localeId = injector.get(LOCALE_ID); | ||
}); | ||
|
||
it('should parse hh:mm:ss a', () => { | ||
parser = new NgTimeParser('hh:mm:ss a', localeId); | ||
result = parser.getTimeResult('04:45:22 上午'); | ||
expect(result?.hour).toBe(4); | ||
expect(result?.minute).toBe(45); | ||
expect(result?.second).toBe(22); | ||
expect(result?.period).toBe(0); | ||
|
||
time = parser.toDate('04:45:22 下午'); | ||
expect(time.getHours()).toBe(16); | ||
}); | ||
}); | ||
}); |
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,143 @@ | ||
/** | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE | ||
*/ | ||
|
||
// from https://github.com/hsuanxyz/ng-time-parser | ||
import { FormStyle, getLocaleDayPeriods, TranslationWidth } from '@angular/common'; | ||
import { isNotNil } from 'ng-zorro-antd/core/util'; | ||
|
||
export interface TimeResult { | ||
hour: number | null; | ||
minute: number | null; | ||
second: number | null; | ||
period: number | null; | ||
} | ||
|
||
export class NgTimeParser { | ||
regex: RegExp = null!; | ||
matchMap: { [key: string]: null | number } = { | ||
hour: null, | ||
minute: null, | ||
second: null, | ||
periodNarrow: null, | ||
periodWide: null, | ||
periodAbbreviated: null | ||
}; | ||
|
||
constructor(private format: string, private localeId: string) { | ||
this.genRegexp(); | ||
} | ||
|
||
toDate(str: string): Date { | ||
const result = this.getTimeResult(str); | ||
const time = new Date(); | ||
|
||
if (isNotNil(result?.hour)) { | ||
time.setHours(result!.hour); | ||
} | ||
|
||
if (isNotNil(result?.minute)) { | ||
time.setMinutes(result!.minute); | ||
} | ||
|
||
if (isNotNil(result?.second)) { | ||
time.setSeconds(result!.second); | ||
} | ||
|
||
if (result?.period === 1 && time.getHours() < 12) { | ||
time.setHours(time.getHours() + 12); | ||
} | ||
|
||
return time; | ||
} | ||
|
||
getTimeResult(str: string): TimeResult | null { | ||
const match = this.regex.exec(str); | ||
let period = null; | ||
if (match) { | ||
if (isNotNil(this.matchMap.periodNarrow)) { | ||
period = getLocaleDayPeriods(this.localeId, FormStyle.Format, TranslationWidth.Narrow).indexOf( | ||
match[this.matchMap.periodNarrow + 1] | ||
); | ||
} | ||
if (isNotNil(this.matchMap.periodWide)) { | ||
period = getLocaleDayPeriods(this.localeId, FormStyle.Format, TranslationWidth.Wide).indexOf(match[this.matchMap.periodWide + 1]); | ||
} | ||
if (isNotNil(this.matchMap.periodAbbreviated)) { | ||
period = getLocaleDayPeriods(this.localeId, FormStyle.Format, TranslationWidth.Abbreviated).indexOf( | ||
match[this.matchMap.periodAbbreviated + 1] | ||
); | ||
} | ||
return { | ||
hour: isNotNil(this.matchMap.hour) ? Number.parseInt(match[this.matchMap.hour + 1], 10) : null, | ||
minute: isNotNil(this.matchMap.minute) ? Number.parseInt(match[this.matchMap.minute + 1], 10) : null, | ||
second: isNotNil(this.matchMap.second) ? Number.parseInt(match[this.matchMap.second + 1], 10) : null, | ||
period | ||
}; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
genRegexp(): void { | ||
let regexStr = this.format.replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$&'); | ||
const hourRegex = /h{1,2}/i; | ||
const minuteRegex = /m{1,2}/; | ||
const secondRegex = /s{1,2}/; | ||
const periodNarrow = /aaaaa/; | ||
const periodWide = /aaaa/; | ||
const periodAbbreviated = /a{1,3}/; | ||
|
||
const hourMatch = hourRegex.exec(this.format); | ||
const minuteMatch = minuteRegex.exec(this.format); | ||
const secondMatch = secondRegex.exec(this.format); | ||
const periodNarrowMatch = periodNarrow.exec(this.format); | ||
let periodWideMatch: null | RegExpExecArray = null; | ||
let periodAbbreviatedMatch: null | RegExpExecArray = null; | ||
if (!periodNarrowMatch) { | ||
periodWideMatch = periodWide.exec(this.format); | ||
} | ||
if (!periodWideMatch && !periodNarrowMatch) { | ||
periodAbbreviatedMatch = periodAbbreviated.exec(this.format); | ||
} | ||
|
||
const matchs = [hourMatch, minuteMatch, secondMatch, periodNarrowMatch, periodWideMatch, periodAbbreviatedMatch] | ||
.filter(m => !!m) | ||
.sort((a, b) => a!.index - b!.index); | ||
|
||
matchs.forEach((match, index) => { | ||
switch (match) { | ||
case hourMatch: | ||
this.matchMap.hour = index; | ||
regexStr = regexStr.replace(hourRegex, '(\\d{1,2})'); | ||
break; | ||
case minuteMatch: | ||
this.matchMap.minute = index; | ||
regexStr = regexStr.replace(minuteRegex, '(\\d{1,2})'); | ||
break; | ||
case secondMatch: | ||
this.matchMap.second = index; | ||
regexStr = regexStr.replace(secondRegex, '(\\d{1,2})'); | ||
break; | ||
case periodNarrowMatch: | ||
this.matchMap.periodNarrow = index; | ||
const periodsNarrow = getLocaleDayPeriods(this.localeId, FormStyle.Format, TranslationWidth.Narrow).join('|'); | ||
regexStr = regexStr.replace(periodNarrow, `(${periodsNarrow})`); | ||
break; | ||
case periodWideMatch: | ||
this.matchMap.periodWide = index; | ||
const periodsWide = getLocaleDayPeriods(this.localeId, FormStyle.Format, TranslationWidth.Wide).join('|'); | ||
regexStr = regexStr.replace(periodWide, `(${periodsWide})`); | ||
break; | ||
case periodAbbreviatedMatch: | ||
this.matchMap.periodAbbreviated = index; | ||
const periodsAbbreviated = getLocaleDayPeriods(this.localeId, FormStyle.Format, TranslationWidth.Abbreviated).join('|'); | ||
regexStr = regexStr.replace(periodAbbreviated, `(${periodsAbbreviated})`); | ||
break; | ||
} | ||
}); | ||
|
||
this.regex = new RegExp(regexStr); | ||
} | ||
} |
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
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
Oops, something went wrong.