Skip to content

Commit e510395

Browse files
committed
fix: add check for nullish required config
1 parent a08bd75 commit e510395

File tree

8 files changed

+97
-91
lines changed

8 files changed

+97
-91
lines changed

example/package-lock.json

Lines changed: 18 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"react": "file:../node_modules/react",
1717
"react-dom": "file:../node_modules/react-dom",
1818
"react-router-dom": "^6.22.1",
19-
"react-scripts": "file:../node_modules/react-scripts"
19+
"react-scripts": "file:../node_modules/react-scripts",
20+
"uuid": "file:../node_modules/uuid"
2021
},
2122
"devDependencies": {
2223
"@babel/plugin-syntax-object-rest-spread": "^7.8.3",

example/public/index.html

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,13 @@
88
content="width=device-width, initial-scale=1, shrink-to-fit=no"
99
/>
1010
<meta name="theme-color" content="#000000" />
11-
12-
<!--
13-
manifest.json provides metadata used when your web app is added to the
14-
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
15-
-->
16-
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
17-
18-
<!--
19-
Notice the use of %PUBLIC_URL% in the tags above.
20-
It will be replaced with the URL of the `public` folder during the build.
21-
Only files inside the `public` folder can be referenced from the HTML.
22-
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
23-
work correctly both with client-side routing and a non-root public URL.
24-
Learn how to configure a non-root public URL by running `npm run build`.
25-
-->
2611
<title>react-form-builder</title>
2712
</head>
2813

2914
<body>
3015
<noscript>
3116
You need to enable JavaScript to run this app.
3217
</noscript>
33-
3418
<div id="root"></div>
35-
36-
<!--
37-
This HTML file is a template.
38-
If you open it directly in the browser, you will see an empty page.
39-
You can add webfonts, meta tags, or analytics to this file.
40-
The build step will place the bundled scripts into the <body> tag.
41-
To begin the development, run `npm start` or `yarn start`.
42-
To create a production bundle, use `npm run build` or `yarn build`.
43-
-->
4419
</body>
45-
</html>
20+
</html>

example/src/forms/Contact/forms.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,4 +359,4 @@
359359
],
360360
"id": "4HHq4GwrEE2bDL9YJUbUyi"
361361
}
362-
}
362+
}

src/Fields/Textarea/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@ import { Textarea as TextareaUI, jsx } from 'theme-ui'
44

55
import React, { useState } from 'react'
66

7-
const Textarea = React.forwardRef(({ ...props }, ref) => {
7+
const Textarea = React.forwardRef(({ countType, maximumLen, ...props }, ref) => {
88
const [count, setCount] = useState(0)
99
return (
1010
<>
1111
<TextareaUI
1212
ref={ref}
1313
{...props}
1414
onChange={(e) => {
15-
if (props.countType === 'word')
15+
if (countType === 'word')
1616
setCount(e.target.value.trim().split(/[\s,.\n]+/).length)
1717
// By default is char count
1818
else setCount(e.target.value.length)
1919
}}
2020
/>
21-
{props.maximumLen && (
21+
{maximumLen && (
2222
<span>
23-
{count}/{props.maximumLen}
23+
{count}/{maximumLen}
2424
</span>
2525
)}
2626
</>

src/Questions/Input/index.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,15 @@ const QuestionInput = ({ question, useForm, component, onLinkOpen }) => {
6969
placeholder={question.placeholder}
7070
defaultValue={question.defaultValue}
7171
data-haserrors={!!errors[question.name]}
72-
{...register(question.name, {
73-
...question.registerConfig,
74-
pattern: new RegExp(question.registerConfig.pattern)
75-
})}
72+
{...register(
73+
question.name,
74+
question.registerConfig
75+
? {
76+
...question.registerConfig,
77+
pattern: new RegExp(question.registerConfig.pattern)
78+
}
79+
: {}
80+
)}
7681
/>
7782
{errors[question.name] && errors[question.name].type && (
7883
<ErrorMessage

src/Questions/MultipleCheckboxes/index.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,23 +78,28 @@ const QuestionMultipleCheckboxes = ({ component, form, question, useForm }) => {
7878
defaultChecked={question.defaultCheckedValues?.find(
7979
(defaultValue) => defaultValue === option.value
8080
)}
81-
{...register(question.name, {
82-
...question.registerConfig,
83-
validate: {
84-
minimumLen: question.registerConfig.minimumLen
85-
? () =>
86-
getValues()[question.name] &&
87-
getValues()[question.name].length >=
88-
question.registerConfig.minimumLen
89-
: () => true,
90-
maximumLen: question.registerConfig.maximumLen
91-
? () =>
92-
getValues()[question.name] &&
93-
getValues()[question.name].length <=
94-
question.registerConfig.maximumLen
95-
: () => true
96-
}
97-
})}
81+
{...register(
82+
question.name,
83+
question.registerConfig
84+
? {
85+
...question.registerConfig,
86+
validate: {
87+
minimumLen: question.registerConfig.minimumLen
88+
? () =>
89+
getValues()[question.name] &&
90+
getValues()[question.name].length >=
91+
question.registerConfig.minimumLen
92+
: () => true,
93+
maximumLen: question.registerConfig.maximumLen
94+
? () =>
95+
getValues()[question.name] &&
96+
getValues()[question.name].length <=
97+
question.registerConfig.maximumLen
98+
: () => true
99+
}
100+
}
101+
: {}
102+
)}
98103
onChange={option.disableOthers && disableOthers}
99104
/>
100105
{option.src ? (

src/Questions/Textarea/index.js

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,41 +19,43 @@ const QuestionTextarea = ({ question, useForm }) => {
1919
formState: { errors }
2020
} = useForm
2121
const defaultRows = 5
22-
const reg = {
23-
...question.registerConfig,
24-
pattern: new RegExp(question.registerConfig.pattern),
25-
// By default is char count
26-
minLength:
27-
question.registerConfig.countType !== 'word' &&
28-
question.registerConfig.minimumLen,
29-
maxLength:
30-
question.registerConfig.countType !== 'word' &&
31-
question.registerConfig.maximumLen,
32-
validate: {
33-
minWordCount: (v) => {
34-
if (
35-
question.registerConfig.countType === 'word' &&
36-
question.registerConfig.minimumLen
37-
) {
38-
return (
39-
v.trim().split(/[\s,.\n]+/).length >=
40-
question.registerConfig.minimumLen
41-
)
42-
} else return true
43-
},
44-
maxWordCount: (v) => {
45-
if (
46-
question.registerConfig.countType === 'word' &&
47-
question.registerConfig.maximumLen
48-
) {
49-
return (
50-
v.trim().split(/[\s,.\n]+/).length <=
51-
question.registerConfig.maximumLen
52-
)
53-
} else return true
22+
const reg = question.registerConfig
23+
? {
24+
...question.registerConfig,
25+
pattern: new RegExp(question.registerConfig.pattern),
26+
// By default is char count
27+
minLength:
28+
question.registerConfig.countType !== 'word' &&
29+
question.registerConfig.minimumLen,
30+
maxLength:
31+
question.registerConfig.countType !== 'word' &&
32+
question.registerConfig.maximumLen,
33+
validate: {
34+
minWordCount: (v) => {
35+
if (
36+
question.registerConfig.countType === 'word' &&
37+
question.registerConfig.minimumLen
38+
) {
39+
return (
40+
v.trim().split(/[\s,.\n]+/).length >=
41+
question.registerConfig.minimumLen
42+
)
43+
} else return true
44+
},
45+
maxWordCount: (v) => {
46+
if (
47+
question.registerConfig.countType === 'word' &&
48+
question.registerConfig.maximumLen
49+
) {
50+
return (
51+
v.trim().split(/[\s,.\n]+/).length <=
52+
question.registerConfig.maximumLen
53+
)
54+
} else return true
55+
}
56+
}
5457
}
55-
}
56-
}
58+
: {}
5759

5860
return (
5961
<div
@@ -87,8 +89,10 @@ const QuestionTextarea = ({ question, useForm }) => {
8789
name={question.name}
8890
placeholder={question.placeholder}
8991
defaultValue={question.defaultValue}
90-
maximumLen={question.registerConfig.maximumLen}
91-
countType={question.registerConfig.countType}
92+
maximumLen={
93+
question.registerConfig && question.registerConfig.maximumLen
94+
}
95+
countType={question.registerConfig && question.registerConfig.countType}
9296
{...register(question.name, reg)}
9397
/>
9498
{errors[question.name] &&

0 commit comments

Comments
 (0)