forked from binary-com/deriv-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
10295fc
commit c0a1dfa
Showing
11 changed files
with
136 additions
and
45 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Empty file.
35 changes: 35 additions & 0 deletions
35
packages/account/src/Components/forms/form-fields/__tests__/form-input-field.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import FormInputField from '../form-input-field'; | ||
import { Formik } from 'formik'; | ||
|
||
describe('Tesing <FormInputField/> component', () => { | ||
it('should render properties', () => { | ||
const props = { | ||
name: 'test-name', | ||
optional: true, | ||
}; | ||
render( | ||
<Formik initialValues={{}} onSubmit={jest.fn()}> | ||
<FormInputField {...props} /> | ||
</Formik> | ||
); | ||
|
||
expect(screen.getByRole('textbox')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should render Input field with optional status', () => { | ||
const props = { | ||
name: 'test-name', | ||
optional: true, | ||
}; | ||
render( | ||
<Formik initialValues={{}} onSubmit={jest.fn()}> | ||
<FormInputField {...props} /> | ||
</Formik> | ||
); | ||
|
||
expect(screen.getByRole('textbox')).toBeInTheDocument(); | ||
expect(screen.getByRole('textbox')).not.toBeRequired(); | ||
}); | ||
}); |
46 changes: 46 additions & 0 deletions
46
packages/account/src/Components/forms/form-fields/date-of-birth-field.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import React from 'react'; | ||
import { Field, FieldInputProps, FormikHelpers, FormikState } from 'formik'; | ||
import { DateOfBirthPicker, TDatePickerOnChangeEvent } from '@deriv/components'; | ||
import { toMoment } from '@deriv/shared'; | ||
|
||
type TDateOfBirthFieldProps = { | ||
name: string; | ||
portal_id: string; | ||
} & React.ComponentProps<typeof DateOfBirthPicker>; | ||
|
||
type TDateOfBirthFieldHelpers = { | ||
field: FieldInputProps<string | moment.Moment>; | ||
form: FormikHelpers<{ date_of_birth: string | moment.Moment }> & | ||
FormikState<{ date_of_birth: string | moment.Moment }>; | ||
}; | ||
|
||
/** | ||
* DateOfBirthField is a wrapper around DateOfBirthPicker that can be used with Formik. | ||
* @name DateOfBirthField | ||
* @param {string} name - Name of the field | ||
* @param {string} portal_id - Portal ID | ||
* @param {TDatePicker} [props] - Other props to pass to DateOfBirthPicker | ||
* @returns {React.ReactNode} | ||
*/ | ||
const DateOfBirthField = ({ name, portal_id, ...rest }: TDateOfBirthFieldProps) => ( | ||
<Field name={name}> | ||
{({ | ||
field: { value }, | ||
form: { setFieldValue, errors, touched, setFieldTouched }, | ||
}: TDateOfBirthFieldHelpers) => ( | ||
<DateOfBirthPicker | ||
{...rest} | ||
error={touched.date_of_birth && errors.date_of_birth ? errors.date_of_birth : undefined} | ||
name={name} | ||
onBlur={() => setFieldTouched(name)} | ||
onChange={({ target }: TDatePickerOnChangeEvent) => | ||
setFieldValue(name, target?.value ? toMoment(target.value).format('YYYY-MM-DD') : '', true) | ||
} | ||
value={value} | ||
portal_id={portal_id} | ||
/> | ||
)} | ||
</Field> | ||
); | ||
|
||
export default DateOfBirthField; |
41 changes: 41 additions & 0 deletions
41
packages/account/src/Components/forms/form-fields/form-input-field.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import React from 'react'; | ||
import { FieldInputProps, FormikHelpers, FormikState, Field } from 'formik'; | ||
import { Input } from '@deriv/components'; | ||
|
||
type FormInputFieldProps = { | ||
name: string; | ||
optional?: boolean; | ||
warn?: string; | ||
} & React.ComponentProps<typeof Input>; | ||
|
||
type TFormInputFieldHelpers = { | ||
field: FieldInputProps<string>; | ||
form: FormikHelpers<Record<string, string>> & FormikState<Record<string, string>>; | ||
}; | ||
|
||
/** | ||
* FormInputField is a wrapper around Input that can be used with Formik. | ||
* @name FormInputField | ||
* @param {string} name - Name of the field | ||
* @param {boolean} [optional] - Whether the field is optional | ||
* @param {string} [warn] - Display a warning message | ||
* @param {Input} [props] - Other props to pass to Input | ||
* @returns {React.ReactNode} | ||
*/ | ||
const FormInputField = ({ name, optional = false, warn, ...rest }: FormInputFieldProps) => ( | ||
<Field name={name}> | ||
{({ field, form: { errors, touched } }: TFormInputFieldHelpers) => ( | ||
<Input | ||
{...field} | ||
{...rest} | ||
type='text' | ||
required={!optional} | ||
autoComplete='off' | ||
error={touched[field.name] && errors[field.name] ? errors[field.name] : undefined} | ||
warn={warn} | ||
/> | ||
)} | ||
</Field> | ||
); | ||
|
||
export default FormInputField; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import FormInputField from './form-input-field'; | ||
import DateOfBirthField from './date-of-birth-field'; | ||
|
||
export { FormInputField, DateOfBirthField }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
import DatePicker from './date-picker'; | ||
import './date-picker.scss'; | ||
|
||
export default DatePicker; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters