Skip to content

Commit

Permalink
Rework logic using ref objects
Browse files Browse the repository at this point in the history
  • Loading branch information
MrChocolatine committed Oct 13, 2024
1 parent f866800 commit 05590d2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
12 changes: 5 additions & 7 deletions gui/src/renderer/components/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import styled from 'styled-components';

import { colors } from '../../config.json';
import { messages } from '../../shared/gettext';
import { useStyledRef } from '../lib/utilityHooks';
import { useCombinedRefs, useStyledRef } from '../lib/utilityHooks';
import { normalText } from './common-styles';
import ImageView from './ImageView';

Expand Down Expand Up @@ -71,12 +71,12 @@ interface ISearchBarProps {
onSearch: (searchTerm: string) => void;
className?: string;
disableAutoFocus?: boolean;
hasLocationTypeChanged?: boolean;
resetLocationTypeChange?: () => void;
searchInputRef?: React.Ref<HTMLInputElement>;
}

export default function SearchBar(props: ISearchBarProps) {
const inputRef = useStyledRef<HTMLInputElement>();
const combinedRef = useCombinedRefs(inputRef, props.searchInputRef);

const onInput = useCallback(
(event: React.FormEvent) => {
Expand All @@ -95,14 +95,12 @@ export default function SearchBar(props: ISearchBarProps) {
if (!props.disableAutoFocus) {
inputRef.current?.focus({ preventScroll: true });
}

return () => props.resetLocationTypeChange?.();
}, [props.hasLocationTypeChanged]);
}, []);

return (
<StyledSearchContainer className={props.className}>
<StyledSearchInput
ref={inputRef}
ref={combinedRef}
value={props.searchTerm}
onInput={onInput}
placeholder={messages.gettext('Search for...')}
Expand Down
12 changes: 6 additions & 6 deletions gui/src/renderer/components/select-location/SelectLocation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { daitaFilterActive, filterSpecialLocations } from '../../lib/filter-loca
import { useHistory } from '../../lib/history';
import { formatHtml } from '../../lib/html-formatter';
import { RoutePath } from '../../lib/routes';
import { useBoolean, useNormalRelaySettings } from '../../lib/utilityHooks';
import { useNormalRelaySettings, useStyledRef } from '../../lib/utilityHooks';
import { useSelector } from '../../redux/store';
import * as Cell from '../cell';
import { useFilteredProviders } from '../Filter';
Expand Down Expand Up @@ -104,14 +104,15 @@ export default function SelectLocation() {
}
}, [relaySettingsUpdater, resetScrollPositions, relaySettings]);

const [hasLocationTypeChanged, notifyLocationTypeChange, resetLocationTypeChange] =
useBoolean(false);
const searchInputRef = useStyledRef<HTMLInputElement>();

const changeLocationType = useCallback(
(locationType: LocationType) => {
saveScrollPosition();
setLocationType(locationType);
notifyLocationTypeChange();

console.log('### changeLocationType | searchInputRef.current', searchInputRef.current);
searchInputRef.current?.focus();
},
[saveScrollPosition],
);
Expand Down Expand Up @@ -247,8 +248,7 @@ export default function SelectLocation() {
<StyledSearchBar
searchTerm={searchValue}
onSearch={updateSearchTerm}
hasLocationTypeChanged={hasLocationTypeChanged}
resetLocationTypeChange={resetLocationTypeChange}
searchInputRef={searchInputRef}
/>
</StyledNavigationBarAttachment>

Expand Down

0 comments on commit 05590d2

Please sign in to comment.