Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
8a72d3a
export typeahead utils
Michele-Masciave Apr 11, 2025
f8dc2b0
integrate placeholder fix and changelog
Michele-Masciave Apr 16, 2025
a857384
adjust typescript
Michele-Masciave Apr 16, 2025
e1267ef
prepare-pr
Michele-Masciave Apr 28, 2025
12211f1
fixed
Michele-Masciave Apr 30, 2025
92e5c21
Merge branch 'main' of https://github.com/Michele-Masciave/react-hook…
Michele-Masciave Apr 30, 2025
a9347da
Merge branch 'neolution-ch:main' into main
Michele-Masciave May 7, 2025
2987413
Merge branch 'neolution-ch:main' into main
Michele-Masciave Jun 3, 2025
acad69e
Merge branch 'main' of https://github.com/Michele-Masciave/react-hook…
Michele-Masciave Jun 13, 2025
de96e5c
Merge branch 'neolution-ch:main' into main
Michele-Masciave Jun 18, 2025
fbfcc8e
Merge branch 'neolution-ch:main' into main
Michele-Masciave Jun 25, 2025
b5c2533
Merge branch 'main' of https://github.com/Michele-Masciave/react-hook…
Michele-Masciave Jul 7, 2025
7843d38
Merge branch 'neolution-ch:main' into main
Michele-Masciave Jul 16, 2025
6b81771
Merge branch 'neolution-ch:main' into main
Michele-Masciave Sep 26, 2025
743acf1
Merge branch 'neolution-ch:main' into main
Michele-Masciave Sep 29, 2025
022db3c
Merge branch 'neolution-ch:main' into main
Michele-Masciave Oct 1, 2025
bbd78a7
Merge branch 'neolution-ch:main' into main
Michele-Masciave Oct 21, 2025
5d28ea3
Merge branch 'neolution-ch:main' into main
Michele-Masciave Dec 3, 2025
3e9f9a7
Merge branch 'neolution-ch:main' into main
Michele-Masciave Dec 10, 2025
814c18e
Merge branch 'neolution-ch:main' into main
Michele-Masciave Dec 15, 2025
fee6b7c
solution
Michele-Masciave Dec 18, 2025
e73fd58
prettier
Michele-Masciave Dec 19, 2025
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- support to specify `innerRef` into `StaticTypeaheadInput` and `AsyncTypeaheadInput`.

## [3.14.0] - 2025-12-10

### Added
Expand Down
35 changes: 35 additions & 0 deletions cypress/cypress/component/Typeahead/AsyncTypeaheadInput.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -929,3 +929,38 @@ it("works with fixed options excluded", () => {
cy.get("input[type=submit]").click({ force: true });
cy.get("@onSubmitSpy").should("be.calledOnceWith", { [name]: [] });
});

it("innerRef works correctly", () => {
const { simpleOptions } = generateOptions();
const name = faker.random.alpha(10);
const options = generateOptions();

const InputWithRef = () => {
const ref = useRef<HTMLInputElement>(null);

return (
<div className="p-4">
<Form
onSubmit={() => {
// Nothing to do
}}
>
<AsyncTypeaheadInput
queryFn={async (query: string) => await fetchMock(options.objectOptions, query, false)}
autocompleteProps={{ openOnFocus: true }}
innerRef={ref}
name={name}
label={name}
/>
<button title="focus" onClick={() => ref.current?.focus()}>
Focus
</button>
</Form>
</div>
);
};

cy.mount(<InputWithRef />);
cy.get("button[title=focus]").click();
cy.get(`#${name}`).should("be.focused");
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { faker, Sex } from "@faker-js/faker";
import * as yup from "yup";
import { yupResolver } from "@hookform/resolvers/yup";
import { generateOptions } from "../../helpers/typeahead";
import { useState } from "react";
import { useRef, useState } from "react";

const selectOption = (name: string, text: string) => {
cy.get(`#${name}`).clear();
Expand Down Expand Up @@ -593,3 +593,31 @@ it("works with fixed options excluded", () => {
cy.get("input[type=submit]").click({ force: true });
cy.get("@onSubmitSpy").should("be.calledOnceWith", { [name]: [] });
});

it("innerRef works correctly", () => {
const { simpleOptions } = generateOptions();
const name = faker.random.alpha(10);

const InputWithRef = () => {
const ref = useRef<HTMLInputElement>(null);

return (
<div className="p-4">
<Form
onSubmit={() => {
// Nothing to do
}}
>
<StaticTypeaheadInput autocompleteProps={{ openOnFocus: true }} innerRef={ref} name={name} label={name} options={simpleOptions} />
<button title="focus" onClick={() => ref.current?.focus()}>
Focus
</button>
</Form>
</div>
);
};

cy.mount(<InputWithRef />);
cy.get("button[title=focus]").click();
cy.get(`#${name}`).should("be.focused");
});
8 changes: 7 additions & 1 deletion src/lib/AsyncTypeaheadInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ interface AsyncTypeaheadInputProps<T extends FieldValues> extends CommonTypeahea
const AsyncTypeaheadInput = <T extends FieldValues>(props: AsyncTypeaheadInputProps<T>) => {
const {
inputRef,
innerRef,
multiple,
disabled,
variant,
Expand Down Expand Up @@ -227,7 +228,12 @@ const AsyncTypeaheadInput = <T extends FieldValues>(props: AsyncTypeaheadInputPr
loadMoreOptions={loadMoreOptions}
setPage={setPage}
{...params}
inputRef={(elem) => ref(elem)}
inputRef={(elem) => {
if (innerRef) {
innerRef.current = elem as HTMLInputElement;
}
ref(elem);
}}
/>
)}
renderTags={createTagRenderer(fixedOptions, autocompleteProps)}
Expand Down
8 changes: 7 additions & 1 deletion src/lib/StaticTypeaheadInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const StaticTypeaheadInput = <T extends FieldValues>(props: StaticTypeaheadInput
autocompleteProps,
fixedOptions,
withFixedOptionsInValue = true,
innerRef,
} = props;

const [page, setPage] = useState(1);
Expand Down Expand Up @@ -192,7 +193,12 @@ const StaticTypeaheadInput = <T extends FieldValues>(props: StaticTypeaheadInput
loadMoreOptions={loadMoreOptions}
setPage={setPage}
{...params}
inputRef={(elem) => ref(elem)}
inputRef={(elem) => {
if (innerRef) {
innerRef.current = elem as HTMLInputElement;
}
ref(elem);
}}
/>
)}
renderTags={createTagRenderer(fixedOptions, autocompleteProps)}
Expand Down
5 changes: 4 additions & 1 deletion src/lib/types/Typeahead.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ReactNode, SyntheticEvent } from "react";
import { MutableRefObject, ReactNode, SyntheticEvent } from "react";
import { FieldValues } from "react-hook-form";
import { CommonInputProps } from "./CommonInputProps";
import { LabelValueOption } from "./LabelValueOption";
Expand All @@ -24,6 +24,7 @@ interface CommonTypeaheadProps<T extends FieldValues>
useBootstrapStyle?: boolean;
fixedOptions?: TypeaheadOptions;
withFixedOptionsInValue?: boolean;
innerRef?: MutableRefObject<HTMLInputElement | null>;
getOptionDisabled?: (option: TypeaheadOption) => boolean;
onChange?: (selected: string | string[]) => void;
onInputChange?: (text: string, reason: AutocompleteInputChangeReason) => void;
Expand Down Expand Up @@ -53,6 +54,7 @@ type AsyncTypeaheadAutocompleteProps = Omit<
| "getOptionDisabled"
| "autoSelect"
| "autoHighlight"
| "ref"
>;

type StaticTypeaheadAutocompleteProps = Omit<
Expand All @@ -76,6 +78,7 @@ type StaticTypeaheadAutocompleteProps = Omit<
| "onChange"
| "autoSelect"
| "autoHighlight"
| "ref"
>;

export { CommonTypeaheadProps, AsyncTypeaheadAutocompleteProps, StaticTypeaheadAutocompleteProps };
Loading