Skip to content

Commit

Permalink
Add example application
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamim Sonntag committed Mar 28, 2022
1 parent 580fa11 commit ca4111e
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/coverage
/node_modules
.parcel-cache
.parcel-dist
11 changes: 11 additions & 0 deletions example/index.html
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>
115 changes: 115 additions & 0 deletions example/index.tsx
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'));
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"coverage": "jest --coverage",
"lint": "eslint --ext .js,.ts,.tsx .",
"lint-staged": "lint-staged",
"start-example": "parcel --dist-dir=.parcel-dist example/index.html",
"test": "jest",
"test-watch": "jest --watch --notify --onlyChanged",
"version": "NODE_ENV=production npm run bundle && npm run changelog --future-release=$npm_package_version && git add -A CHANGELOG.md dist"
Expand Down

0 comments on commit ca4111e

Please sign in to comment.