Skip to content

Commit

Permalink
ksearch: adjust id generation to add basename from callers (#471)
Browse files Browse the repository at this point in the history
This PR adjusts our useId function, which generates ids for
html elements, to also take a 'basename' argument from
callers. This is to help reduce the likelihood of an id
made conflicting with an id somewhere else on the page.

J=WAT-4464
TEST=auto

Ran tests. Was having issues getting the test site running

---------

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
Fondryext and github-actions[bot] authored Oct 17, 2024
1 parent 8834ba3 commit 1c102c5
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 8 deletions.
Binary file modified .storybook/snapshots/__snapshots__/mapboxmap--custom-pin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .storybook/snapshots/__snapshots__/mapboxmap--multiple-pins.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .storybook/snapshots/__snapshots__/mapboxmap--primary.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions src/components/Dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export function Dropdown(props: PropsWithChildren<DropdownProps>): JSX.Element {
} = props;

const containerRef = useRef<HTMLDivElement>(null);
const screenReaderUUID = useId();
const screenReaderUUID = useId('dropdown');
const [screenReaderKey, setScreenReaderKey] = useState<number>(0);
const [hasTyped, setHasTyped] = useState<boolean>(false);
const [childrenWithDropdownItemsTransformed, items] = useMemo(() => {
Expand Down Expand Up @@ -295,4 +295,4 @@ function getTransformedChildrenAndItemData(children: ReactNode): [ReactNode, Dro
return createElement(DropdownItemWithIndex, { ...props, index: items.length - 1 });
}));
return [childrenWithDropdownItemsTransformed, items];
}
}
2 changes: 1 addition & 1 deletion src/components/Filters/CheckboxOption.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export function CheckboxOption(props: CheckboxOptionProps): JSX.Element | null {
resultsCount
} = props;
const cssClasses = useComposedCssClasses(builtInCssClasses, props.customCssClasses);
const optionId = useId();
const optionId = useId('facet');
const { selectFilter, filters, applyFilters } = useFiltersContext();

const handleClick = useCallback((checked: boolean) => {
Expand Down
10 changes: 5 additions & 5 deletions src/hooks/useId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { useLayoutEffect } from "./useLayoutEffect";

let serverHandoffComplete = false;
let id = 0;
function genId(): string {
function genId(baseName: string): string {
++id;
return id.toString();
return baseName + '-' + id.toString();
}

// Workaround for https://github.com/webpack/webpack/issues/14814
Expand All @@ -28,14 +28,14 @@ const maybeReactUseId: undefined | (() => string) = (React as any)[
* @see Docs https://reach.tech/auto-id
*/

export function useId(): string {
export function useId(baseName: string): string {
if (maybeReactUseId !== undefined) {
return maybeReactUseId();
}

// If this instance isn't part of the initial render, we don't have to do the
// double render/patch-up dance. We can just generate the ID and return it.
const initialId = (serverHandoffComplete ? genId() : '');
const initialId = (serverHandoffComplete ? genId(baseName) : '');
const [id, setId] = useState(initialId);

useLayoutEffect(() => {
Expand All @@ -44,7 +44,7 @@ export function useId(): string {
// rendering flicker, though it'll make the first render slower (unlikely
// to matter, but you're welcome to measure your app and let us know if
// it's a problem).
setId(genId());
setId(genId(baseName));
}
}, [id]);

Expand Down

0 comments on commit 1c102c5

Please sign in to comment.