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

refactor(ui): Extract InputSearch component #4147

Merged
merged 1 commit into from
Jan 9, 2024
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
8 changes: 4 additions & 4 deletions packages/ui/src/components/bundle-assets/bundle-assets.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import config from '../../config.json';
import I18N from '../../i18n';
import { FlexStack } from '../../layout/flex-stack';
import { Icon } from '../../ui/icon';
import { InputSearch } from '../../ui/input-search';
import { FileName } from '../../ui/file-name';
import { HoverCard } from '../../ui/hover-card';
import { Tag } from '../../ui/tag';
Expand All @@ -25,7 +26,6 @@ import { AssetInfo } from '../asset-info';
import { AssetNotPredictive } from '../asset-not-predictive';
import { ComponentLink } from '../component-link';
import { MetricsTable } from '../metrics-table';
import { MetricsTableSearch } from '../metrics-table-search';
import { MetricsTableOptions } from '../metrics-table-options';
import { MetricsTableTitle } from '../metrics-table-title';
import css from './bundle-assets.module.css';
Expand Down Expand Up @@ -235,11 +235,11 @@ export const BundleAssets = (props) => {
)}
>
<FlexStack space="xxsmall">
<MetricsTableSearch
<InputSearch
className={css.toolbarSearch}
placeholder="Search by name"
search={search}
updateSearch={updateSearch}
defaultValue={search}
onChange={updateSearch}
/>
<Filters
className={css.toolbarFilters}
Expand Down
8 changes: 4 additions & 4 deletions packages/ui/src/components/bundle-modules/bundle-modules.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import config from '../../config.json';
import I18N from '../../i18n';
import { ComponentLink } from '../component-link';
import { FlexStack } from '../../layout/flex-stack';
import { InputSearch } from '../../ui/input-search';
import { EmptySet } from '../../ui/empty-set';
import { FileName } from '../../ui/file-name';
import { Filters } from '../../ui/filters';
import { Tag } from '../../ui/tag';
import { Toolbar } from '../../ui/toolbar';
import { MetricsTable } from '../metrics-table';
import { MetricsTableSearch } from '../metrics-table-search';
import { MetricsTableOptions } from '../metrics-table-options';
import { MetricsTableTitle } from '../metrics-table-title';
import { ModuleInfo } from '../module-info';
Expand Down Expand Up @@ -159,11 +159,11 @@ export const BundleModules = (props: BundleModulesProps) => {
)}
>
<FlexStack space="xxsmall">
<MetricsTableSearch
<InputSearch
className={css.toolbarSearch}
search={search}
updateSearch={updateSearch}
placeholder={I18N_MODULES.SEARCH}
defaultValue={search}
onChange={updateSearch}
/>
<Filters
className={css.tableDropdown}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import { COMPONENT, PACKAGE_FILTERS, PACKAGES_SEPARATOR, SECTIONS } from '@bundl
import config from '../../config.json';
import I18N from '../../i18n';
import { FlexStack } from '../../layout/flex-stack';
import { InputSearch } from '../../ui/input-search';
import { EmptySet } from '../../ui/empty-set';
import { Filters } from '../../ui/filters';
import { Tag } from '../../ui/tag';
import { Toolbar } from '../../ui/toolbar';
import { ComponentLink } from '../component-link';
import { MetricsTable } from '../metrics-table';
import { MetricsTableSearch } from '../metrics-table-search';
import { MetricsTableOptions } from '../metrics-table-options';
import { MetricsTableTitle } from '../metrics-table-title';
import { PackageInfo } from '../package-info';
Expand Down Expand Up @@ -194,11 +194,11 @@ export const BundlePackages = (props) => {
)}
>
<FlexStack space="xxsmall">
<MetricsTableSearch
<InputSearch
className={css.toolbarSearch}
placeholder="Search by name"
search={search}
updateSearch={updateSearch}
defaultValue={search}
onChange={updateSearch}
/>
<Filters
filters={dropdownFilters}
Expand Down
1 change: 0 additions & 1 deletion packages/ui/src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export { Metric } from './metric';
export { MetricRunInfo, MetricRunInfoProps } from './metric-run-info';
export { MetricsTable } from './metrics-table';
export { MetricsTableOptions } from './metrics-table-options';
export { MetricsTableSearch } from './metrics-table-search';
export { MetricsTableTitle } from './metrics-table-title';
export { ModuleInfo } from './module-info';
export { RunInfo, RunInfoProps } from './run-info';
Expand Down
1 change: 0 additions & 1 deletion packages/ui/src/components/metrics-table-search/index.js

This file was deleted.

This file was deleted.

This file was deleted.

1 change: 1 addition & 0 deletions packages/ui/src/ui/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export { HorizontalBarChart } from './horizontal-bar-chart';
export { HoverCard } from './hover-card';
export { Icon } from './icon';
export { Input } from './input';
export { InputSearch } from './input-search';
export { Loader } from './loader';
export { Skeleton } from './skeleton';
export { Table } from './table';
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/ui/input-search/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './input-search';
24 changes: 24 additions & 0 deletions packages/ui/src/ui/input-search/input-search.stories.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React, { useState } from 'react';

import { getWrapperDecorator } from '../../stories';
import { InputSearch } from '.';

export default {
title: 'UI/InputSearch',
component: InputSearch,
decorators: [getWrapperDecorator()],
};

const InputSearchWithState = () => {
const [search, updateSearch] = useState('');

return (
<div>
<pre>{JSON.stringify({ search }, null, 2)}</pre>
<br />
<InputSearch placeholder="Search by name" defaultValue={search} onChange={updateSearch} />
</div>
);
};

export const Default = () => <InputSearchWithState />;
82 changes: 82 additions & 0 deletions packages/ui/src/ui/input-search/input-search.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import React, { useCallback, useEffect, useState } from 'react';
import cx from 'classnames';
import debounce from 'lodash/debounce';
import noop from 'lodash/noop';

import { Icon } from '../icon';
import { Input } from '../input';

import css from './input-search.module.css';

interface InputSearchProps {
placeholder?: string;
defaultValue?: string;
onChange?: (newValue: string) => void;
debounceWait?: number;
}

export const InputSearch = (
props: InputSearchProps & Omit<React.ComponentProps<'div'>, 'onChange'>,
) => {
const {
className = '',
placeholder = 'Search',
defaultValue = '',
onChange = noop,
debounceWait = 500,
} = props;
const rootClassname = cx(css.root, className);

const [value, setValue] = useState(defaultValue);

// Update local state when default value changes
useEffect(() => {
setValue(defaultValue);
}, [defaultValue]);

const debouncedUpdateSearch = useCallback(debounce(onChange, debounceWait), [onChange]);

const changeValue = useCallback(
(newValue: string) => {
if (newValue !== value) {
debouncedUpdateSearch(newValue);
setValue(newValue);
}
},
[setValue, value],
);

const inputOnChange = useCallback(
(event: React.ChangeEvent<HTMLInputElement>) => {
changeValue(event.target.value);
},
[changeValue],
);

const clearButtonOnClick = useCallback(() => {
changeValue('');
}, [changeValue]);

return (
<div className={rootClassname}>
<Input
className={css.input}
placeholder={placeholder}
onChange={inputOnChange}
value={value}
size="small"
autoComplete="off"
/>
{value && (
<button
className={css.cancelButton}
type="button"
onClick={clearButtonOnClick}
aria-label="Clear search"
>
<Icon className={css.cancelButtonIcon} glyph={Icon.ICONS.CANCEL} />
</button>
)}
</div>
);
};