Skip to content

Commit

Permalink
Add keyboard scroll to inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
tsar-boomba committed Sep 12, 2024
1 parent a919189 commit cb26156
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
2 changes: 2 additions & 0 deletions web/components/atoms/DatePicker.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ReactDatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";
import ErrorText from "./ErrorText";
import keyboardScroll from "@lib/utils/KeyboardScroll";

interface Props {
label: string;
Expand All @@ -22,6 +23,7 @@ export default function DatePicker({
<label htmlFor={label}>{label}</label>
<ReactDatePicker
name={label}
onFocus={(e) => keyboardScroll(e)}
selected={value}
onChange={onChange}
disabled={disabled}
Expand Down
4 changes: 3 additions & 1 deletion web/components/atoms/Dropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import ReactDropdown, { Option, Group } from "react-dropdown";
import "react-dropdown/style.css";
import ErrorText from "./ErrorText";
import keyboardScroll from "@lib/utils/KeyboardScroll";
import { useRef } from "react";

interface Props {
options: (string | Option | Group)[];
Expand Down Expand Up @@ -39,7 +41,7 @@ export default function Dropdown({
placeholder = "Select",
}: Props) {
return (
<div className="flex flex-col">
<div className="flex flex-col" onFocus={(e) => keyboardScroll(e)}>
<label>{label}</label>
<ReactDropdown
options={options}
Expand Down
2 changes: 2 additions & 0 deletions web/components/atoms/TextInput.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { ChangeEvent, useState } from "react";
import ErrorText from "./ErrorText";
import keyboardScroll from "@lib/utils/KeyboardScroll";

interface Props {
label: string;
Expand Down Expand Up @@ -27,6 +28,7 @@ export default function TextInput({
</label>
<input
type="text"
onFocus={(e) => keyboardScroll(e)}
{...formValue}
name={label}
className={`w-full mt-2 py-2.5 px-2 bg-secondary-background items-center border border-light-gray rounded ${disabled ? "!bg-light-gray" : "!bg-secondary-background"} !text-black ${error ? "!border-[#FF3939]" : "!border-light-gray"}`}
Expand Down
9 changes: 9 additions & 0 deletions web/lib/utils/keyboardScroll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { FocusEvent } from "react";

export default function keyboardScroll(e: FocusEvent) {
e.target.scrollIntoView({
behavior: 'smooth',
block: 'center',
inline: 'center'
})
}

0 comments on commit cb26156

Please sign in to comment.