From e031032b9397064e37b05efbcd5736d16470c0b9 Mon Sep 17 00:00:00 2001 From: Andrey Goncharov Date: Wed, 20 Sep 2023 04:36:57 -0700 Subject: [PATCH] Make date term editable Reviewed By: lblasa Differential Revision: D49453947 fbshipit-source-id: b6959c6ac74d8966e21fb91f7dcbdf186253b93b --- .../PowerSearchAbsoluteDateTerm.tsx | 84 ++++++++++++------- .../src/ui/PowerSearch/PowerSearchTerm.tsx | 19 +++-- 2 files changed, 67 insertions(+), 36 deletions(-) diff --git a/desktop/flipper-plugin/src/ui/PowerSearch/PowerSearchAbsoluteDateTerm.tsx b/desktop/flipper-plugin/src/ui/PowerSearch/PowerSearchAbsoluteDateTerm.tsx index c74e511ca0f..3dc427f506a 100644 --- a/desktop/flipper-plugin/src/ui/PowerSearch/PowerSearchAbsoluteDateTerm.tsx +++ b/desktop/flipper-plugin/src/ui/PowerSearch/PowerSearchAbsoluteDateTerm.tsx @@ -7,8 +7,12 @@ * @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; @@ -16,6 +20,7 @@ type PowerSearchAbsoluteTermProps = { dateOnly?: boolean; minValue?: Date; maxValue?: Date; + defaultValue?: Date; }; export const DATE_ONLY_FORMAT = 'YYYY-MM-DD'; @@ -23,7 +28,9 @@ 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) { @@ -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(); + if (defaultValue && !valueRef.current) { + valueRef.current = defaultValue; + } - return ( - { - if (!newValue) { - onCancel(); - return; - } + if (editing) { + return ( + { + 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 ( + ); }; diff --git a/desktop/flipper-plugin/src/ui/PowerSearch/PowerSearchTerm.tsx b/desktop/flipper-plugin/src/ui/PowerSearch/PowerSearchTerm.tsx index 684580be84a..e4c1a47201c 100644 --- a/desktop/flipper-plugin/src/ui/PowerSearch/PowerSearchTerm.tsx +++ b/desktop/flipper-plugin/src/ui/PowerSearch/PowerSearchTerm.tsx @@ -155,6 +155,7 @@ export const PowerSearchTerm: React.FC = ({ minValue={searchTerm.operator.minValue} maxValue={searchTerm.operator.maxValue} dateOnly={searchTerm.operator.dateOnly} + defaultValue={searchTerm.searchValue} /> ); break; @@ -262,11 +263,19 @@ export const PowerSearchTerm: React.FC = ({ } case 'ABSOLUTE_DATE': { searchValueComponent = ( - + { + onFinalize({ + ...searchTerm, + searchValue: newValue, + }); + }} + minValue={searchTerm.operator.minValue} + maxValue={searchTerm.operator.maxValue} + dateOnly={searchTerm.operator.dateOnly} + defaultValue={searchTerm.searchValue} + /> ); break; }