Skip to content

Commit 269ddef

Browse files
style: add classname to textarea when it has errors (#237)
* style: add error-input class to Textarea when it has errors * chore: add error styles to example forms
1 parent c5db271 commit 269ddef

File tree

5 files changed

+17
-3
lines changed

5 files changed

+17
-3
lines changed

example/src/App.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { Link, Outlet } from 'react-router-dom'
44
import 'react-datepicker/dist/react-datepicker.css'
55
import 'react-phone-number-input/style.css'
66

7+
import './styles.css'
8+
79
const App = () => {
810
return (
911
<div style={{ display: 'flex' }}>

example/src/styles.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.error-input {
2+
border: 1px solid red !important
3+
}

src/Fields/Textarea/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ import React, { useState } from 'react'
66

77
const Textarea = React.forwardRef(({ ...props }, ref) => {
88
const [count, setCount] = useState(0)
9+
const { 'data-haserrors': haserrors } = props
10+
911
return (
1012
<>
1113
<TextareaUI
1214
ref={ref}
15+
className={haserrors ? 'error-input' : ''}
1316
{...props}
1417
onChange={(e) => {
1518
if (props.countType === 'word')

src/Questions/Textarea/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { useFormContext } from 'react-hook-form'
2+
13
import ErrorMessage from '../../Fields/Error'
24
import Textarea from '../../Fields/Textarea'
35
import Label from '../../Fields/Label'
@@ -13,11 +15,14 @@ const styles = {
1315
}
1416
}
1517

16-
const QuestionTextarea = ({ question, useForm }) => {
18+
const QuestionTextarea = ({ question }) => {
1719
const {
1820
register,
1921
formState: { errors }
20-
} = useForm
22+
} = useFormContext()
23+
24+
25+
2126
const defaultRows = 5
2227
const reg = {
2328
...question.registerConfig,
@@ -89,6 +94,7 @@ const QuestionTextarea = ({ question, useForm }) => {
8994
defaultValue={question.defaultValue}
9095
maximumLen={question.registerConfig.maximumLen}
9196
countType={question.registerConfig.countType}
97+
data-haserrors={!!errors[question.name]}
9298
{...register(question.name, reg)}
9399
/>
94100
{errors[question.name] &&

src/builder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ const FormBuilder = ({
8686
<QuestionImageInput useForm={useFormObj} question={question} />
8787
),
8888
password: <QuestionInput useForm={useFormObj} question={question} />,
89-
textarea: <QuestionTextarea useForm={useFormObj} question={question} />,
89+
textarea: <QuestionTextarea question={question} />,
9090
select: (
9191
<>
9292
<QuestionSelect useForm={useFormObj} question={question} />

0 commit comments

Comments
 (0)