angular / angular/components

bug(MaterialLuxonAdapter): Parsing format for some text inputs fails due to iso check before format check

Đang mở
#28,703 1 bình luận 0 reaction 0 người được giao Xem trên GitHub
area: material/datepicker P3
Ngôn ngữ chính
TypeScript
Star
25k
Fork
6.8k
Merge trung bình
1 ngày 8 giờ
Pull request đã merge (30 ngày)
91

Mô tả

### Is this a regression?

- No

### The previous version in which this bug was not present was

N/A

### Description

In `/src/material-luxon-adapter/adapter/luxon-date-adapter.ts#L174` the date is formatted with ISO before the user provided formats.

This leads to a bug where specific text inputs are not parsing correctly according to the user provided formats.

For example, I provide the format `MMyyyy` to the parse dateInputs array. This gets ignored for certain string inputs. One example is `082001`. This gets parsed as `yyyyMM` by the ISO check before checking my provided formats array and returns incorrectly as `01/0820` instead of the desired `08/2001`.

```
parse(value: any, parseFormat: string | string[]): LuxonDateTime | null {
const iso8601Date = LuxonDateTime.fromISO(value, options);

if (this.isValid(iso8601Date)) {
return iso8601Date;
}

const formats = Array.isArray(parseFormat) ? parseFormat : [parseFormat];

if (!parseFormat.length) {
throw Error('Formats array must not be empty.');
}

for (const format of formats) {
const fromFormat = LuxonDateTime.fromFormat(value, format, options);

if (this.isValid(fromFormat)) {
return fromFormat;
}
}

...
```

### Reproduction

StackBlitz link: https://stackblitz.com/edit/ocbei5?file=package.json
Steps to reproduce:
1. provide the format `MMyyyy` to the parse dateInputs array
2. type 082001 into the date input with luxon date times and tab off to enter

### Expected Behavior

Gets parsed correctly as 08/2001 and displays as 08/2001.

### Actual Behavior

Gets parsed incorrectly by ISO check as 01/0820 and displays as 01/0820.

### Environment

- Angular: 16.0.4
- CDK/Material:
- "@angular/material": "^16.0.3",
- "@angular/material-luxon-adapter": "^16.2.4",
- Browser(s): Chrome
- Operating System: macOS

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Hướng nghiên cứu

Bắt đầu trong src/material-luxon-adapter/adapter/luxon-date-adapter.ts, tại phương thức parse quanh dòng 174, và sử dụng bản tái hiện StackBlitz được liên kết với định dạng MMyyyy và đầu vào 082001. Kiểm tra thứ tự phân tích cú pháp và xác minh rằng kết quả hiển thị là 08/2001 thay vì 01/0820.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
angular, typescript
Lĩnh vực
frontend
Loại issue
Lỗi
Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
48/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.