Skip to content

Commit

Permalink
[dagit] Make log filter input clearable (#7805)
Browse files Browse the repository at this point in the history
  • Loading branch information
hellendag committed May 9, 2022
1 parent 9cf7cde commit 7055f90
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
40 changes: 40 additions & 0 deletions js_modules/dagit/packages/core/src/runs/LogsFilterInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
TextInput,
SuggestionProvider,
useSuggestionsForString,
Icon,
IconWrapper,
} from '@dagster-io/ui';
import Fuse from 'fuse.js';
import * as React from 'react';
Expand Down Expand Up @@ -58,6 +60,7 @@ export const LogsFilterInput: React.FC<Props> = (props) => {

const [state, dispatch] = React.useReducer(reducer, initialState);
const {shown, highlight} = state;
const inputRef = React.useRef<HTMLInputElement>(null);

const {empty, perProvider} = React.useMemo(() => {
const perProvider = suggestionProviders.reduce((accum, provider) => {
Expand Down Expand Up @@ -114,6 +117,12 @@ export const LogsFilterInput: React.FC<Props> = (props) => {
[onChange, onSelectSuggestion],
);

const onClear = React.useCallback(() => {
dispatch({type: 'change-query'});
onChange('');
inputRef.current?.focus();
}, [onChange]);

const onKeyDown = (e: React.KeyboardEvent) => {
const {key} = e;
if (key === 'Escape') {
Expand Down Expand Up @@ -165,10 +174,16 @@ export const LogsFilterInput: React.FC<Props> = (props) => {
spellCheck={false}
autoCorrect="off"
value={value}
ref={inputRef}
onChange={onInputChange}
onFocus={() => dispatch({type: 'show-popover'})}
onBlur={() => dispatch({type: 'hide-popover'})}
onKeyDown={onKeyDown}
rightElement={
<ClearButton onClick={onClear}>
<Icon name="cancel" />
</ClearButton>
}
/>
</Popover>
);
Expand Down Expand Up @@ -202,6 +217,31 @@ const ResultItem: React.FC<{
);
};

const ClearButton = styled.button`
background: transparent;
border: none;
cursor: pointer;
margin: 0 -2px 0 0;
padding: 2px;
${IconWrapper} {
background-color: ${Colors.Gray400};
transition: background-color 100ms linear;
}
:hover ${IconWrapper}, :focus ${IconWrapper} {
background-color: ${Colors.Gray700};
}
:active ${IconWrapper} {
background-color: ${Colors.Dark};
}
:focus {
outline: none;
}
`;

const FilterInput = styled(TextInput)`
width: 300px;
`;
Expand Down
14 changes: 14 additions & 0 deletions js_modules/dagit/packages/ui/src/components/TextInput.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Meta} from '@storybook/react/types-6-0';
import * as React from 'react';

import {Colors} from './Colors';
import {Icon} from './Icon';
import {TextInput} from './TextInput';

// eslint-disable-next-line import/no-default-export
Expand Down Expand Up @@ -45,3 +46,16 @@ export const StrokeColor = () => {
/>
);
};

export const RightElement = () => {
const [value, setValue] = React.useState('');
return (
<TextInput
icon="layers"
placeholder="Type anything…"
value={value}
onChange={(e) => setValue(e.target.value)}
rightElement={<Icon name="info" color={Colors.Gray500} />}
/>
);
};
14 changes: 12 additions & 2 deletions js_modules/dagit/packages/ui/src/components/TextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const TextInput = React.forwardRef(
$hasIcon={!!icon}
type={type}
/>
{rightElement ? rightElement : null}
{rightElement ? <RightContainer>{rightElement}</RightContainer> : null}
</Container>
);
},
Expand All @@ -58,7 +58,7 @@ export const TextInputContainerStyles = css`
const Container = styled.div<{$disabled: boolean}>`
${TextInputContainerStyles}
${IconWrapper}:first-child {
> ${IconWrapper}:first-child {
position: absolute;
left: 8px;
top: 8px;
Expand All @@ -71,6 +71,16 @@ const Container = styled.div<{$disabled: boolean}>`
}
`;

const RightContainer = styled.div`
position: absolute;
bottom: 0;
top: 0;
right: 8px;
display: flex;
flex-direction: column;
justify-content: center;
`;

export const TextInputStyles = css`
border: none;
border-radius: 8px;
Expand Down

1 comment on commit 7055f90

@vercel
Copy link

@vercel vercel bot commented on 7055f90 May 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

dagit-storybook – ./js_modules/dagit/packages/ui

dagit-storybook-elementl.vercel.app
dagit-storybook.vercel.app
dagit-storybook-git-master-elementl.vercel.app

Please sign in to comment.