Skip to content

Commit

Permalink
improve focus util
Browse files Browse the repository at this point in the history
  • Loading branch information
kentcdodds committed Jul 25, 2023
1 parent 6d7c504 commit 3d6e494
Show file tree
Hide file tree
Showing 11 changed files with 0 additions and 253 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useFormAction, useNavigation } from '@remix-run/react'
import { clsx, type ClassValue } from 'clsx'
import { useEffect } from 'react'
import { twMerge } from 'tailwind-merge'

/**
Expand Down Expand Up @@ -78,25 +77,3 @@ export function useIsSubmitting({
navigation.formMethod === formMethod
)
}

/**
* A hook that focuses the first invalid element in a form.
*/
export function useFocusInvalid(
formEl: HTMLFormElement | null,
hasErrors: boolean,
) {
useEffect(() => {
if (!formEl) return
if (!hasErrors) return

if (formEl.matches('[aria-invalid="true"]')) {
formEl.focus()
} else {
const firstInvalid = formEl.querySelector('[aria-invalid="true"]')
if (firstInvalid instanceof HTMLElement) {
firstInvalid.focus()
}
}
}, [formEl, hasErrors])
}
23 changes: 0 additions & 23 deletions exercises/04.file-upload/01.problem.multi-part/app/utils/misc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useFormAction, useNavigation } from '@remix-run/react'
import { clsx, type ClassValue } from 'clsx'
import { useEffect } from 'react'
import { twMerge } from 'tailwind-merge'

/**
Expand Down Expand Up @@ -78,25 +77,3 @@ export function useIsSubmitting({
navigation.formMethod === formMethod
)
}

/**
* A hook that focuses the first invalid element in a form.
*/
export function useFocusInvalid(
formEl: HTMLFormElement | null,
hasErrors: boolean,
) {
useEffect(() => {
if (!formEl) return
if (!hasErrors) return

if (formEl.matches('[aria-invalid="true"]')) {
formEl.focus()
} else {
const firstInvalid = formEl.querySelector('[aria-invalid="true"]')
if (firstInvalid instanceof HTMLElement) {
firstInvalid.focus()
}
}
}, [formEl, hasErrors])
}
23 changes: 0 additions & 23 deletions exercises/04.file-upload/01.solution.multi-part/app/utils/misc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useFormAction, useNavigation } from '@remix-run/react'
import { clsx, type ClassValue } from 'clsx'
import { useEffect } from 'react'
import { twMerge } from 'tailwind-merge'

/**
Expand Down Expand Up @@ -78,25 +77,3 @@ export function useIsSubmitting({
navigation.formMethod === formMethod
)
}

/**
* A hook that focuses the first invalid element in a form.
*/
export function useFocusInvalid(
formEl: HTMLFormElement | null,
hasErrors: boolean,
) {
useEffect(() => {
if (!formEl) return
if (!hasErrors) return

if (formEl.matches('[aria-invalid="true"]')) {
formEl.focus()
} else {
const firstInvalid = formEl.querySelector('[aria-invalid="true"]')
if (firstInvalid instanceof HTMLElement) {
firstInvalid.focus()
}
}
}, [formEl, hasErrors])
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useFormAction, useNavigation } from '@remix-run/react'
import { clsx, type ClassValue } from 'clsx'
import { useEffect } from 'react'
import { twMerge } from 'tailwind-merge'

/**
Expand Down Expand Up @@ -78,25 +77,3 @@ export function useIsSubmitting({
navigation.formMethod === formMethod
)
}

/**
* A hook that focuses the first invalid element in a form.
*/
export function useFocusInvalid(
formEl: HTMLFormElement | null,
hasErrors: boolean,
) {
useEffect(() => {
if (!formEl) return
if (!hasErrors) return

if (formEl.matches('[aria-invalid="true"]')) {
formEl.focus()
} else {
const firstInvalid = formEl.querySelector('[aria-invalid="true"]')
if (firstInvalid instanceof HTMLElement) {
firstInvalid.focus()
}
}
}, [formEl, hasErrors])
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useFormAction, useNavigation } from '@remix-run/react'
import { clsx, type ClassValue } from 'clsx'
import { useEffect } from 'react'
import { twMerge } from 'tailwind-merge'

/**
Expand Down Expand Up @@ -78,25 +77,3 @@ export function useIsSubmitting({
navigation.formMethod === formMethod
)
}

/**
* A hook that focuses the first invalid element in a form.
*/
export function useFocusInvalid(
formEl: HTMLFormElement | null,
hasErrors: boolean,
) {
useEffect(() => {
if (!formEl) return
if (!hasErrors) return

if (formEl.matches('[aria-invalid="true"]')) {
formEl.focus()
} else {
const firstInvalid = formEl.querySelector('[aria-invalid="true"]')
if (firstInvalid instanceof HTMLElement) {
firstInvalid.focus()
}
}
}, [formEl, hasErrors])
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useFormAction, useNavigation } from '@remix-run/react'
import { clsx, type ClassValue } from 'clsx'
import { useEffect } from 'react'
import { twMerge } from 'tailwind-merge'

/**
Expand Down Expand Up @@ -78,25 +77,3 @@ export function useIsSubmitting({
navigation.formMethod === formMethod
)
}

/**
* A hook that focuses the first invalid element in a form.
*/
export function useFocusInvalid(
formEl: HTMLFormElement | null,
hasErrors: boolean,
) {
useEffect(() => {
if (!formEl) return
if (!hasErrors) return

if (formEl.matches('[aria-invalid="true"]')) {
formEl.focus()
} else {
const firstInvalid = formEl.querySelector('[aria-invalid="true"]')
if (firstInvalid instanceof HTMLElement) {
firstInvalid.focus()
}
}
}, [formEl, hasErrors])
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useFormAction, useNavigation } from '@remix-run/react'
import { clsx, type ClassValue } from 'clsx'
import { useEffect } from 'react'
import { twMerge } from 'tailwind-merge'

/**
Expand Down Expand Up @@ -78,25 +77,3 @@ export function useIsSubmitting({
navigation.formMethod === formMethod
)
}

/**
* A hook that focuses the first invalid element in a form.
*/
export function useFocusInvalid(
formEl: HTMLFormElement | null,
hasErrors: boolean,
) {
useEffect(() => {
if (!formEl) return
if (!hasErrors) return

if (formEl.matches('[aria-invalid="true"]')) {
formEl.focus()
} else {
const firstInvalid = formEl.querySelector('[aria-invalid="true"]')
if (firstInvalid instanceof HTMLElement) {
firstInvalid.focus()
}
}
}, [formEl, hasErrors])
}
23 changes: 0 additions & 23 deletions exercises/05.complex-structures/02.problem.lists/app/utils/misc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useFormAction, useNavigation } from '@remix-run/react'
import { clsx, type ClassValue } from 'clsx'
import { useEffect } from 'react'
import { twMerge } from 'tailwind-merge'

/**
Expand Down Expand Up @@ -78,25 +77,3 @@ export function useIsSubmitting({
navigation.formMethod === formMethod
)
}

/**
* A hook that focuses the first invalid element in a form.
*/
export function useFocusInvalid(
formEl: HTMLFormElement | null,
hasErrors: boolean,
) {
useEffect(() => {
if (!formEl) return
if (!hasErrors) return

if (formEl.matches('[aria-invalid="true"]')) {
formEl.focus()
} else {
const firstInvalid = formEl.querySelector('[aria-invalid="true"]')
if (firstInvalid instanceof HTMLElement) {
firstInvalid.focus()
}
}
}, [formEl, hasErrors])
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useFormAction, useNavigation } from '@remix-run/react'
import { clsx, type ClassValue } from 'clsx'
import { useEffect } from 'react'
import { twMerge } from 'tailwind-merge'

/**
Expand Down Expand Up @@ -78,25 +77,3 @@ export function useIsSubmitting({
navigation.formMethod === formMethod
)
}

/**
* A hook that focuses the first invalid element in a form.
*/
export function useFocusInvalid(
formEl: HTMLFormElement | null,
hasErrors: boolean,
) {
useEffect(() => {
if (!formEl) return
if (!hasErrors) return

if (formEl.matches('[aria-invalid="true"]')) {
formEl.focus()
} else {
const firstInvalid = formEl.querySelector('[aria-invalid="true"]')
if (firstInvalid instanceof HTMLElement) {
firstInvalid.focus()
}
}
}, [formEl, hasErrors])
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useFormAction, useNavigation } from '@remix-run/react'
import { clsx, type ClassValue } from 'clsx'
import { useEffect } from 'react'
import { twMerge } from 'tailwind-merge'

/**
Expand Down Expand Up @@ -78,25 +77,3 @@ export function useIsSubmitting({
navigation.formMethod === formMethod
)
}

/**
* A hook that focuses the first invalid element in a form.
*/
export function useFocusInvalid(
formEl: HTMLFormElement | null,
hasErrors: boolean,
) {
useEffect(() => {
if (!formEl) return
if (!hasErrors) return

if (formEl.matches('[aria-invalid="true"]')) {
formEl.focus()
} else {
const firstInvalid = formEl.querySelector('[aria-invalid="true"]')
if (firstInvalid instanceof HTMLElement) {
firstInvalid.focus()
}
}
}, [formEl, hasErrors])
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useFormAction, useNavigation } from '@remix-run/react'
import { clsx, type ClassValue } from 'clsx'
import { useEffect } from 'react'
import { twMerge } from 'tailwind-merge'

/**
Expand Down Expand Up @@ -78,25 +77,3 @@ export function useIsSubmitting({
navigation.formMethod === formMethod
)
}

/**
* A hook that focuses the first invalid element in a form.
*/
export function useFocusInvalid(
formEl: HTMLFormElement | null,
hasErrors: boolean,
) {
useEffect(() => {
if (!formEl) return
if (!hasErrors) return

if (formEl.matches('[aria-invalid="true"]')) {
formEl.focus()
} else {
const firstInvalid = formEl.querySelector('[aria-invalid="true"]')
if (firstInvalid instanceof HTMLElement) {
firstInvalid.focus()
}
}
}, [formEl, hasErrors])
}

0 comments on commit 3d6e494

Please sign in to comment.