feat(date/timepicker): Enhance DateAdapter to accept diffetent types for date and time
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 35/100
- Loại issue
- Tính năng
- Độ rõ ràng
- Cần làm rõ
- Mức độ hoạt động
- Ít trao đổi
- Công nghệ
- angular, typescript
- Lĩnh vực
- frontend
Hướng nghiên cứu
Bắt đầu bằng cách lần theo API DateAdapter đến các điểm vào của date picker và time picker. So sánh các giả định dùng chung hiện tại về ngày/giờ với các kiểu ngày và giờ riêng biệt được yêu cầu, cách xử lý giá trị không hợp lệ và hành vi của addSeconds/setTime. Được xem là hoàn tất khi có thiết kế API tương thích ngược và kế hoạch triển khai hỗ trợ LocalDate/PlainDate cùng với LocalTime/PlainTime mà không cần các phép cast không an toàn.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Feature Description
Currently DateAdapter is designed to work with instances of the same time for date and time, this is how JavaScript Date works and many other replacement libraries do too. Many libraries have noticed this is not always desirable. Java got the external Joda-Time library that became the model for the modern java.time replacement. On the JavaScript side I use js-joda, a port of the original Java Joda-Time library
Much like the new Temporal API, these libraries have aditional split representations of a date and a time, LocalDate and LocalTime and LocalDateTime when you need both without a timezone attached to it.
The Temporal API, brings these kind of types to JavaScript with PlainDate, PlainTime and PlainDateTime (better naming than the Local prefix).
DateAdapter is not designed to have a date picker with a LocalDate/PlainDate and a time picker with a LocalTime/PlainTime. Yes you can make it work but it requires a few hacks, for example the time parts on my js-joda adapter.
isDateInstance(obj: unknown): boolean {
return obj instanceof LocalDate || obj instanceof LocalTime;
}
isValid(date: LocalDate): boolean {
return date !== INVALID_VALUE;
}
invalid(): LocalDate {
return INVALID_VALUE;
}
override compareDate(first: LocalDate, second: LocalDate): number {
return first.compareTo(second);
}
override sameDate(first: LocalDate | null, second: LocalDate | null): boolean {
if (first != null && second != null) {
// WORKAROUND to allow generating the list of times in the time picker
if (first instanceof LocalTime && second instanceof LocalTime) {
return true;
}
return first.equals(second);
}
return (first ?? null) === (second ?? null);
}
override setTime(_target: LocalDate, hours: number, minutes: number, seconds: number): LocalDate {
return LocalTime.of(hours, minutes, seconds) as unknown as LocalDate;
}
override getHours(date: LocalDate): number {
return (date as unknown as LocalTime).hour();
}
override getMinutes(date: LocalDate): number {
return (date as unknown as LocalTime).minute();
}
override getSeconds(date: LocalDate): number {
return (date as unknown as LocalTime).second();
}
override addSeconds(date: LocalDate, amount: number): LocalDate {
const time = date as unknown as LocalTime;
const remainingSeconds = SECONDS_PER_DAY - time.toSecondOfDay();
if (remainingSeconds < amount) {
return INVALID_VALUE;
}
return time.plusSeconds(amount) as unknown as LocalDate;
}
Notice there is a lot of unsafe casting because the subclass extends DateAdapter<LocalDate>, it will have less casting if it was DateAdapter<LocalDate | LocalTime> but that would need more type checks on methods exclusively used by the date picker.
The API changes required are resumed in:
- The adapter should be split in two or have a different type for the time. I prefer to split the adapter, because that way there could be multiple combinations of adapters for PlainDate, PlainTime, Instant (for date and for time pickers) in Temporal API for example.
- js-joda/Temporal API has no concept of an invalid Local/Plain object, their constructors throw exceptions, So I have to hack a fake date
INVALID_VALUEas an specific instance with a very big year, to use as an invalid value. So the logic that requires an invalid value should change, for example the time picker usesisValid()to stop the loop that builds the times to show. addSeconds()andsetTime()should not tie a time to date ever, if possible start with a start time.
Making these changes backward compatible will be a challenge, but I think it is needed to make usable the Temporal API Plain classes with the pickers.
Use Case
No response
- Ngôn ngữ chính
- TypeScript
- Star
- 25k
- Fork
- 6.8k
- Merge trung bình
- 1 ngày 1 giờ
- Pull request đã merge (30 ngày)
- 84
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của angular/components
-
area: material/tree docs gemini-triaged needs triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
angular/components#33832 ·
-
area: material/datepicker gemini-triaged P4
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
angular/components#33763 · 2 bình luận · 3 reaction ·
-
area: material/table gemini-triaged P4
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
angular/components#33709 · 1 bình luận ·
-
area: material/table docs gemini-triaged P4
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
angular/components#33455 ·
-
area: material/core gemini-triaged P3
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 78/100
angular/components#33059 · 2 bình luận ·
Tất cả issue của angular/components
Issue tương tự
-
bug(cli): hapi doctor inline-media prints a fabricated B:\ helper-script path in packaged installs Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
-
Crush Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 85/100
catppuccin/catppuccin#3125 ·
-
Add a SECURITY.md Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
ElementsProject/cln-application#167 · 1 bình luận · 1 reaction ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Quantco/pnpm-licenses#17 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100