Skip to content
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

fix: handle undefined item in select noEmpty validation and remove phone input default value #275

Merged
merged 5 commits into from
Aug 22, 2024
Merged
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
7 changes: 4 additions & 3 deletions src/Fields/Phone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const Phone = ({
registerConfig,
setValue,
name,
defaultValue,
...props
}) => {
return (
Expand All @@ -28,8 +29,8 @@ const Phone = ({
registerConfig.required &&
!isValidPhoneNumber(phone)
? setError(name, {
type: 'isValidPhoneNumber'
})
type: 'isValidPhoneNumber'
})
: clearErrors(name)
}
shouldSetError()
Expand All @@ -41,12 +42,12 @@ const Phone = ({
name={name}
render={({ field: { onChange } }) => (
<PhoneInput
defaultValue=''
onChange={onChange}
dir='ltr'
placeholder={placeholder}
defaultCountry={defaultCountry}
inputComponent={Input}
value={props.value || defaultValue || ''}
{...props}
/>
)}
Expand Down
4 changes: 3 additions & 1 deletion src/Fields/Select/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ const Select = forwardRef(function InnerSelect({
...registerConfig,
validate: {
noEmpty: (item) =>
registerConfig && registerConfig.required ? item.value !== '' : true
registerConfig && registerConfig.required
? 'value' in Object(item || null) && item.value !== ''
: true
}
}}
name={name}
Expand Down
28 changes: 28 additions & 0 deletions src/Questions/Phone/__tests__/phone.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,31 @@ test('country code is visible', () => {
result.container.querySelector('input[type="tel"]').getAttribute('value')
).toBe('+44')
})

test('default value is displayed', () => {
const defaultValue = '555666777'
const { getByTestId } = render(
<QuestionPhone
question={{ ...question, defaultValue }}
useForm={formMethods}
/>
)
const phoneComponent = getByTestId('question-phone')
expect(phoneComponent.getAttribute('value')).toBe(defaultValue)
})

test('default value can be changed', () => {
const defaultValue = '555666777'
const changedValue = '777666555'
const formatedChangedValue = '+7 776 665 55'
const { getByTestId } = render(
<QuestionPhone
question={{ ...question, defaultValue }}
useForm={formMethods}
/>
)
const phoneComponent = getByTestId('question-phone')
expect(phoneComponent.value).toBe(defaultValue)
fireEvent.change(phoneComponent, { target: { value: changedValue } })
expect(phoneComponent.value).toBe(formatedChangedValue)
})
1 change: 1 addition & 0 deletions src/Questions/Phone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const QuestionPhone = ({ isMobile, isoCode, question, useForm, ...props }) => {
registerConfig={question.registerConfig}
name={question.name}
international={question.international}
defaultValue={question.defaultValue}
{...props}
/>

Expand Down
25 changes: 13 additions & 12 deletions src/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ const FormBuilder = ({
}) => {
const useFormObj = useForm()
const [formDataValues, setFormDataValues] = useState({})
const hasRecaptcha = form.questions.some(question => question.type === 'recaptcha')
const hasRecaptcha = form.questions.some(
(question) => question.type === 'recaptcha'
)
const recaptchaRef = useRef(null)

useEffect(() => {
Expand Down Expand Up @@ -125,10 +127,7 @@ const FormBuilder = ({
),
country_v2: (
<>
<QuestionCountryV2
question={question}
language={language}
/>
<QuestionCountryV2 question={question} language={language} />
{question.dependentQuestions &&
question.dependentQuestions.map(
ConditionalQuestion(question.dependentQuestions, question.name)
Expand Down Expand Up @@ -269,17 +268,17 @@ const FormBuilder = ({
<React.Fragment key={i}>
{
QuestionsMap(dependentQuestion.question)[
dependentQuestion.question.type
dependentQuestion.question.type
]
}

{nestedQuestion.dependentQuestions
? nestedQuestion.dependentQuestions.map(
ConditionalQuestion(
nestedQuestion.question,
dependentQuestion.name
)
ConditionalQuestion(
nestedQuestion.question,
dependentQuestion.name
)
)
: null}
</React.Fragment>
)
Expand Down Expand Up @@ -336,12 +335,14 @@ const FormBuilder = ({
if (hasRecaptcha) {
recaptchaRef.current?.execute()
setFormDataValues(await formatData(data))

} else {
onSubmitForm(await formatData(data))
}
}

const hasErrors =
Object.values(errors).some((error) => error.type === 'required').length > 0

return (
<FormProvider {...useFormObj}>
<form
Expand Down Expand Up @@ -387,7 +388,7 @@ const FormBuilder = ({
<Button
sx={styles.fitContent}
key={cfa.caption}
disabled={isLoading}
disabled={isLoading || hasErrors || formErrors.length > 0}
isLoading={isLoading}
id={cfa.id}
caption={cfa.caption}
Expand Down