-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[datetime2] feat(DateInput3): simpler formatting & parsing API (#6398)
- Loading branch information
Showing
21 changed files
with
545 additions
and
187 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
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,23 @@ | ||
/* | ||
* Copyright 2023 Palantir Technologies, Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
export const TimePrecision = { | ||
MILLISECOND: "millisecond" as "millisecond", | ||
MINUTE: "minute" as "minute", | ||
SECOND: "second" as "second", | ||
}; | ||
// eslint-disable-next-line @typescript-eslint/no-redeclare | ||
export type TimePrecision = (typeof TimePrecision)[keyof typeof TimePrecision]; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2023 Palantir Technologies, Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { format, Locale, parse } from "date-fns"; | ||
|
||
import { DatePickerBaseProps, TimePrecision } from "@blueprintjs/datetime"; | ||
|
||
export const DefaultDateFnsFormats = { | ||
DATE_ONLY: "yyyy-MM-dd", | ||
DATE_TIME_MILLISECONDS: "yyyy-MM-dd HH:mm:ss.SSS", | ||
DATE_TIME_MINUTES: "yyyy-MM-dd HH:mm", | ||
DATE_TIME_SECONDS: "yyyy-MM-dd HH:mm:ss", | ||
}; | ||
|
||
export function getDefaultDateFnsFormat(props: Pick<DatePickerBaseProps, "timePickerProps" | "timePrecision">): string { | ||
const hasTimePickerProps = props.timePickerProps !== undefined && Object.keys(props.timePickerProps).length > 0; | ||
const precision = | ||
props.timePrecision ?? | ||
props.timePickerProps?.precision ?? | ||
// if timePickerProps is non-empty but has no precision defined, use the default value of "minute" | ||
(hasTimePickerProps ? TimePrecision.MINUTE : undefined); | ||
|
||
switch (precision) { | ||
case TimePrecision.MILLISECOND: | ||
return DefaultDateFnsFormats.DATE_TIME_MILLISECONDS; | ||
case TimePrecision.MINUTE: | ||
return DefaultDateFnsFormats.DATE_TIME_MINUTES; | ||
case TimePrecision.SECOND: | ||
return DefaultDateFnsFormats.DATE_TIME_SECONDS; | ||
default: | ||
return DefaultDateFnsFormats.DATE_ONLY; | ||
} | ||
} | ||
|
||
export function getDateFnsFormatter(formatStr: string, locale: Locale | undefined) { | ||
return (date: Date) => format(date, formatStr, { locale }); | ||
} | ||
|
||
export function getDateFnsParser(formatStr: string, locale: Locale | undefined) { | ||
return (str: string) => parse(str, formatStr, new Date(), { locale }); | ||
} |
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.
a4e517f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[datetime2] feat(DateInput3): simpler formatting & parsing API (#6398)
Build artifact links for this commit: documentation | landing | table | demoThis is an automated comment from the deploy-preview CircleCI job.