Skip to content

Commit

Permalink
Merge pull request #56 from tomonarifeehan/refactor-custom-select
Browse files Browse the repository at this point in the history
fix: refactored-custom-select
  • Loading branch information
MintOwlTech authored May 28, 2024
2 parents 339230d + 13ade06 commit 07e7845
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 60 deletions.
56 changes: 0 additions & 56 deletions src/UI/FormComponents/FieldTypes/CustomSelectComponent.jsx

This file was deleted.

8 changes: 4 additions & 4 deletions src/UI/FormComponents/FieldTypes/SmartFolderField.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import CustomSelect from "./CustomSelectComponent"
import CustomSelect from "../../../components/ui/custom-select"
import React from "react"

const extractSecondLevelKeys = (obj) => {

const options = [];
const options = []

const traverse = (obj, level = 0) => {
if (level === 1) {
Expand All @@ -21,7 +21,7 @@ const extractSecondLevelKeys = (obj) => {

traverse(obj)

return options;
return options
}

export function SmartFolderField({ folder, folderContent }) {
Expand All @@ -43,4 +43,4 @@ export function SmartFolderField({ folder, folderContent }) {
isMulti = { isMultipleFolder }
/>
)
}
}
53 changes: 53 additions & 0 deletions src/components/ui/custom-select.jsx
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

0 comments on commit 07e7845

Please sign in to comment.