Skip to content

Commit

Permalink
First iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenthoms committed Oct 24, 2023
1 parent 38ddee3 commit f4860a4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 23 deletions.
56 changes: 33 additions & 23 deletions frontend/src/lib/components/Select/select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import React, { Key } from "react";
import { resolveClassNames } from "@lib/utils/resolveClassNames";
import { getTextWidth } from "@lib/utils/textSize";

Check warning on line 4 in frontend/src/lib/components/Select/select.tsx

View workflow job for this annotation

GitHub Actions / frontend

'getTextWidth' is defined but never used

import { isEqual } from "lodash";

import { BaseComponent, BaseComponentProps } from "../BaseComponent";
import { Input } from "../Input";
import { Virtualization } from "../Virtualization";
Expand Down Expand Up @@ -45,7 +47,7 @@ export const Select = withDefaults<SelectProps>()(defaultProps, (props) => {
const [startIndex, setStartIndex] = React.useState<number>(0);
const [lastShiftIndex, setLastShiftIndex] = React.useState<number>(-1);
const [currentIndex, setCurrentIndex] = React.useState<number>(0);
const [minWidth, setMinWidth] = React.useState<number>(0);
const [prevFilteredOptions, setPrevFilteredOptions] = React.useState<SelectOption[]>([]);

const ref = React.useRef<HTMLDivElement>(null);
const noOptionsText = props.placeholder ?? "No options";
Expand All @@ -56,24 +58,25 @@ export const Select = withDefaults<SelectProps>()(defaultProps, (props) => {
return props.options.filter((option) => option.label.toLowerCase().includes(filter.toLowerCase()));
}, [props.options, filter]);

React.useEffect(() => {
let longestOptionWidth = props.options.reduce((prev, current) => {
const labelWidth = getTextWidth(current.label, document.body);
if (labelWidth > prev) {
return labelWidth;
}
return prev;
}, 0);

if (longestOptionWidth === 0) {
if (props.options.length === 0 || filter === "") {
longestOptionWidth = getTextWidth(noOptionsText, document.body);
} else {
longestOptionWidth = getTextWidth(noMatchingOptionsText, document.body);
if (!isEqual(filteredOptions, prevFilteredOptions)) {
let newCurrentIndex = 0;
let newStartIndex = 0;
const indexOffset = currentIndex - startIndex;
let oldCurrentValue = prevFilteredOptions[currentIndex]?.value;
if (props.value?.length >= 1) {
oldCurrentValue = props.value[0];
}
setPrevFilteredOptions(filteredOptions);
if (oldCurrentValue) {
const newIndex = filteredOptions.findIndex((option) => option.value === oldCurrentValue);
if (newIndex !== -1) {
newCurrentIndex = newIndex;
newStartIndex = Math.max(0, newIndex - indexOffset);
}
}
setMinWidth(longestOptionWidth + 40);
}, [props.options, noOptionsText, filter]);
setCurrentIndex(newCurrentIndex);
setStartIndex(newStartIndex);
}

const toggleValue = React.useCallback(
(option: SelectOption, index: number) => {
Expand Down Expand Up @@ -121,10 +124,12 @@ export const Select = withDefaults<SelectProps>()(defaultProps, (props) => {
const handleKeyDown = (e: KeyboardEvent) => {
setKeysPressed((keysPressed) => [...keysPressed, e.key]);

if (e.key === "Shift") {
console.debug("Shift pressed", currentIndex);
setLastShiftIndex(currentIndex);
}

if (hasFocus) {
if (e.key === "Shift") {
setLastShiftIndex(currentIndex);
}
if (e.key === "ArrowUp") {
e.preventDefault();
const newIndex = Math.max(0, currentIndex - 1);
Expand Down Expand Up @@ -186,6 +191,8 @@ export const Select = withDefaults<SelectProps>()(defaultProps, (props) => {
}
};

console.debug("startIndex", startIndex, "currentIndex", currentIndex);

return (
<BaseComponent disabled={props.disabled}>
<div
Expand All @@ -195,7 +202,7 @@ export const Select = withDefaults<SelectProps>()(defaultProps, (props) => {
"pointer-events-none": props.disabled,
"opacity-30": props.disabled,
})}
style={{ width: props.width, minWidth: props.width ?? minWidth }}
style={{ width: props.width, minWidth: props.width }}
>
{props.filter && (
<Input
Expand Down Expand Up @@ -254,8 +261,11 @@ export const Select = withDefaults<SelectProps>()(defaultProps, (props) => {
style={{ height: 24 }}
>
{option.icon}
<span title={option.label} className="min-w-0 text-ellipsis overflow-hidden whitespace-nowrap">
{option.label}
<span
title={option.label}
className="min-w-0 text-ellipsis overflow-hidden whitespace-nowrap"
>
{option.label}
</span>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const Virtualization = withDefaults<VirtualizationProps>()(defaultProps,
props.direction,
props.startIndex,
props.itemSize,
props.items,
containerSize.height,
containerSize.width,
]);
Expand Down

0 comments on commit f4860a4

Please sign in to comment.