Skip to content

Commit

Permalink
refactor: improve example app to support multiple forms
Browse files Browse the repository at this point in the history
  • Loading branch information
inigomarquinez committed Feb 27, 2024
1 parent d3a8624 commit 2ad9348
Show file tree
Hide file tree
Showing 19 changed files with 420 additions and 140 deletions.
6 changes: 4 additions & 2 deletions example/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var OFF = 0,
const OFF = 0,
WARN = 1,
ERROR = 2

Expand Down Expand Up @@ -223,6 +223,8 @@ module.exports = exports = {
'spaced-comment': [WARN, 'always'],
'import/no-duplicates': WARN,
'react/jsx-no-bind': WARN,
'wrap-regex': WARN
'wrap-regex': WARN,

'no-console': WARN,
}
}
74 changes: 58 additions & 16 deletions example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"react": "file:../node_modules/react",
"react-dom": "file:../node_modules/react-dom",
"react-form-builder": "file:..",
"react-router-dom": "^6.22.1",
"react-scripts": "file:../node_modules/react-scripts"
},
"devDependencies": {
Expand Down
27 changes: 27 additions & 0 deletions example/src/App.jsx
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
17 changes: 17 additions & 0 deletions example/src/error-page.jsx
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>
);
}
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.
7 changes: 2 additions & 5 deletions example/src/App.js → example/src/forms/Contact/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@ import {
import forms from './forms.json'
import styles from './styles.js'

import 'react-datepicker/dist/react-datepicker.css'
import 'react-phone-number-input/style.css'

const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));

const App = () => {
const Contact = () => {
const [modalText, setModalText] = useState('')
const [show, setShow] = useState(false)
const [isLoading, setLoading] = useState(false)
Expand Down Expand Up @@ -71,4 +68,4 @@ const App = () => {
)
}

export default App
export default Contact
5 changes: 5 additions & 0 deletions example/src/forms/Contact/styles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
container: {
padding: '30px 30px'
}
}
27 changes: 27 additions & 0 deletions example/src/forms/Countries/form-new.json
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"
}
]
}
27 changes: 27 additions & 0 deletions example/src/forms/Countries/form-old.json
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"
}
]
}
92 changes: 92 additions & 0 deletions example/src/forms/Countries/index.jsx
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
Loading

0 comments on commit 2ad9348

Please sign in to comment.