-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: improve example app to support multiple forms
- Loading branch information
1 parent
d3a8624
commit 2ad9348
Showing
19 changed files
with
420 additions
and
140 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,27 @@ | ||
import React from 'react' | ||
import { Link, Outlet } from 'react-router-dom' | ||
|
||
import 'react-datepicker/dist/react-datepicker.css' | ||
import 'react-phone-number-input/style.css' | ||
|
||
const App = () => { | ||
return ( | ||
<div style={{ display: 'flex' }}> | ||
<nav> | ||
<ul> | ||
<li> | ||
<Link to={'contact'}>Contact form</Link> | ||
</li> | ||
<li> | ||
<Link to={'countries'}>Countries form</Link> | ||
</li> | ||
</ul> | ||
</nav> | ||
<div> | ||
<Outlet /> | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
export default 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import React from 'react' | ||
import { useRouteError } from 'react-router-dom' | ||
|
||
export default function ErrorPage() { | ||
const error = useRouteError(); | ||
// console.error(error); | ||
|
||
return ( | ||
<div id="error-page"> | ||
<h1>Oops!</h1> | ||
<p>Sorry, an unexpected error has occurred.</p> | ||
<p> | ||
<i>{error.statusText || error.message}</i> | ||
</p> | ||
</div> | ||
); | ||
} |
7 changes: 4 additions & 3 deletions
7
example/src/App.test.js → example/src/forms/Contact/Contact.test.js
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,9 +1,10 @@ | ||
import React from 'react' | ||
import ReactDOM from 'react-dom/client' | ||
import App from './App' | ||
|
||
it('renders without crashing', () => { | ||
import Contact from './index' | ||
|
||
it.skip('renders without crashing', () => { | ||
const div = document.createElement('div') | ||
ReactDOM.createRoot(<App />, div) | ||
ReactDOM.createRoot(<Contact />, div) | ||
ReactDOM.unmountComponentAtNode(div) | ||
}) |
File renamed without changes.
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
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,5 @@ | ||
export default { | ||
container: { | ||
padding: '30px 30px' | ||
} | ||
} |
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,27 @@ | ||
{ | ||
"name": "Form with new Country question format", | ||
"title": "Form with new Country question format", | ||
"questions": [ | ||
{ | ||
"name": "Country", | ||
"placeholder": "Select your country", | ||
"priorityOptions": [ | ||
"GB" | ||
], | ||
"type": "country", | ||
"label": "Current country question", | ||
"errorMessages": { | ||
"required": "This field is required" | ||
}, | ||
"registerConfig": { | ||
"required": true | ||
} | ||
} | ||
], | ||
"callForAction": [ | ||
{ | ||
"caption": "Enter", | ||
"type": "submit" | ||
} | ||
] | ||
} |
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,27 @@ | ||
{ | ||
"name": "Form with old Country question format", | ||
"title": "Form with old Country question format", | ||
"questions": [ | ||
{ | ||
"name": "Country", | ||
"placeholder": "Select your country", | ||
"priorityOptions": [ | ||
"GB" | ||
], | ||
"type": "country", | ||
"label": "Current country question", | ||
"errorMessages": { | ||
"required": "This field is required" | ||
}, | ||
"registerConfig": { | ||
"required": true | ||
} | ||
} | ||
], | ||
"callForAction": [ | ||
{ | ||
"caption": "Enter", | ||
"type": "submit" | ||
} | ||
] | ||
} |
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,92 @@ | ||
/* eslint-disable */ | ||
/** @jsxRuntime classic */ | ||
/** @jsx jsx */ | ||
import { jsx } from 'theme-ui' | ||
import React, { useState } from 'react' | ||
import { | ||
Label, | ||
Modal, | ||
FormBuilder | ||
} from 'react-form-builder' | ||
|
||
import oldForm from './form-old.json' | ||
import newForm from './form-new.json' | ||
import styles from './styles.js' | ||
|
||
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); | ||
|
||
const CountriesForm = () => { | ||
const [modalText, setModalText] = useState('') | ||
const [show, setShow] = useState(false) | ||
const [isLoading, setLoading] = useState(false) | ||
const [formErrors, setFormErrors] = useState() | ||
|
||
function onLinkOpen(name) { | ||
setModalText(forms.contact.textToShow[name]) | ||
setShow(true) | ||
} | ||
|
||
const onSubmitForm = async (data) => { | ||
console.log('Submitting the form...') | ||
|
||
// Simulate loading | ||
setLoading(true) | ||
await sleep(5000); | ||
setLoading(false) | ||
|
||
alert( | ||
`You have submitted your form correctly: ${'\n'} ${JSON.stringify( | ||
data, | ||
null, | ||
2 | ||
)}` | ||
) | ||
} | ||
|
||
return ( | ||
<div sx={styles.container}> | ||
<div sx={styles.formContainer}> | ||
<h1> | ||
{oldForm.title} | ||
</h1> | ||
<Modal | ||
title='test' | ||
onClose={() => setShow(false)} | ||
show={show} | ||
modalText={modalText} | ||
/> | ||
<FormBuilder | ||
form={oldForm} | ||
onSubmit={onSubmitForm} | ||
isoCode='ES' | ||
language='en' | ||
onLinkOpen={onLinkOpen} | ||
isLoading={isLoading} | ||
formErrors={formErrors} | ||
/> | ||
</div> | ||
<div sx={styles.formContainer}> | ||
<h1> | ||
{newForm.title} | ||
</h1> | ||
<Modal | ||
title='test' | ||
onClose={() => setShow(false)} | ||
show={show} | ||
modalText={modalText} | ||
/> | ||
<FormBuilder | ||
form={newForm} | ||
onSubmit={onSubmitForm} | ||
isoCode='ES' | ||
language='en' | ||
onLinkOpen={onLinkOpen} | ||
isLoading={isLoading} | ||
formErrors={formErrors} | ||
/> | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
export default CountriesForm |
Oops, something went wrong.