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

[v15] Add filter to cluster dropdown #48800

Merged
merged 1 commit into from
Nov 12, 2024
Merged
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
96 changes: 84 additions & 12 deletions web/packages/shared/components/ClusterDropdown/ClusterDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
*/

import React, { useState, useEffect } from 'react';
import styled from 'styled-components';
import { useHistory } from 'react-router';
import { ButtonSecondary, Flex, Menu, MenuItem, Text } from 'design';
import { Box, ButtonSecondary, Flex, Menu, MenuItem, Text } from 'design';
import { ChevronDown } from 'design/Icon';
import cfg from 'teleport/config';
import { Cluster } from 'teleport/services/clusters';
Expand Down Expand Up @@ -66,6 +67,8 @@ export function ClusterDropdown({
const [options, setOptions] = React.useState<Option[]>(
createOptions(initialClusters)
);
const showInput = options.length > 5 ? true : false;
const [clusterFilter, setClusterFilter] = useState('');
const history = useHistory();
const [anchorEl, setAnchorEl] = useState(null);

Expand Down Expand Up @@ -133,6 +136,17 @@ export function ClusterDropdown({
return null;
}

const onClusterFilterChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setClusterFilter(e.target.value);
};

let filteredOptions = options;
if (clusterFilter) {
filteredOptions = options.filter(cluster =>
cluster.label.toLowerCase().includes(clusterFilter.toLowerCase())
);
}

return (
<Flex textAlign="center" alignItems="center" mb={mb}>
<HoverTooltip tipContent={'Select cluster'}>
Expand All @@ -150,7 +164,11 @@ export function ClusterDropdown({
</ButtonSecondary>
</HoverTooltip>
<Menu
popoverCss={() => `margin-top: 36px;`}
popoverCss={() => `
margin-top: ${showInput ? '40px' : '4px'};
max-height: 265px;
overflow: hidden;
`}
transformOrigin={{
vertical: 'top',
horizontal: 'left',
Expand All @@ -163,20 +181,74 @@ export function ClusterDropdown({
open={Boolean(anchorEl)}
onClose={handleClose}
>
{options.map(cluster => (
<MenuItem
px={2}
key={cluster.value}
onClick={() => onChangeOption(cluster.value)}
{showInput ? (
<Box
css={`
padding: ${p => p.theme.space[2]}px;
`}
>
<Text ml={2} fontWeight={300} fontSize={2}>
{cluster.label}
</Text>
</MenuItem>
))}
<ClusterFilter
type="text"
autoFocus
value={clusterFilter}
autoComplete="off"
onChange={onClusterFilterChange}
placeholder={'Search clusters…'}
/>
</Box>
) : (
// without this empty box, the entire positioning is way out of whack
// TODO (avatus): find out why during menu/popover rework
<Box />
)}
<Box
css={`
max-height: 220px;
overflow: auto;
`}
>
{filteredOptions.map(cluster => (
<MenuItem
px={2}
key={cluster.value}
onClick={() => onChangeOption(cluster.value)}
>
<Text
ml={2}
fontWeight={cluster.value === clusterId ? 500 : 300}
fontSize={2}
>
{cluster.label}
</Text>
</MenuItem>
))}
</Box>
</Menu>
</Flex>
);
}

type Option = { value: string; label: string };

const ClusterFilter = styled.input(
({ theme }) => `
background-color: ${theme.colors.spotBackground[0]};
padding-left: ${theme.space[3]}px;
width: 100%;
border-radius: 29px;
box-sizing: border-box;
color: ${theme.colors.text.main};
height: 32px;
font-size: ${theme.fontSizes[1]}px;
outline: none;
border: none;
&:focus {
border: none;
}

::placeholder {
color: ${theme.colors.text.muted};
opacity: 1;
}
`
);
Loading