2019-11-06 12:12:41 +00:00
|
|
|
import React from "react";
|
chore: upgrade to prettier v2 + enforce import types (#21013)Co-authored-by: Satish Gandham <hello@satishgandham.com> Co-authored-by: Satish Gandham <satish.iitg@gmail.com>
## Description
This PR upgrades Prettier to v2 + enforces TypeScript’s [`import
type`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export)
syntax where applicable. It’s submitted as a separate PR so we can merge
it easily.
As a part of this PR, we reformat the codebase heavily:
- add `import type` everywhere where it’s required, and
- re-format the code to account for Prettier 2’s breaking changes:
https://prettier.io/blog/2020/03/21/2.0.0.html#breaking-changes
This PR is submitted against `release` to make sure all new code by team
members will adhere to new formatting standards, and we’ll have fewer
conflicts when merging `bundle-optimizations` into `release`. (I’ll
merge `release` back into `bundle-optimizations` once this PR is
merged.)
### Why is this needed?
This PR is needed because, for the Lodash optimization from
https://github.com/appsmithorg/appsmith/commit/7cbb12af886621256224be0c93e6a465dd710ad3,
we need to use `import type`. Otherwise, `babel-plugin-lodash` complains
that `LoDashStatic` is not a lodash function.
However, just using `import type` in the current codebase will give you
this:
<img width="962" alt="Screenshot 2023-03-08 at 17 45 59"
src="https://user-images.githubusercontent.com/2953267/223775744-407afa0c-e8b9-44a1-90f9-b879348da57f.png">
That’s because Prettier 1 can’t parse `import type` at all. To parse it,
we need to upgrade to Prettier 2.
### Why enforce `import type`?
Apart from just enabling `import type` support, this PR enforces
specifying `import type` everywhere it’s needed. (Developers will get
immediate TypeScript and ESLint errors when they forget to do so.)
I’m doing this because I believe `import type` improves DX and makes
refactorings easier.
Let’s say you had a few imports like below. Can you tell which of these
imports will increase the bundle size? (Tip: it’s not all of them!)
```ts
// app/client/src/workers/Linting/utils.ts
import { Position } from "codemirror";
import { LintError as JSHintError, LintOptions } from "jshint";
import { get, isEmpty, isNumber, keys, last, set } from "lodash";
```
It’s pretty hard, right?
What about now?
```ts
// app/client/src/workers/Linting/utils.ts
import type { Position } from "codemirror";
import type { LintError as JSHintError, LintOptions } from "jshint";
import { get, isEmpty, isNumber, keys, last, set } from "lodash";
```
Now, it’s clear that only `lodash` will be bundled.
This helps developers to see which imports are problematic, but it
_also_ helps with refactorings. Now, if you want to see where
`codemirror` is bundled, you can just grep for `import \{.*\} from
"codemirror"` – and you won’t get any type-only imports.
This also helps (some) bundlers. Upon transpiling, TypeScript erases
type-only imports completely. In some environment (not ours), this makes
the bundle smaller, as the bundler doesn’t need to bundle type-only
imports anymore.
## Type of change
- Chore (housekeeping or task changes that don't impact user perception)
## How Has This Been Tested?
This was tested to not break the build.
### Test Plan
> Add Testsmith test cases links that relate to this PR
### Issues raised during DP testing
> Link issues raised during DP testing for better visiblity and tracking
(copy link from comments dropped on this PR)
## Checklist:
### Dev activity
- [x] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] PR is being merged under a feature flag
### QA activity:
- [ ] Test plan has been approved by relevant developers
- [ ] Test plan has been peer reviewed by QA
- [ ] Cypress test cases have been added and approved by either SDET or
manual QA
- [ ] Organized project review call with relevant stakeholders after
Round 1/2 of QA
- [ ] Added Test Plan Approved label after reveiwing all Cypress test
---------
Co-authored-by: Satish Gandham <hello@satishgandham.com>
Co-authored-by: Satish Gandham <satish.iitg@gmail.com>
2023-03-16 11:41:47 +00:00
|
|
|
import type { ControlData, ControlProps } from "./BaseControl";
|
|
|
|
|
import BaseControl from "./BaseControl";
|
2023-07-16 18:49:41 +00:00
|
|
|
import moment from "moment";
|
2020-04-15 11:42:11 +00:00
|
|
|
import { TimePrecision } from "@blueprintjs/datetime";
|
chore: upgrade to prettier v2 + enforce import types (#21013)Co-authored-by: Satish Gandham <hello@satishgandham.com> Co-authored-by: Satish Gandham <satish.iitg@gmail.com>
## Description
This PR upgrades Prettier to v2 + enforces TypeScript’s [`import
type`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export)
syntax where applicable. It’s submitted as a separate PR so we can merge
it easily.
As a part of this PR, we reformat the codebase heavily:
- add `import type` everywhere where it’s required, and
- re-format the code to account for Prettier 2’s breaking changes:
https://prettier.io/blog/2020/03/21/2.0.0.html#breaking-changes
This PR is submitted against `release` to make sure all new code by team
members will adhere to new formatting standards, and we’ll have fewer
conflicts when merging `bundle-optimizations` into `release`. (I’ll
merge `release` back into `bundle-optimizations` once this PR is
merged.)
### Why is this needed?
This PR is needed because, for the Lodash optimization from
https://github.com/appsmithorg/appsmith/commit/7cbb12af886621256224be0c93e6a465dd710ad3,
we need to use `import type`. Otherwise, `babel-plugin-lodash` complains
that `LoDashStatic` is not a lodash function.
However, just using `import type` in the current codebase will give you
this:
<img width="962" alt="Screenshot 2023-03-08 at 17 45 59"
src="https://user-images.githubusercontent.com/2953267/223775744-407afa0c-e8b9-44a1-90f9-b879348da57f.png">
That’s because Prettier 1 can’t parse `import type` at all. To parse it,
we need to upgrade to Prettier 2.
### Why enforce `import type`?
Apart from just enabling `import type` support, this PR enforces
specifying `import type` everywhere it’s needed. (Developers will get
immediate TypeScript and ESLint errors when they forget to do so.)
I’m doing this because I believe `import type` improves DX and makes
refactorings easier.
Let’s say you had a few imports like below. Can you tell which of these
imports will increase the bundle size? (Tip: it’s not all of them!)
```ts
// app/client/src/workers/Linting/utils.ts
import { Position } from "codemirror";
import { LintError as JSHintError, LintOptions } from "jshint";
import { get, isEmpty, isNumber, keys, last, set } from "lodash";
```
It’s pretty hard, right?
What about now?
```ts
// app/client/src/workers/Linting/utils.ts
import type { Position } from "codemirror";
import type { LintError as JSHintError, LintOptions } from "jshint";
import { get, isEmpty, isNumber, keys, last, set } from "lodash";
```
Now, it’s clear that only `lodash` will be bundled.
This helps developers to see which imports are problematic, but it
_also_ helps with refactorings. Now, if you want to see where
`codemirror` is bundled, you can just grep for `import \{.*\} from
"codemirror"` – and you won’t get any type-only imports.
This also helps (some) bundlers. Upon transpiling, TypeScript erases
type-only imports completely. In some environment (not ours), this makes
the bundle smaller, as the bundler doesn’t need to bundle type-only
imports anymore.
## Type of change
- Chore (housekeeping or task changes that don't impact user perception)
## How Has This Been Tested?
This was tested to not break the build.
### Test Plan
> Add Testsmith test cases links that relate to this PR
### Issues raised during DP testing
> Link issues raised during DP testing for better visiblity and tracking
(copy link from comments dropped on this PR)
## Checklist:
### Dev activity
- [x] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] PR is being merged under a feature flag
### QA activity:
- [ ] Test plan has been approved by relevant developers
- [ ] Test plan has been peer reviewed by QA
- [ ] Cypress test cases have been added and approved by either SDET or
manual QA
- [ ] Organized project review call with relevant stakeholders after
Round 1/2 of QA
- [ ] Added Test Plan Approved label after reveiwing all Cypress test
---------
Co-authored-by: Satish Gandham <hello@satishgandham.com>
Co-authored-by: Satish Gandham <satish.iitg@gmail.com>
2023-03-16 11:41:47 +00:00
|
|
|
import type { WidgetProps } from "widgets/BaseWidget";
|
2021-02-02 14:42:49 +00:00
|
|
|
import { ISO_DATE_FORMAT } from "constants/WidgetValidation";
|
2023-05-19 18:37:06 +00:00
|
|
|
import { DatePicker } from "design-system";
|
2022-06-16 09:47:35 +00:00
|
|
|
import { isDynamicValue } from "utils/DynamicBindingUtils";
|
2020-04-15 11:42:11 +00:00
|
|
|
|
2020-05-07 10:51:37 +00:00
|
|
|
class DatePickerControl extends BaseControl<
|
|
|
|
|
DatePickerControlProps,
|
|
|
|
|
DatePickerControlState
|
|
|
|
|
> {
|
2020-06-19 07:51:07 +00:00
|
|
|
now = moment();
|
|
|
|
|
year = this.now.get("year");
|
|
|
|
|
maxDate: Date = this.now
|
|
|
|
|
.clone()
|
2021-05-07 13:08:43 +00:00
|
|
|
.set({ month: 11, date: 31, year: this.year + 100 })
|
2020-06-19 07:51:07 +00:00
|
|
|
.toDate();
|
|
|
|
|
minDate: Date = this.now
|
|
|
|
|
.clone()
|
2022-01-13 08:48:43 +00:00
|
|
|
.set({ month: 0, date: 1, year: this.year - 150 })
|
2020-06-19 07:51:07 +00:00
|
|
|
.toDate();
|
|
|
|
|
|
2022-05-25 10:05:53 +00:00
|
|
|
private wrapperRef = React.createRef<HTMLInputElement>();
|
|
|
|
|
private inputRef = React.createRef<HTMLInputElement>();
|
|
|
|
|
|
2020-05-07 10:51:37 +00:00
|
|
|
constructor(props: DatePickerControlProps) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.state = {
|
|
|
|
|
selectedDate: props.propertyValue,
|
|
|
|
|
};
|
|
|
|
|
}
|
2020-06-19 07:51:07 +00:00
|
|
|
|
2022-05-25 10:05:53 +00:00
|
|
|
componentDidMount() {
|
|
|
|
|
window.addEventListener("keydown", this.handleKeydown);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
|
window.removeEventListener("keydown", this.handleKeydown);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private handleKeydown = (e: KeyboardEvent) => {
|
|
|
|
|
switch (e.key) {
|
|
|
|
|
case "Enter":
|
|
|
|
|
case " ":
|
|
|
|
|
if (document.activeElement === this.wrapperRef?.current) {
|
|
|
|
|
this.inputRef?.current?.focus();
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case "Escape":
|
|
|
|
|
if (document.activeElement === this.inputRef?.current) {
|
|
|
|
|
this.wrapperRef?.current?.focus();
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-11-06 12:12:41 +00:00
|
|
|
render() {
|
2021-02-23 12:35:09 +00:00
|
|
|
const version = this.props.widgetProperties.version;
|
2021-02-02 14:42:49 +00:00
|
|
|
const dateFormat =
|
2021-02-23 12:35:09 +00:00
|
|
|
version === 2
|
|
|
|
|
? ISO_DATE_FORMAT
|
|
|
|
|
: this.props.widgetProperties.dateFormat || ISO_DATE_FORMAT;
|
2021-02-02 14:42:49 +00:00
|
|
|
const isValid = this.state.selectedDate
|
|
|
|
|
? this.validateDate(moment(this.state.selectedDate, dateFormat).toDate())
|
|
|
|
|
: true;
|
2021-02-23 12:35:09 +00:00
|
|
|
const value =
|
|
|
|
|
this.props.propertyValue && isValid
|
|
|
|
|
? version === 2
|
|
|
|
|
? new Date(this.props.propertyValue)
|
|
|
|
|
: this.parseDate(this.props.propertyValue)
|
|
|
|
|
: null;
|
2022-05-25 10:05:53 +00:00
|
|
|
|
2019-11-06 12:12:41 +00:00
|
|
|
return (
|
2023-05-19 18:37:06 +00:00
|
|
|
<div ref={this.wrapperRef} tabIndex={0}>
|
feat: import changes batch 2 (#15722)
* Remove treedropdown from ads
* Change Treedropdown imports
* Remove Notification Banner, change imports
* Remove Toggle from ads
* Change toggle imports
* explicitly declare function argument types
* Remove Menu from ads
* Change menu imports
* Remove Spinner from ads
* Change spinner imports
* Remove Radio, import changes
* test: updated flaky test under default meta (#15707)
* updated flaky test
* Updated tests
* updated tests
* updated the tests
* updated tests
* Update constants.ts
* add more typecasting
* Remove ListSegmentHeader, import changes
* Remove TagInputComponent, import changes
* Remove Switch, import changes
* Remove SearchInput, change imports
* Rename TagInputComponent to TagInput
* Remove ProgressiveImage, import changes
* import changes for SearchVariant
* Remove menu divider, import changes
* Remove TableDropdown, import changes
* Remove Switcher
* Remove StatusBar, import changes
* Remove showcase carousel
* Remove RectangularSwitcher, import change
* Add types to TableDropdown's args
* Remove MultiSwitch, import change
* Remove GifPlayerComponent, import change
* Remove DraggableList, import change
* Remove DisplayImageUpload, import change
* Remove DatePickerComponent, import change
* Remove CopyToClipBoard, import change
* Remove ColorSelector, import change
* Remove TabItemBackgroundFill, NumberedStep, ColorPickerComponent
* GifPlayerComponent -> GifPlayer
* change named import
* Remove FormFieldError, change imports
* Update to new version of Tree Dropdown
* Fix issue with ads/index.ts
* Test file fix
* Fix issue with merge?!?!??
* update design system to 1.0.18
* Bump ds version
* bump ds version
* bump ds version
Co-authored-by: NandanAnantharamu <67676905+NandanAnantharamu@users.noreply.github.com>
Co-authored-by: Albin <albin@appsmith.com>
2022-09-02 08:38:17 +00:00
|
|
|
<DatePicker
|
2021-04-28 10:28:39 +00:00
|
|
|
closeOnSelection
|
2023-05-19 18:37:06 +00:00
|
|
|
dateFormat="yyyy-MM-dd'T'HH:mm:ss z"
|
2020-04-15 11:42:11 +00:00
|
|
|
formatDate={this.formatDate}
|
2022-05-25 10:05:53 +00:00
|
|
|
inputRef={this.inputRef}
|
2021-02-25 12:17:13 +00:00
|
|
|
maxDate={this.maxDate}
|
|
|
|
|
minDate={this.minDate}
|
2021-04-28 10:28:39 +00:00
|
|
|
onChange={this.onDateSelected}
|
|
|
|
|
parseDate={this.parseDate}
|
2023-05-19 18:37:06 +00:00
|
|
|
placeholderText="YYYY-MM-DD HH:mm"
|
|
|
|
|
portalId="date-picker-control"
|
|
|
|
|
selected={value}
|
2021-04-28 10:28:39 +00:00
|
|
|
showActionsBar
|
2023-05-19 18:37:06 +00:00
|
|
|
showTimeInput
|
2022-05-25 10:05:53 +00:00
|
|
|
tabIndex={-1}
|
2020-04-23 06:12:13 +00:00
|
|
|
timePrecision={TimePrecision.MINUTE}
|
2020-04-15 11:42:11 +00:00
|
|
|
/>
|
2023-05-19 18:37:06 +00:00
|
|
|
</div>
|
2019-11-06 12:12:41 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-03 11:30:19 +00:00
|
|
|
getValidDate = (date: string, format: string) => {
|
|
|
|
|
const _date = moment(date, format);
|
|
|
|
|
return _date.isValid() ? _date.toDate() : undefined;
|
|
|
|
|
};
|
|
|
|
|
|
2020-11-27 08:48:38 +00:00
|
|
|
/**
|
|
|
|
|
* here we put the selected state into state
|
|
|
|
|
* before putting it into state, we check if widget date is in range
|
|
|
|
|
* of property value ( min /max range )
|
|
|
|
|
*
|
|
|
|
|
* @param date
|
|
|
|
|
*/
|
2021-07-16 12:26:39 +00:00
|
|
|
onDateSelected = (date: Date | null, isUserChange: boolean): void => {
|
2021-02-02 14:42:49 +00:00
|
|
|
if (isUserChange) {
|
2021-02-23 12:35:09 +00:00
|
|
|
const selectedDate = date
|
|
|
|
|
? this.props.widgetProperties.version === 2
|
|
|
|
|
? date.toISOString()
|
|
|
|
|
: this.formatDate(date)
|
|
|
|
|
: undefined;
|
2021-07-16 12:26:39 +00:00
|
|
|
const isValid = date ? this.validateDate(date) : true;
|
2021-02-02 14:42:49 +00:00
|
|
|
if (!isValid) return;
|
|
|
|
|
// if everything is ok, put date in state
|
|
|
|
|
this.setState({ selectedDate: selectedDate });
|
|
|
|
|
this.updateProperty(this.props.propertyName, selectedDate);
|
|
|
|
|
}
|
2020-04-15 11:42:11 +00:00
|
|
|
};
|
|
|
|
|
|
2020-11-27 08:48:38 +00:00
|
|
|
/**
|
2021-02-23 12:35:09 +00:00
|
|
|
* checks if date is of valid date format
|
2020-11-27 08:48:38 +00:00
|
|
|
*/
|
|
|
|
|
validateDate = (date: Date): boolean => {
|
2021-02-02 14:42:49 +00:00
|
|
|
const dateFormat =
|
2021-02-23 12:35:09 +00:00
|
|
|
this.props.widgetProperties.version === 2
|
|
|
|
|
? ISO_DATE_FORMAT
|
|
|
|
|
: this.props.widgetProperties.dateFormat || ISO_DATE_FORMAT;
|
|
|
|
|
return date ? moment(date, dateFormat).isValid() : true;
|
2020-11-27 08:48:38 +00:00
|
|
|
};
|
|
|
|
|
|
2020-04-15 11:42:11 +00:00
|
|
|
formatDate = (date: Date): string => {
|
2021-02-02 14:42:49 +00:00
|
|
|
const dateFormat =
|
|
|
|
|
this.props.widgetProperties.dateFormat || ISO_DATE_FORMAT;
|
|
|
|
|
return moment(date).format(dateFormat);
|
2020-04-15 11:42:11 +00:00
|
|
|
};
|
|
|
|
|
|
2021-07-16 12:26:39 +00:00
|
|
|
parseDate = (dateStr: string): Date | null => {
|
|
|
|
|
if (!dateStr) {
|
|
|
|
|
return null;
|
|
|
|
|
} else {
|
|
|
|
|
const dateFormat =
|
|
|
|
|
this.props.widgetProperties.version === 2
|
|
|
|
|
? ISO_DATE_FORMAT
|
|
|
|
|
: this.props.widgetProperties.dateFormat || ISO_DATE_FORMAT;
|
|
|
|
|
const date = moment(dateStr, dateFormat);
|
2021-02-03 11:30:19 +00:00
|
|
|
|
2021-07-16 12:26:39 +00:00
|
|
|
if (date.isValid()) return moment(dateStr, dateFormat).toDate();
|
|
|
|
|
else return moment().toDate();
|
|
|
|
|
}
|
2019-11-06 12:12:41 +00:00
|
|
|
};
|
|
|
|
|
|
2022-06-16 09:47:35 +00:00
|
|
|
static canDisplayValueInUI(config: ControlData, value: any): boolean {
|
|
|
|
|
return !isDynamicValue(value);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-14 05:35:16 +00:00
|
|
|
static getControlType() {
|
2019-11-06 12:12:41 +00:00
|
|
|
return "DATE_PICKER";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface DatePickerControlProps extends ControlProps {
|
|
|
|
|
placeholderText: string;
|
2020-04-23 06:12:13 +00:00
|
|
|
propertyValue: string;
|
2020-06-19 07:51:07 +00:00
|
|
|
widgetProperties: WidgetProps;
|
2019-11-06 12:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
2020-05-07 10:51:37 +00:00
|
|
|
interface DatePickerControlState {
|
2020-06-09 13:04:47 +00:00
|
|
|
selectedDate?: string;
|
2020-05-07 10:51:37 +00:00
|
|
|
}
|
|
|
|
|
|
2019-11-06 12:12:41 +00:00
|
|
|
export default DatePickerControl;
|