-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Benjamim Sonntag
committed
Mar 28, 2022
1 parent
580fa11
commit ca4111e
Showing
4 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
/coverage | ||
/node_modules | ||
.parcel-cache | ||
.parcel-dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>Example app</title> | ||
</head> | ||
<body> | ||
<div id="app"></div> | ||
<script type="module" src="index.tsx"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
import { FormProvider, useField, useFormState } from '../src'; | ||
import ReactDOM from 'react-dom'; | ||
|
||
/** | ||
* `Input` component. | ||
*/ | ||
|
||
function Input({ label, name, type = 'text' }) { | ||
const { meta, onBlur, onChange, onFocus, value } = useField(name); | ||
|
||
return ( | ||
<label> | ||
<span | ||
style={{ | ||
color: meta.active ? 'rebeccapurple' : 'initial' | ||
}} | ||
> | ||
{label} | ||
</span> | ||
|
||
<input | ||
name={name} | ||
onBlur={onBlur} | ||
onChange={event => onChange(event.target.value)} | ||
onFocus={onFocus} | ||
type={type} | ||
value={value ?? ''} | ||
/> | ||
</label> | ||
); | ||
} | ||
|
||
/** | ||
* `RerenderTester` component. | ||
*/ | ||
|
||
function RerenderTester() { | ||
console.log('RerenderTester rendered'); // eslint-disable-line no-console | ||
|
||
return ( | ||
<div> | ||
{'Re-render tester'} | ||
</div> | ||
); | ||
} | ||
|
||
/** | ||
* `SubmitButton` component. | ||
*/ | ||
|
||
function SubmitButton() { | ||
const { isSubmitting } = useFormState(); | ||
|
||
return ( | ||
<button | ||
disabled={isSubmitting} | ||
type={'submit'} | ||
> | ||
{isSubmitting ? 'Submitting' : 'Submit'} | ||
</button> | ||
); | ||
} | ||
|
||
/** | ||
* `App` component. | ||
*/ | ||
|
||
function App() { | ||
return ( | ||
<div> | ||
<h1>{'Hello world!'}</h1> | ||
|
||
<FormProvider | ||
initialValues={{}} | ||
jsonSchema={{}} | ||
onSubmit={values => { | ||
console.log('submit', values); // eslint-disable-line no-console | ||
|
||
return new Promise(resolve => { | ||
setTimeout(() => resolve, 5000); | ||
}); | ||
}} | ||
> | ||
{({ submit }) => ( | ||
<form onSubmit={submit}> | ||
<RerenderTester /> | ||
|
||
<Input | ||
label={'Name'} | ||
name={'name'} | ||
/> | ||
|
||
<Input | ||
label={'Email'} | ||
name={'email'} | ||
/> | ||
|
||
<SubmitButton /> | ||
</form> | ||
)} | ||
</FormProvider> | ||
</div> | ||
); | ||
} | ||
|
||
/** | ||
* Render app. | ||
*/ | ||
|
||
ReactDOM.render(<App />, document.getElementById('app')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters