Skip to content

fix: allow optional field configurations #281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 33 additions & 16 deletions example/package-lock.json

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

3 changes: 2 additions & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"react": "file:../node_modules/react",
"react-dom": "file:../node_modules/react-dom",
"react-router-dom": "^6.22.1",
"react-scripts": "file:../node_modules/react-scripts"
"react-scripts": "file:../node_modules/react-scripts",
"uuid": "file:../node_modules/uuid"
},
"devDependencies": {
"@babel/plugin-syntax-object-rest-spread": "^7.8.3",
Expand Down
27 changes: 1 addition & 26 deletions example/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,13 @@
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
<meta name="theme-color" content="#000000" />

<!--
manifest.json provides metadata used when your web app is added to the
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />

<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>react-form-builder</title>
</head>

<body>
<noscript>
You need to enable JavaScript to run this app.
</noscript>

<div id="root"></div>

<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
</html>
2 changes: 1 addition & 1 deletion example/src/forms/Contact/forms.json
Original file line number Diff line number Diff line change
Expand Up @@ -377,4 +377,4 @@
],
"id": "4HHq4GwrEE2bDL9YJUbUyi"
}
}
}
8 changes: 4 additions & 4 deletions src/Fields/Textarea/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Textarea as TextareaUI, jsx } from 'theme-ui'

import React, { useState } from 'react'

const Textarea = React.forwardRef(({ ...props }, ref) => {
const Textarea = React.forwardRef(({ countType, maximumLen, ...props }, ref) => {
const [count, setCount] = useState(0)
const { 'data-haserrors': haserrors } = props

Expand All @@ -15,15 +15,15 @@ const Textarea = React.forwardRef(({ ...props }, ref) => {
className={haserrors ? 'error-input' : ''}
{...props}
onChange={(e) => {
if (props.countType === 'word')
if (countType === 'word')
setCount(e.target.value.trim().split(/[\s,.\n]+/).length)
// By default is char count
else setCount(e.target.value.length)
}}
/>
{props.maximumLen && (
{maximumLen && (
<span>
{count}/{props.maximumLen}
{count}/{maximumLen}
</span>
)}
</>
Expand Down
13 changes: 9 additions & 4 deletions src/Questions/Input/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,15 @@ const QuestionInput = ({ question, useForm, component, onLinkOpen }) => {
placeholder={question.placeholder}
defaultValue={question.defaultValue}
data-haserrors={!!errors[question.name]}
{...register(question.name, {
...question.registerConfig,
pattern: new RegExp(question.registerConfig.pattern)
})}
{...register(
question.name,
question.registerConfig
? {
...question.registerConfig,
pattern: new RegExp(question.registerConfig.pattern)
}
: {}
)}
/>
{errors[question.name] && errors[question.name].type && (
<ErrorMessage
Expand Down
10 changes: 5 additions & 5 deletions src/Questions/MultipleCheckboxes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,22 @@ const QuestionMultipleCheckboxes = ({ question, useForm }) => {
(defaultValue) => defaultValue === option.value
)}
{...register(question.name, {
...question.registerConfig,
...(question.registerConfig ?? {}),
onChange: option.disableOthers
? disableOthers(option)
: undefined,
validate: {
minimumLen: question.registerConfig.minimumLen
minimumLen: question.registerConfig?.minimumLen
? () =>
getValues()[question.name] &&
getValues()[question.name].length >=
question.registerConfig.minimumLen
question.registerConfig?.minimumLen
: () => true,
maximumLen: question.registerConfig.maximumLen
maximumLen: question.registerConfig?.maximumLen
? () =>
getValues()[question.name] &&
getValues()[question.name].length <=
question.registerConfig.maximumLen
question.registerConfig?.maximumLen
: () => true
}
})}
Expand Down
78 changes: 40 additions & 38 deletions src/Questions/Textarea/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,44 +21,44 @@ const QuestionTextarea = ({ question }) => {
formState: { errors }
} = useFormContext()



const defaultRows = 5
const reg = {
...question.registerConfig,
pattern: new RegExp(question.registerConfig.pattern),
// By default is char count
minLength:
question.registerConfig.countType !== 'word' &&
question.registerConfig.minimumLen,
maxLength:
question.registerConfig.countType !== 'word' &&
question.registerConfig.maximumLen,
validate: {
minWordCount: (v) => {
if (
question.registerConfig.countType === 'word' &&
question.registerConfig.minimumLen
) {
return (
v.trim().split(/[\s,.\n]+/).length >=
question.registerConfig.minimumLen
)
} else return true
},
maxWordCount: (v) => {
if (
question.registerConfig.countType === 'word' &&
question.registerConfig.maximumLen
) {
return (
v.trim().split(/[\s,.\n]+/).length <=
question.registerConfig.maximumLen
)
} else return true
const reg = question.registerConfig
? {
...question.registerConfig,
pattern: new RegExp(question.registerConfig.pattern),
// By default is char count
minLength:
question.registerConfig.countType !== 'word' &&
question.registerConfig.minimumLen,
maxLength:
question.registerConfig.countType !== 'word' &&
question.registerConfig.maximumLen,
validate: {
minWordCount: (v) => {
if (
question.registerConfig.countType === 'word' &&
question.registerConfig.minimumLen
) {
return (
v.trim().split(/[\s,.\n]+/).length >=
question.registerConfig.minimumLen
)
} else return true
},
maxWordCount: (v) => {
if (
question.registerConfig.countType === 'word' &&
question.registerConfig.maximumLen
) {
return (
v.trim().split(/[\s,.\n]+/).length <=
question.registerConfig.maximumLen
)
} else return true
}
}
}
}
}
: {}

return (
<div
Expand Down Expand Up @@ -92,9 +92,11 @@ const QuestionTextarea = ({ question }) => {
name={question.name}
placeholder={question.placeholder}
defaultValue={question.defaultValue}
maximumLen={question.registerConfig.maximumLen}
countType={question.registerConfig.countType}
data-haserrors={!!errors[question.name]}
maximumLen={
question.registerConfig && question.registerConfig.maximumLen
}
countType={question.registerConfig && question.registerConfig.countType}
{...register(question.name, reg)}
/>
{errors[question.name] &&
Expand Down
Loading