Skip to content

Commit

Permalink
fix: Flatten array before parsing DEV-53
Browse files Browse the repository at this point in the history
  • Loading branch information
giubatt committed May 19, 2021
1 parent 56b7356 commit f88a851
Showing 1 changed file with 28 additions and 20 deletions.
48 changes: 28 additions & 20 deletions src/components/Select/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,37 @@ import { toArray } from "../../../helpers/utils"
import { SelectOption } from "../Select"

export function getDataFromChildren(nodes: React.ReactNode) {
return toArray(nodes)
.map((node) => {
if (!React.isValidElement(node) || !node.type) {
return null
}
return (
toArray(nodes)
// eslint-disable-next-line unicorn/no-array-reduce
.reduce<Array<React.ReactNode>>((accumulator, node) => {
if (Array.isArray(node)) return [...accumulator, ...node]
return [...accumulator, node]
}, [])
.map((node) => {
if (!React.isValidElement(node) || !node.type) {
return null
}

const {
type: { isSelectOption, isSelectOptionCustom },
props: { children, value, label: propLabel },
} = node as React.ReactElement & {
type: { isSelectOption: boolean; isSelectOptionCustom: boolean }
}
if (!isSelectOption && !isSelectOptionCustom) return null
const {
type: { isSelectOption, isSelectOptionCustom },
props: { children, value, label: propLabel },
} = node as React.ReactElement & {
type: { isSelectOption: boolean; isSelectOptionCustom: boolean }
}
if (!isSelectOption && !isSelectOptionCustom) return null

let label = children
if (isSelectOptionCustom) label = propLabel || "Custom..."
let label = children
if (isSelectOptionCustom) label = propLabel || "Custom..."

return {
label,
value: value,
} as SelectOption
})
.filter((data) => data) as SelectOption[]
return {
label,
value: value,
isCustom: isSelectOptionCustom,
} as SelectOption
})
.filter((data) => data) as SelectOption[]
)
}

export function isArrayEqual<T = number | string>(
Expand Down

0 comments on commit f88a851

Please sign in to comment.