Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docs-app] feat(DateInput2Example): use date-fns formatter #5371

Merged
merged 2 commits into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/datetime2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
"@blueprintjs/popover2": "^1.4.0",
"@blueprintjs/select": "^4.4.0",
"classnames": "^2.2",
"date-fns": "2.22.1",
"date-fns-tz": "1.3.4",
"date-fns": "^2.28.0",
"date-fns-tz": "^1.3.4",
"lodash-es": "^4.17.15",
"tslib": "~2.3.1"
},
Expand Down
1 change: 1 addition & 0 deletions packages/docs-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"chroma-js": "^2.1.0",
"classnames": "^2.2",
"core-js": "^3.21.1",
"date-fns": "^2.28.0",
"dom4": "^2.1.5",
"downloadjs": "^1.4.7",
"lodash-es": "^4.17.15",
Expand Down
45 changes: 45 additions & 0 deletions packages/docs-app/src/common/dateFormatSelector.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2022 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 * as React from "react";

import { Radio, RadioGroup } from "@blueprintjs/core";
import { DateFormatProps } from "@blueprintjs/datetime";
import { handleNumberChange } from "@blueprintjs/docs-theme";

export interface DateFormatSelectorProps {
/** Format options */
formatOptions: DateFormatProps[];

/** Selected formatter. */
format: DateFormatProps;

/** The callback to fire when a new formatter is chosen. */
onChange: (format: DateFormatProps) => void;
}

export const DateFormatSelector: React.FC<DateFormatSelectorProps> = props => {
const handleChange = handleNumberChange(index => props.onChange(props.formatOptions[index]));
const value = props.formatOptions.indexOf(props.format);

return (
<RadioGroup label="Date format" onChange={handleChange} selectedValue={value}>
{props.formatOptions.map((format, index) => (
<Radio key={index} label={format.placeholder} value={index} />
))}
</RadioGroup>
);
};

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2017 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 moment from "moment";
import * as React from "react";

import { DateFormatProps } from "@blueprintjs/datetime";

import { DateFormatSelector, DateFormatSelectorProps } from "../../../common/dateFormatSelector";

export const MomentFormatSelector: React.FC<Omit<DateFormatSelectorProps, "formatOptions">> = props => {
return <DateFormatSelector formatOptions={MOMENT_FORMATS} {...props} />;
};

export const MOMENT_FORMATS: DateFormatProps[] = [
{
formatDate: date => date?.toLocaleDateString() ?? "",
parseDate: str => new Date(Date.parse(str)),
placeholder: "JS Date",
},
getMomentFormatter("MM/DD/YYYY"),
getMomentFormatter("YYYY-MM-DD"),
getMomentFormatter("YYYY-MM-DD HH:mm:ss"),
];

function getMomentFormatter(format: string): DateFormatProps {
return {
formatDate: date => moment(date).format(format),
parseDate: str => moment(str, format).toDate(),
placeholder: `${format} (moment)`,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import * as React from "react";
import { Classes, HTMLSelect } from "@blueprintjs/core";
import { TimePrecision } from "@blueprintjs/datetime";

export interface IPrecisionSelectProps {
export interface PrecisionSelectProps {
/**
* The precision-string option to display as selected.
*/
Expand All @@ -43,9 +43,9 @@ export interface IPrecisionSelectProps {
label?: string;
}

export const PrecisionSelect: React.FC<IPrecisionSelectProps> = props => (
export const PrecisionSelect: React.FC<PrecisionSelectProps> = props => (
<label className={Classes.LABEL}>
{props.label || "Precision"}
{props.label ?? "Precision"}
<HTMLSelect value={props.value} onChange={props.onChange}>
{props.allowNone && <option value="none">None</option>}
<option value={TimePrecision.MINUTE}>Minute</option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import { H5, Position, Switch } from "@blueprintjs/core";
import { DateFormatProps, DateInput, TimePrecision } from "@blueprintjs/datetime";
import { Example, handleBooleanChange, handleValueChange, IExampleProps } from "@blueprintjs/docs-theme";

import { FORMATS, FormatSelect } from "./common/formatSelect";
import { MomentDate } from "./common/momentDate";
import { MOMENT_FORMATS, MomentFormatSelector } from "./common/momentFormats";
import { PrecisionSelect } from "./common/precisionSelect";

export interface IDateInputExampleState {
Expand All @@ -42,7 +42,7 @@ export class DateInputExample extends React.PureComponent<IExampleProps, IDateIn
date: null,
disabled: false,
fill: false,
format: FORMATS[0],
format: MOMENT_FORMATS[0],
reverseMonthAndYearMenus: false,
shortcuts: false,
showTimeArrowButtons: false,
Expand All @@ -57,7 +57,9 @@ export class DateInputExample extends React.PureComponent<IExampleProps, IDateIn

private toggleFill = handleBooleanChange(fill => this.setState({ fill }));

private toggleReverseMenus = handleBooleanChange(reverse => this.setState({ reverseMonthAndYearMenus: reverse }));
private toggleReverseMenus = handleBooleanChange(reverseMonthAndYearMenus =>
this.setState({ reverseMonthAndYearMenus }),
);

private toggleTimePrecision = handleValueChange((timePrecision: TimePrecision | "none") =>
this.setState({ timePrecision: timePrecision === "none" ? undefined : timePrecision }),
Expand Down Expand Up @@ -93,7 +95,7 @@ export class DateInputExample extends React.PureComponent<IExampleProps, IDateIn
closeOnSelection,
disabled,
fill,
reverseMonthAndYearMenus: reverse,
reverseMonthAndYearMenus,
format,
timePrecision,
shortcuts,
Expand All @@ -106,8 +108,12 @@ export class DateInputExample extends React.PureComponent<IExampleProps, IDateIn
<Switch checked={shortcuts} label="Show shortcuts" onChange={this.toggleShortcuts} />
<Switch label="Disabled" checked={disabled} onChange={this.toggleDisabled} />
<Switch label="Fill" checked={fill} onChange={this.toggleFill} />
<Switch label="Reverse month and year menus" checked={reverse} onChange={this.toggleReverseMenus} />
<FormatSelect format={format} onChange={this.handleFormatChange} />
<Switch
label="Reverse month and year menus"
checked={reverseMonthAndYearMenus}
onChange={this.toggleReverseMenus}
/>
<MomentFormatSelector format={format} onChange={this.handleFormatChange} />
<PrecisionSelect
allowNone={true}
label="Time precision"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import { H5, Switch } from "@blueprintjs/core";
import { DateFormatProps, DateRange, DateRangeInput, TimePrecision } from "@blueprintjs/datetime";
import { Example, handleBooleanChange, IExampleProps } from "@blueprintjs/docs-theme";

import { FORMATS, FormatSelect } from "./common/formatSelect";
import { MomentDateRange } from "./common/momentDate";
import { MOMENT_FORMATS, MomentFormatSelector } from "./common/momentFormats";

export interface IDateRangeInputExampleState {
allowSingleDayRange: boolean;
Expand All @@ -45,7 +45,7 @@ export class DateRangeInputExample extends React.PureComponent<IExampleProps, ID
contiguousCalendarMonths: true,
disabled: false,
enableTimePicker: false,
format: FORMATS[0],
format: MOMENT_FORMATS[0],
range: [null, null],
reverseMonthAndYearMenus: false,
selectAllOnFocus: false,
Expand Down Expand Up @@ -146,7 +146,7 @@ export class DateRangeInputExample extends React.PureComponent<IExampleProps, ID
label="Show timepicker arrow buttons"
onChange={this.toggleTimepickerArrowButtons}
/>
<FormatSelect key="Format" format={this.state.format} onChange={this.handleFormatChange} />
<MomentFormatSelector key="Format" format={this.state.format} onChange={this.handleFormatChange} />
</>
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2022 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 * as React from "react";

import { DateFormatProps } from "@blueprintjs/datetime";

import { DateFormatSelector, DateFormatSelectorProps } from "../../common/dateFormatSelector";

// eslint-disable-next-line @typescript-eslint/no-var-requires
const locales: { [localeCode: string]: Locale } = require("date-fns/locale");

export const DateFnsFormatSelector: React.FC<Omit<DateFormatSelectorProps, "formatOptions">> = props => {
return <DateFormatSelector formatOptions={DATE_FNS_FORMATS} {...props} />;
};

export const DATE_FNS_FORMATS: DateFormatProps[] = [
getDateFnsFormatter("MM/dd/yyyy"),
getDateFnsFormatter("yyyy-MM-dd"),
getDateFnsFormatter("yyyy-MM-dd HH:mm:ss"),
getDateFnsFormatter("LLL do, yyyy 'at' K:mm a"),
];

function getDateFnsFormatter(formatStr: string): DateFormatProps {
return {
formatDate: (date, localeCode) => format(date, formatStr, maybeGetLocaleOptions(localeCode)),
parseDate: (str, localeCode) => parse(str, formatStr, new Date(), maybeGetLocaleOptions(localeCode)),
placeholder: `${formatStr} (date-fns)`,
};
}

function maybeGetLocaleOptions(localeCode: string): { locale: Locale } | undefined {
if (locales[localeCode] !== undefined) {
return { locale: locales[localeCode] };
}
return undefined;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import { DateFormatProps, TimePrecision } from "@blueprintjs/datetime";
import { DateInput2 } from "@blueprintjs/datetime2";
import { Example, handleBooleanChange, handleValueChange, IExampleProps } from "@blueprintjs/docs-theme";

import { FORMATS, FormatSelect } from "../datetime-examples/common/formatSelect";
import { PrecisionSelect } from "../datetime-examples/common/precisionSelect";
import { DATE_FNS_FORMATS, DateFnsFormatSelector } from "./dateFnsFormatSelector";

export interface DateInput2ExampleState {
closeOnSelection: boolean;
Expand All @@ -44,7 +44,7 @@ export class DateInput2Example extends React.PureComponent<IExampleProps, DateIn
disableTimezoneSelect: false,
disabled: false,
fill: false,
format: FORMATS[0],
format: DATE_FNS_FORMATS[0],
reverseMonthAndYearMenus: false,
shortcuts: false,
showTimezoneSelect: true,
Expand Down Expand Up @@ -116,7 +116,7 @@ export class DateInput2Example extends React.PureComponent<IExampleProps, DateIn
<Switch label="Disabled" checked={disabled} onChange={this.toggleDisabled} />
<Switch label="Fill" checked={fill} onChange={this.toggleFill} />
<Switch label="Reverse month and year menus" checked={reverse} onChange={this.toggleReverseMenus} />
<FormatSelect format={format} onChange={this.handleFormatChange} />
<DateFnsFormatSelector format={format} onChange={this.handleFormatChange} />

<H5>Timezone props</H5>
<Switch
Expand Down
10 changes: 10 additions & 0 deletions packages/docs-app/src/styles/_examples.scss
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,16 @@
}
}

//
// DATETIME2
//

#{example("DateInput2Example")} {
.#{$ns}-input-group {
min-width: 300px;
}
}

//
// TABLE
//
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-config/tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"no-submodule-imports": {
"options": [
"core-js",
"date-fns",
"react-dom",
"@blueprintjs/table/src",
"@blueprintjs/test-commons/bootstrap",
Expand Down
1 change: 1 addition & 0 deletions packages/tslint-config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ module.exports = {
"no-submodule-imports": {
options: [
"core-js",
"date-fns",
"lodash",
"react-dom",
"@blueprintjs/table/src",
Expand Down
Loading