Skip to content

Commit

Permalink
fix(form): add new form component with scroll to first invalid field
Browse files Browse the repository at this point in the history
  • Loading branch information
hirsch88 committed Aug 23, 2022
1 parent 3ba8d54 commit 812e967
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions packages/components/src/utils/form.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
interface ScrollToFirstInvalidFieldOptions {
formEl: HTMLElement
selector?: string
}

export const scrollToFirstInvalidField = (options: ScrollToFirstInvalidFieldOptions) => {
const invalidFieldNodes = options.formEl.querySelectorAll('.bal-field--invalid')
const selector = options.selector || '.bal-field--invalid'
const invalidFieldNodes = options.formEl.querySelectorAll(selector)
const invalidFields = Array.from(invalidFieldNodes)

if (invalidFields.length > 0) {
const firstInvalidField = invalidFields[0]
if (firstInvalidField && firstInvalidField.scrollIntoView) {
firstInvalidField.scrollIntoView()
if (firstInvalidField) {
const invalidField = firstInvalidField.closest('bal-field')

if (invalidField && invalidField.scrollIntoView) {
invalidField.scrollIntoView()
}
}
}
}

0 comments on commit 812e967

Please sign in to comment.