-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from tomonarifeehan/refactor-custom-select
fix: refactored-custom-select
- Loading branch information
Showing
3 changed files
with
57 additions
and
60 deletions.
There are no files selected for viewing
56 changes: 0 additions & 56 deletions
56
src/UI/FormComponents/FieldTypes/CustomSelectComponent.jsx
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import React from 'react' | ||
import Select from 'react-select' | ||
|
||
import { Field } from 'formik' | ||
|
||
const CustomSelectComponent = ({ | ||
className, | ||
placeholder, | ||
field, | ||
form, | ||
options, | ||
isMulti = false, | ||
...props | ||
}) => { | ||
|
||
const onChange = (option) => { | ||
form.setFieldValue( | ||
field.name, | ||
isMulti ? option.map((item) => item.value) : option.value | ||
) | ||
} | ||
|
||
const getValue = () => { | ||
if (options) { | ||
|
||
const fieldValue = Array.isArray(field.value) ? field.value : [] | ||
return isMulti ? options.filter(option => fieldValue.includes(option.value)) : options.find(option => option.value === field.value) | ||
|
||
} else { | ||
return isMulti ? [] : "" | ||
} | ||
} | ||
|
||
return ( | ||
<Select | ||
className = { className } | ||
name = { field.name } | ||
value = { getValue() } | ||
onChange = { onChange } | ||
placeholder = { placeholder } | ||
options = { options } | ||
isMulti = { isMulti } | ||
{ ...props } | ||
/> | ||
) | ||
} | ||
|
||
// Formik field wrapper for custom select. | ||
const CustomSelect = (props) => ( | ||
<Field component = { CustomSelectComponent } { ...props } /> | ||
) | ||
|
||
export default CustomSelect |