Skip to content

Commit d2cfc0f

Browse files
committed
form inner in a new file
1 parent 26acf2f commit d2cfc0f

2 files changed

Lines changed: 37 additions & 21 deletions

File tree

src/Form/FormInner.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import React from 'react'
2+
import isReactNative from '../utility/isReactNative'
3+
4+
interface FormInnerProps {
5+
domProps: any
6+
children: React.ReactNode
7+
onFormSubmit: (event: React.FormEvent) => void
8+
useFormTag: boolean
9+
}
10+
11+
function FormInner({domProps, children, onFormSubmit, useFormTag}: FormInnerProps) {
12+
if (isReactNative()) {
13+
return <>{children}</>
14+
}
15+
16+
if (useFormTag !== false) {
17+
return (
18+
<form {...domProps} onSubmit={onFormSubmit}>
19+
{children}
20+
</form>
21+
)
22+
}
23+
24+
return <>{children}</>
25+
}
26+
27+
export default FormInner

src/Form/index.tsx

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
ValueContext,
2121
} from '../Contexts'
2222
import {FormProps, FormRef} from '../types'
23-
import isReactNative from '../utility/isReactNative'
23+
import FormInner from './FormInner'
2424
import getNewValue from './getNewValue'
2525

2626
function Form(props: FormProps, ref: React.Ref<FormRef>) {
@@ -108,30 +108,19 @@ function Form(props: FormProps, ref: React.Ref<FormRef>) {
108108
[props],
109109
)
110110

111-
const renderChild = () => {
112-
if (isReactNative()) {
113-
return props.children
114-
}
115-
116-
if (props.useFormTag !== false) {
117-
return (
118-
<form {...domProps} onSubmit={onFormSubmit}>
119-
{props.children}
120-
</form>
121-
)
122-
}
123-
124-
return props.children
125-
}
126-
127-
console.log('[simple-react-form] state', state)
128-
console.log('[simple-react-form] props', props)
129-
130111
return (
131112
<ParentFieldNameContext.Provider value={null}>
132113
<ErrorMessagesContext.Provider value={props.errorMessages}>
133114
<OnChangeContext.Provider value={onChange}>
134-
<ValueContext.Provider value={state}>{renderChild()}</ValueContext.Provider>
115+
<ValueContext.Provider value={state}>
116+
<FormInner
117+
domProps={domProps}
118+
onFormSubmit={onFormSubmit}
119+
useFormTag={props.useFormTag}
120+
>
121+
{props.children}
122+
</FormInner>
123+
</ValueContext.Provider>
135124
</OnChangeContext.Provider>
136125
</ErrorMessagesContext.Provider>
137126
</ParentFieldNameContext.Provider>

0 commit comments

Comments
 (0)