Skip to content

Commit

Permalink
feat(mobile): add default country value (#124)
Browse files Browse the repository at this point in the history
* feat(mobile): add default country value

* feat(readme): update documentation
  • Loading branch information
masopego committed Nov 24, 2022
1 parent d8deffa commit 37843c6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ https://user-images.githubusercontent.com/79102959/134945855-52577cab-9b16-4df5-
| **registerConfig** | | json |
| required | Define if the phone input is required | boolean | false

Reminder: The isoCode prop that can be sent in the ReactFormBuilder component will define the default country displayed in the phone field.
Reminder: The isoCode prop that can also be sent in the ReactFormBuilder component will define the default country displayed in the phone field.

Basic phone

Expand All @@ -513,6 +513,7 @@ Basic phone
"registerConfig": {
"required": true
},
"defaultCountry":"GB",
"placeholder": "Phone",
"errorMessages": {
"required": "This field is required"
Expand Down
18 changes: 18 additions & 0 deletions src/Questions/Phone/__tests__/phone.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,24 @@ test('phone ES country is displayed', () => {
expect(countryComponent.value).toBe('FR')
})

test('default country code is displayed', () => {
const newQuestion = { ...question, defaultCountry: 'fr' }
const { getByLabelText } = render(
<QuestionPhone
question={newQuestion}
useForm={{
errors: {},
register: () => {},
setValue: jest.fn()
}}
/>
)
const countryComponent = getByLabelText('Phone number country')
expect(countryComponent.value).toBe('FR')
fireEvent.change(countryComponent, { target: { value: 'GB' } })
expect(countryComponent.value).toBe('GB')
})

test('pattern error is displayed', () => {
render(
<QuestionPhone
Expand Down
10 changes: 9 additions & 1 deletion src/Questions/Phone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import { jsx } from 'theme-ui'
const QuestionPhone = ({ isMobile, isoCode, question, useForm, ...props }) => {
const { clearErrors, errors, register, setError, setValue } = useForm

const getDefaultCountry = () => {
if (question?.defaultCountry && question?.defaultCountry !== '')
return question?.defaultCountry.toUpperCase()

if (isoCode) return isoCode.toUpperCase()

return ''
}
return (
<React.Fragment>
<div
Expand All @@ -24,7 +32,7 @@ const QuestionPhone = ({ isMobile, isoCode, question, useForm, ...props }) => {
id={question.name}
data-testid='question-phone'
aria-describedby={'error_message_' + question.name}
defaultCountry={isoCode ? isoCode.toUpperCase() : ''}
defaultCountry={getDefaultCountry()}
register={register}
setValue={setValue}
setError={setError}
Expand Down

0 comments on commit 37843c6

Please sign in to comment.