Skip to content

Commit 9c7101a

Browse files
authored
Added auto focus for input field (#143)
1 parent c8a9601 commit 9c7101a

4 files changed

Lines changed: 23 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Support for `autoFocus` for `input` fields
13+
1014
### Changed
1115

1216
- move cypress declarations into type file

cypress/cypress/component/Input/BasicInputField.cy.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,4 +476,20 @@ it("minlenght and maxlenght work", () => {
476476
cy.get(`input[name=${name}]`).clear();
477477
cy.get(`input[name=${name}]`).type(invalidInput);
478478
cy.get(`input[name=${name}]`).should("have.value", invalidInput.slice(0, maxLength));
479+
480+
it("is auto focused", () => {
481+
const name = faker.random.word();
482+
483+
cy.mount(
484+
<Form
485+
onSubmit={() => {
486+
// Do nothing
487+
}}
488+
>
489+
<Input name={name} label={name} autoFocus={true} />
490+
</Form>,
491+
);
492+
493+
cy.get(`input[name=${name}]`).should("be.focused");
494+
});
479495
});

src/lib/Input.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ interface InputProps<T extends FieldValues> extends CommonInputProps<T> {
2424
step?: number;
2525
autoComplete?: string;
2626
innerRef?: MutableRefObject<HTMLInputElement | HTMLTextAreaElement | null>;
27+
autoFocus?: boolean;
2728
}
2829

2930
// eslint-disable-next-line complexity

src/lib/components/Input/InputInternal.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const InputInternal = <T extends FieldValues>(props: InputProps<T>) => {
3131
minLength,
3232
maxLength,
3333
autoComplete,
34+
autoFocus,
3435
} = props;
3536
const { name, id } = useSafeNameId(props.name, props.id);
3637
const focusHandler = useMarkOnFocusHandler(markAllOnFocus);
@@ -74,6 +75,7 @@ const InputInternal = <T extends FieldValues>(props: InputProps<T>) => {
7475
placeholder={placeholder}
7576
step={step}
7677
autoComplete={autoComplete}
78+
autoFocus={autoFocus}
7779
{...rest}
7880
{...(value ? { value } : {})}
7981
onBlur={(e) => {

0 commit comments

Comments
 (0)