Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clicking Label (and Div) element focuses the input field #53

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
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
11 changes: 10 additions & 1 deletion src/components/input/input-field.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import cn from 'clsx';
import { useRef } from 'react';
import type { User, EditableData } from '@lib/types/user';
import type { KeyboardEvent, ChangeEvent } from 'react';
import type { KeyboardEvent, ChangeEvent, RefObject } from 'react';

export type InputFieldProps = {
label: string;
Expand Down Expand Up @@ -28,11 +29,15 @@ export function InputField({
handleChange,
handleKeyboardShortcut
}: InputFieldProps): JSX.Element {
const ref: RefObject<HTMLInputElement & HTMLTextAreaElement> = useRef(null);

const slicedInputValue = inputValue?.slice(0, inputLimit) ?? '';

const inputLength = slicedInputValue.length;
const isHittingInputLimit = inputLimit && inputLength > inputLimit;

const handleRefClicked = (): void => ref.current?.focus();

return (
<div className='flex flex-col gap-1'>
<div
Expand All @@ -43,9 +48,11 @@ export function InputField({
: `ring-light-line-reply focus-within:ring-2
focus-within:!ring-main-accent dark:ring-dark-border`
)}
onClick={handleRefClicked}
>
{useTextArea ? (
<textarea
ref={ref}
className='peer mt-6 w-full resize-none bg-inherit px-3 pb-1
placeholder-transparent outline-none transition'
id={inputId}
Expand All @@ -57,6 +64,7 @@ export function InputField({
/>
) : (
<input
ref={ref}
className='peer mt-6 w-full bg-inherit px-3 pb-1
placeholder-transparent outline-none transition'
id={inputId}
Expand All @@ -78,6 +86,7 @@ export function InputField({
: 'peer-focus:text-main-accent'
)}
htmlFor={inputId}
onClick={handleRefClicked}
>
{label}
</label>
Expand Down
Loading