Skip to content

Commit a205e4f

Browse files
committed
Rename a few things, minor refactors
1 parent 1c392b3 commit a205e4f

File tree

3 files changed

+39
-47
lines changed

3 files changed

+39
-47
lines changed

src/form.js

+36-43
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,25 @@ import React, {
88
import { func } from 'prop-types'
99
export const FormContext = createContext(null)
1010

11-
const getElement = (search, elements, mapper = (x) => x) =>
12-
elements[
13-
Array.from(elements)
14-
.map(mapper)
15-
.findIndex((x) => x === search || x.id === search || x.name === search)
16-
]
11+
const getName = (ref) => ref.id || ref.name
1712

1813
const Form = forwardRef(({ onSubmit, ...rest }, ref) => {
1914
const formRef = useRef(ref)
2015
const touched = useRef({})
21-
const fields = useRef([])
16+
const fields = useRef({})
2217

2318
/**
2419
* This is invoked from `useValidation`
2520
* Each element, as it's mounted, must register with us so we can do things with them
2621
* This happens in a `useEffect` - the disposable will call the unregister function.
2722
*/
28-
const register = useCallback(
29-
(field, details) => fields.current.push({ field, details }),
30-
[]
31-
)
23+
const register = useCallback((ref, ctx) => {
24+
fields.current[getName(ref)] = { ref, ctx }
25+
}, [])
3226

33-
const unregister = useCallback(
34-
(field) =>
35-
(fields.current = fields.current.filter(
36-
(f) => f.field.name !== field.name
37-
)),
38-
[]
39-
)
27+
const unregister = useCallback((ref) => {
28+
delete fields.current[getName(ref)]
29+
}, [])
4030

4131
/**
4232
* Validates a single input.
@@ -54,29 +44,27 @@ const Form = forwardRef(({ onSubmit, ...rest }, ref) => {
5444
* @param {HtmlInputElement} formInput the input to validate
5545
* @param {boolean} [force=false] whether to bypass touched check.
5646
*/
57-
const validateSingle = useCallback((formInput, force = false) => {
58-
const isTouched = touched.current[formInput.name]
47+
const validateSingle = useCallback((ref, force = false) => {
48+
const isTouched = touched.current[ref.name]
5949
if (!force && !isTouched) return
6050

61-
formInput.setCustomValidity('')
62-
if (!formInput.checkValidity()) return
51+
ref.setCustomValidity('')
52+
if (!ref.checkValidity()) return // the invalid event will have fired.
6353

64-
let error = null
65-
const field = getElement(formInput, fields.current, (x) => x.field)
66-
const others = fields.current.map((x) => x.field)
54+
const { ctx } = fields.current[getName(ref)]
55+
const refs = Object.entries(fields.current).map(([, { ref }]) => ref)
6756

68-
for (const fn of field.details.validation ?? []) {
69-
let err = fn(formInput, others)
70-
if (typeof err === 'string') error = new Error(err)
71-
else if (err instanceof Error) error = err
72-
if (error) break
73-
}
57+
let [error] = (ctx.validation ?? [])
58+
.map((fn) => fn(ref, refs))
59+
.filter((valResult) => valResult != null)
60+
61+
if (typeof error === 'string') error = new Error(error)
7462

75-
if (error) {
76-
formInput.setCustomValidity(error.message)
77-
formInput.checkValidity()
63+
if (error != null) {
64+
ref.setCustomValidity(error.message)
65+
ref.checkValidity()
7866
} else {
79-
field.details.updateState(error, formInput.validity)
67+
ctx.updateState(null, ref.validity)
8068
}
8169
}, [])
8270

@@ -86,12 +74,17 @@ const Form = forwardRef(({ onSubmit, ...rest }, ref) => {
8674
*/
8775
const validate = useCallback(
8876
({ target: element }) => {
89-
const field = getElement(element, fields.current, (x) => x.field)
90-
validateSingle(element)
91-
for (const item of field.details.otherArray) {
92-
const other = getElement(item, element.form.elements)
93-
if (other) validateSingle(other)
94-
}
77+
const { ctx } = fields.current[getName(element)]
78+
const allFields = ctx.otherArray.reduce(
79+
(acc, item) => {
80+
const other = fields.current[item]
81+
if (other) acc.push(other.ref)
82+
return acc
83+
},
84+
[element]
85+
)
86+
87+
allFields.forEach((field) => validateSingle(field))
9588
},
9689
[validateSingle]
9790
)
@@ -103,8 +96,8 @@ const Form = forwardRef(({ onSubmit, ...rest }, ref) => {
10396
*/
10497
const handleSubmit = useCallback(
10598
(e) => {
106-
for (const { field } of fields.current) {
107-
validateSingle(field, true)
99+
for (const [, { ref }] of Object.entries(fields.current)) {
100+
validateSingle(ref, true)
108101
}
109102
if (e.target.checkValidity()) {
110103
onSubmit?.(e)

src/input.js

-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ const createInput = (inputType) => {
7878
}
7979
)
8080

81-
Wrapped.isFormElement = true
8281
Wrapped.displayName = `Validated(${inputType})`
8382
Wrapped.propTypes = propTypes
8483

src/use-validation.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ const useValidation = (
108108
[onClick, click, validate]
109109
)
110110

111-
const details = useMemo(
111+
const ctx = useMemo(
112112
() => ({
113113
validation:
114114
validation == null
@@ -124,9 +124,9 @@ const useValidation = (
124124

125125
useEffect(() => {
126126
const thisRef = innerRef.current
127-
register(thisRef, details)
127+
register(thisRef, ctx)
128128
return () => unregister(thisRef)
129-
}, [innerRef, register, unregister, details])
129+
}, [innerRef, register, unregister, ctx])
130130

131131
useEffect(() => {
132132
const thisRef = innerRef.current

0 commit comments

Comments
 (0)