Skip to content

Commit

Permalink
update filtering into the one input search box for name,ns and type
Browse files Browse the repository at this point in the history
Signed-off-by: R-Lawton <[email protected]>
  • Loading branch information
R-Lawton committed Nov 18, 2024
1 parent a0e4da2 commit 7e9e2a9
Showing 1 changed file with 31 additions and 48 deletions.
79 changes: 31 additions & 48 deletions src/components/ResourceList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
EmptyStateBody,
Title,
Tooltip,
SearchInput,
} from '@patternfly/react-core';
import {
K8sResourceCommon,
Expand Down Expand Up @@ -356,29 +357,23 @@ const ResourceList: React.FC<ResourceListProps> = ({
loadErrors.length > 0 ? new Error(loadErrors.map((err) => err.message).join('; ')) : null;

// Implement local filter state
const [filters, setFilters] = React.useState<{ [key: string]: string }>({});
const [filters, setFilters] = React.useState<string>('');
const [filteredData, setFilteredData] = React.useState<K8sResourceCommon[]>([]);

React.useEffect(() => {
let data = allData;

// Apply filters
Object.keys(filters).forEach((key) => {
const value = filters[key];
if (value) {
data = data.filter((item) => {
if (key === 'name') {
return item.metadata.name.toLowerCase().includes(value.toLowerCase());
} else if (key === 'namespace') {
return item.metadata.namespace?.toLowerCase().includes(value.toLowerCase());
} else if (key === 'type') {
return item.kind.toLowerCase().includes(value.toLowerCase());
}
return true;
});
}
});

if (filters) {
const search = filters.toLowerCase().trim().split(/\s+/); // Split by spaces
data = data.filter((item) => {
return search.every(
(searchValue) =>
item.metadata?.name?.toLowerCase().includes(searchValue) ||
item.metadata?.namespace?.toLowerCase().includes(searchValue) ||
item.kind?.toLowerCase().includes(searchValue),
);
});
}
setFilteredData(data);
}, [allData, filters]);

Expand Down Expand Up @@ -442,6 +437,16 @@ const ResourceList: React.FC<ResourceListProps> = ({
setCurrentPage(1);
};

const handleFilterChange = (value: string) => {
setCurrentPage(1);
setFilters(value);
};

const handleClear = () => {
setFilters('');
setCurrentPage(1);
};

const ResourceRow: React.FC<RowProps<K8sResourceCommon>> = ({ obj, activeColumnIDs }) => {
const { apiVersion, kind } = obj;
const [group, version] = apiVersion.includes('/') ? apiVersion.split('/') : ['', apiVersion];
Expand Down Expand Up @@ -521,36 +526,14 @@ const ResourceList: React.FC<ResourceListProps> = ({
)}
<div className="kuadrant-policy-list-body">
<ListPageBody>
{/* Filter UI */}
<div className="kuadrant-filter-bar" style={{ marginBottom: '16px' }}>
<input
type="text"
placeholder="Filter by name"
value={filters.name || ''}
onChange={(e) => {
setCurrentPage(1);
setFilters({ ...filters, name: e.target.value });
}}
style={{ marginRight: '8px' }}
/>
<input
type="text"
placeholder="Filter by namespace"
value={filters.namespace || ''}
onChange={(e) => {
setCurrentPage(1);
setFilters({ ...filters, namespace: e.target.value });
}}
style={{ marginRight: '8px' }}
/>
<input
type="text"
placeholder="Filter by type"
value={filters.type || ''}
onChange={(e) => {
setCurrentPage(1);
setFilters({ ...filters, type: e.target.value });
}}
<div className="kuadrant-filter-bar" style={{ marginBottom: '16px' , marginTop: '15px' }}>
<SearchInput
placeholder="Search across name, namespace or resource type"
value={String(filters) || ''}
onChange={(_, value: string) => handleFilterChange(value)}
onClear={handleClear}
aria-label="Global search input"
style={{ marginRight: '8px', width: '350px' }}
/>
</div>

Expand Down

0 comments on commit 7e9e2a9

Please sign in to comment.