feat: autofocus input when adding

This commit is contained in:
2025-01-26 17:49:32 +00:00
parent df9be06aa6
commit e367182dfa
2 changed files with 26 additions and 4 deletions

View File

@@ -1,4 +1,11 @@
import { createContext, memo, useContext } from "react" import {
type Ref,
createContext,
memo,
useContext,
useEffect,
useRef,
} from "react"
import { create } from "zustand" import { create } from "zustand"
import { useShallow } from "zustand/react/shallow" import { useShallow } from "zustand/react/shallow"
import { Button } from "~/components/button" import { Button } from "~/components/button"
@@ -84,16 +91,24 @@ const StageItem = memo(({ step }: { step: string }) => {
function NewStageInput() { function NewStageInput() {
const isAddingStage = useListItemStore((state) => state.isAddingStage) const isAddingStage = useListItemStore((state) => state.isAddingStage)
const inputRef = useRef<HTMLInputElement | null>(null)
useEffect(() => {
if (isAddingStage) {
inputRef.current?.focus()
}
}, [isAddingStage])
if (isAddingStage) { if (isAddingStage) {
return ( return (
<li className="px-1"> <li className="px-1">
<ActualStageInput /> <ActualStageInput ref={inputRef} />
</li> </li>
) )
} }
return null return null
} }
function ActualStageInput() { function ActualStageInput({ ref }: { ref: Ref<HTMLInputElement> }) {
const entry = useContext(EntryContext) const entry = useContext(EntryContext)
const newStageValue = useListItemStore((state) => state.newStageValue) const newStageValue = useListItemStore((state) => state.newStageValue)
const setNewStageValue = useListItemStore((state) => state.setNewStageValue) const setNewStageValue = useListItemStore((state) => state.setNewStageValue)
@@ -101,6 +116,7 @@ function ActualStageInput() {
return ( return (
<input <input
ref={ref}
value={newStageValue} value={newStageValue}
onChange={(event) => { onChange={(event) => {
setNewStageValue(event.currentTarget.value) setNewStageValue(event.currentTarget.value)

View File

@@ -1,5 +1,5 @@
import clsx from "clsx" import clsx from "clsx"
import { useMemo, useRef, useState } from "react" import { useEffect, useMemo, useRef, useState } from "react"
import Chart from "react-google-charts" import Chart from "react-google-charts"
import { Button } from "~/components/button" import { Button } from "~/components/button"
import { ApplicationList } from "~/home/application-list" import { ApplicationList } from "~/home/application-list"
@@ -90,6 +90,12 @@ function AddApplicationForm() {
const hasEntry = useStore((state) => state.hasEntry) const hasEntry = useStore((state) => state.hasEntry)
const inputRef = useRef<HTMLInputElement | null>(null) const inputRef = useRef<HTMLInputElement | null>(null)
useEffect(() => {
if (isAddingEntry) {
inputRef.current?.focus()
}
}, [isAddingEntry])
function onAddButtonClick() { function onAddButtonClick() {
if (!isAddingEntry) { if (!isAddingEntry) {
setIsAddingEntry(true) setIsAddingEntry(true)