Skip to content

Commit

Permalink
style: add classname to textarea when it has errors (#237)
Browse files Browse the repository at this point in the history
* style: add error-input class to Textarea when it has errors

* chore: add error styles to example forms
  • Loading branch information
inigomarquinez committed Apr 10, 2024
1 parent c5db271 commit 269ddef
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 3 deletions.
2 changes: 2 additions & 0 deletions example/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { Link, Outlet } from 'react-router-dom'
import 'react-datepicker/dist/react-datepicker.css'
import 'react-phone-number-input/style.css'

import './styles.css'

const App = () => {
return (
<div style={{ display: 'flex' }}>
Expand Down
3 changes: 3 additions & 0 deletions example/src/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.error-input {
border: 1px solid red !important
}
3 changes: 3 additions & 0 deletions src/Fields/Textarea/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import React, { useState } from 'react'

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

return (
<>
<TextareaUI
ref={ref}
className={haserrors ? 'error-input' : ''}
{...props}
onChange={(e) => {
if (props.countType === 'word')
Expand Down
10 changes: 8 additions & 2 deletions src/Questions/Textarea/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { useFormContext } from 'react-hook-form'

import ErrorMessage from '../../Fields/Error'
import Textarea from '../../Fields/Textarea'
import Label from '../../Fields/Label'
Expand All @@ -13,11 +15,14 @@ const styles = {
}
}

const QuestionTextarea = ({ question, useForm }) => {
const QuestionTextarea = ({ question }) => {
const {
register,
formState: { errors }
} = useForm
} = useFormContext()



const defaultRows = 5
const reg = {
...question.registerConfig,
Expand Down Expand Up @@ -89,6 +94,7 @@ const QuestionTextarea = ({ question, useForm }) => {
defaultValue={question.defaultValue}
maximumLen={question.registerConfig.maximumLen}
countType={question.registerConfig.countType}
data-haserrors={!!errors[question.name]}
{...register(question.name, reg)}
/>
{errors[question.name] &&
Expand Down
2 changes: 1 addition & 1 deletion src/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const FormBuilder = ({
<QuestionImageInput useForm={useFormObj} question={question} />
),
password: <QuestionInput useForm={useFormObj} question={question} />,
textarea: <QuestionTextarea useForm={useFormObj} question={question} />,
textarea: <QuestionTextarea question={question} />,
select: (
<>
<QuestionSelect useForm={useFormObj} question={question} />
Expand Down

0 comments on commit 269ddef

Please sign in to comment.