Skip to content

Commit

Permalink
feat: do not compute open state
Browse files Browse the repository at this point in the history
  • Loading branch information
Jujulego committed Feb 9, 2025
1 parent bf864e6 commit c476d8e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
17 changes: 7 additions & 10 deletions components/search/SearchBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default function SearchBox({ children, sx }: SearchProviderProps) {
const listBoxRef = useRef<HTMLUListElement>(null);

// Open state
const [_isOpen, setIsOpen] = useState(false);
const [isOpen, setIsOpen] = useState(false);

Check warning on line 31 in components/search/SearchBox.tsx

View check run for this annotation

Codecov / codecov/patch

components/search/SearchBox.tsx#L31

Added line #L31 was not covered by tests
const handleOpen = useCallback(() => setIsOpen(true), []);
const handleClose = useCallback(() => setIsOpen(false), []);

Expand Down Expand Up @@ -66,19 +66,20 @@ export default function SearchBox({ children, sx }: SearchProviderProps) {
}, [router]);

// Options
const optionsRef = useRef<OptionsRecord>({});
const optionsRef = useRef(new Map<string, URL>());

Check warning on line 69 in components/search/SearchBox.tsx

View check run for this annotation

Codecov / codecov/patch

components/search/SearchBox.tsx#L69

Added line #L69 was not covered by tests
const [activeOption, setActiveOption] = useState<string | null>(null);
const [isEmpty, setIsEmpty] = useState(true);

const registerOption = useCallback((id: string, target: URL) => {
optionsRef.current[id] = target;
optionsRef.current.set(id, target);

Check warning on line 74 in components/search/SearchBox.tsx

View check run for this annotation

Codecov / codecov/patch

components/search/SearchBox.tsx#L74

Added line #L74 was not covered by tests
setIsEmpty(false);
}, []);

const unregisterOption = useCallback((id: string) => {
delete optionsRef.current[id];
optionsRef.current.delete(id);

Check warning on line 79 in components/search/SearchBox.tsx

View check run for this annotation

Codecov / codecov/patch

components/search/SearchBox.tsx#L79

Added line #L79 was not covered by tests

setActiveOption((old) => old === id ? null : old);
setIsEmpty(Object.keys(optionsRef.current).length === 0);
setIsEmpty(optionsRef.current.size === 0);

Check warning on line 82 in components/search/SearchBox.tsx

View check run for this annotation

Codecov / codecov/patch

components/search/SearchBox.tsx#L82

Added line #L82 was not covered by tests
}, []);

const handleFocusDown = useCallback(() => {
Expand Down Expand Up @@ -131,7 +132,7 @@ export default function SearchBox({ children, sx }: SearchProviderProps) {
}

if (optionId) {
const target = optionsRef.current[optionId];
const target = optionsRef.current.get(optionId);

Check warning on line 135 in components/search/SearchBox.tsx

View check run for this annotation

Codecov / codecov/patch

components/search/SearchBox.tsx#L135

Added line #L135 was not covered by tests

if (target) {
search(target);
Expand All @@ -140,8 +141,6 @@ export default function SearchBox({ children, sx }: SearchProviderProps) {
}, [activeOption, search]);

// Render
const isOpen = _isOpen && !!inputValue;

return (
<SearchSurface
isOpen={isOpen}
Expand Down Expand Up @@ -206,8 +205,6 @@ const SearchListBoxContainer = styled('div')({
});

// Utils
type OptionsRecord = Partial<Record<string, URL>>;

function isEnabledOption(element: Element): boolean {
return element.role === 'option' && element.ariaDisabled !== 'false';
}
Expand Down
6 changes: 4 additions & 2 deletions components/search/SearchComboBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ export function SearchComboBox(props: SearchComboBoxProps) {

const handleChange = useCallback((event: ChangeEvent<HTMLInputElement>) => {
onInputChange(event.currentTarget.value);
}, [onInputChange]);
onOpen();
}, [onInputChange, onOpen]);

Check warning on line 45 in components/search/SearchComboBox.tsx

View check run for this annotation

Codecov / codecov/patch

components/search/SearchComboBox.tsx#L44-L45

Added lines #L44 - L45 were not covered by tests

const handleKeyDown = useCallback((event: KeyboardEvent) => {
switch (event.key) {
Expand Down Expand Up @@ -71,7 +72,8 @@ export function SearchComboBox(props: SearchComboBoxProps) {

const handleClear = useCallback(() => {
onInputChange('');
}, [onInputChange]);
onClose();
}, [onClose, onInputChange]);

Check warning on line 76 in components/search/SearchComboBox.tsx

View check run for this annotation

Codecov / codecov/patch

components/search/SearchComboBox.tsx#L75-L76

Added lines #L75 - L76 were not covered by tests

// Render
return (
Expand Down

0 comments on commit c476d8e

Please sign in to comment.