Skip to content

Commit

Permalink
Make date term editable
Browse files Browse the repository at this point in the history
Reviewed By: lblasa

Differential Revision: D49453947

fbshipit-source-id: b6959c6ac74d8966e21fb91f7dcbdf186253b93b
  • Loading branch information
aigoncharov authored and facebook-github-bot committed Sep 20, 2023
1 parent e180a1e commit e031032
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,30 @@
* @format
*/

import {DatePicker, DatePickerProps} from 'antd';
import {Button, DatePicker, DatePickerProps} from 'antd';
import dayjs from 'dayjs';
import React from 'react';
// Use this exact version of moment to match what antd has
// eslint-disable-next-line no-restricted-imports
import moment from 'antd/node_modules/moment';

type PowerSearchAbsoluteTermProps = {
onCancel: () => void;
onChange: (value: Date) => void;
dateOnly?: boolean;
minValue?: Date;
maxValue?: Date;
defaultValue?: Date;
};

export const DATE_ONLY_FORMAT = 'YYYY-MM-DD';
export const DATE_TIME_FORMAT = 'YYYY-MM-DD HH:mm:ss';

export const PowerSearchAbsoluteDateTerm: React.FC<
PowerSearchAbsoluteTermProps
> = ({onCancel, onChange, dateOnly, minValue, maxValue}) => {
> = ({onCancel, onChange, dateOnly, minValue, maxValue, defaultValue}) => {
const [editing, setEditing] = React.useState(!defaultValue);

const disabledDate: DatePickerProps['disabledDate'] = React.useCallback(
(date) => {
if (minValue !== undefined && date < minValue) {
Expand All @@ -40,36 +47,51 @@ export const PowerSearchAbsoluteDateTerm: React.FC<
const format = dateOnly ? 'YYYY-MM-DD' : 'YYYY-MM-DD HH:mm:ss';

const valueRef = React.useRef<Date>();
if (defaultValue && !valueRef.current) {
valueRef.current = defaultValue;
}

return (
<DatePicker
autoFocus
style={{width: 100}}
placeholder="..."
format={format}
onChange={(newValue) => {
if (!newValue) {
onCancel();
return;
}
if (editing) {
return (
<DatePicker
autoFocus
style={{width: 100}}
placeholder="..."
format={format}
onChange={(newValue) => {
if (!newValue) {
onCancel();
return;
}

const newDate = newValue.toDate();
valueRef.current = newDate;
onChange(newDate);
}}
onKeyDown={(event) => {
if (event.key === 'Escape') {
onCancel();
}
}}
onBlur={() => {
if (!valueRef.current) {
onCancel();
}
}}
disabledDate={disabledDate}
showTime={!dateOnly}
defaultOpen
/>
const newDate = newValue.toDate();
valueRef.current = newDate;
onChange(newDate);
}}
onKeyDown={(event) => {
if (event.key === 'Escape') {
onCancel();
}
}}
onBlur={() => {
if (!valueRef.current) {
onCancel();
}
setEditing(false);
}}
disabledDate={disabledDate}
showTime={!dateOnly}
defaultOpen
defaultValue={defaultValue ? moment(defaultValue) : undefined}
/>
);
}

return (
<Button onClick={() => setEditing(true)}>
{dateOnly
? dayjs(defaultValue).format(DATE_ONLY_FORMAT)
: dayjs(defaultValue).format(DATE_TIME_FORMAT)}
</Button>
);
};
19 changes: 14 additions & 5 deletions desktop/flipper-plugin/src/ui/PowerSearch/PowerSearchTerm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ export const PowerSearchTerm: React.FC<PowerSearchTermProps> = ({
minValue={searchTerm.operator.minValue}
maxValue={searchTerm.operator.maxValue}
dateOnly={searchTerm.operator.dateOnly}
defaultValue={searchTerm.searchValue}
/>
);
break;
Expand Down Expand Up @@ -262,11 +263,19 @@ export const PowerSearchTerm: React.FC<PowerSearchTermProps> = ({
}
case 'ABSOLUTE_DATE': {
searchValueComponent = (
<Button>
{searchTerm.operator.dateOnly
? dayjs(searchTerm.searchValue).format(DATE_ONLY_FORMAT)
: dayjs(searchTerm.searchValue).format(DATE_TIME_FORMAT)}
</Button>
<PowerSearchAbsoluteDateTerm
onCancel={onCancel}
onChange={(newValue) => {
onFinalize({
...searchTerm,
searchValue: newValue,
});
}}
minValue={searchTerm.operator.minValue}
maxValue={searchTerm.operator.maxValue}
dateOnly={searchTerm.operator.dateOnly}
defaultValue={searchTerm.searchValue}
/>
);
break;
}
Expand Down

0 comments on commit e031032

Please sign in to comment.