Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | 11x 11x 11x 11x 11x 11x 11x 11x 7x 117x 7x 7x 7x 7x 7x 7x 7x 117x 7x 11x | /*
* Copyright 2021 Harness Inc. All rights reserved.
* Use of this source code is governed by the PolyForm Shield 1.0.0 license
* that can be found in the licenses directory at the root of this repository, also available at
* https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
*/
import React, { useCallback, useState } from 'react'
import { Container, Icon, Checkbox, TextInput } from '@wings-software/uicore'
import cx from 'classnames'
import { debounce } from 'lodash-es'
import { Virtuoso } from 'react-virtuoso'
import type { Maybe } from 'services/ce/services'
import { useStrings } from 'framework/strings'
import css from './MultiValueSelectorComponent.module.scss'
interface MultiValueSelectorComponentProps {
fetching: boolean
valueList: Maybe<string>[]
shouldFetchMore?: boolean
setSelectedValues: React.Dispatch<React.SetStateAction<Record<string, boolean>>>
selectedValues: Record<string, boolean>
fetchMore?: (e: number) => void
onInputChange: (val: string) => void
searchText?: string
}
const MultiValueSelectorComponent: (props: MultiValueSelectorComponentProps) => JSX.Element = ({
fetching,
valueList,
shouldFetchMore,
setSelectedValues,
selectedValues,
fetchMore,
onInputChange,
searchText: intialSearchVal
}) => {
const { getString } = useStrings()
const filteredValues = valueList.filter(val => val) as string[]
const [searchText, setSearchText] = useState(intialSearchVal || '')
const renderValues = () => {
const allowToFetchMore = shouldFetchMore
return (
<Container>
<Virtuoso
style={{ height: 344 }}
data={filteredValues}
overscan={{ main: 20, reverse: 20 }}
endReached={e => {
allowToFetchMore && fetchMore && fetchMore(e)
}}
itemContent={(_, value) => {
if (!value) return null
return (
<Checkbox
onClick={() => {
setSelectedValues(prevVal => ({
...prevVal,
[value]: !prevVal[value]
}))
}}
checked={selectedValues[value]}
className={cx(css.checkbox, css.labelItem)}
key={value}
value={value}
label={value}
/>
)
}}
components={{
Footer: function renderFooter() {
return allowToFetchMore ? (
<Container padding="small" className={css.fetchingMoreLoader}>
<Icon name="spinner" size={20} color="blue500" />
</Container>
) : null
}
}}
/>
</Container>
)
}
const debouncedOnChange = useCallback(debounce(onInputChange, 500), [])
const handleInputChange: (e: any) => void = e => {
const target = e.target
setSearchText(target.value)
debouncedOnChange(target.value)
}
const isSelectAllChecked = filteredValues.filter(val => selectedValues[val]).length === filteredValues.length
return (
<Container className={css.valueContainer}>
<Container className={css.searchBoxContainer}>
<div className={css.searchCheckBox}>
<Checkbox
className={css.checkbox}
disabled={fetching || !filteredValues.length}
checked={isSelectAllChecked}
onChange={() => {
const selectAllValues: Record<string, boolean> = {}
filteredValues.forEach(val => {
if (val) {
selectAllValues[val] = !isSelectAllChecked
}
})
setSelectedValues(prevVal => ({
...prevVal,
...selectAllValues
}))
}}
/>
</div>
<TextInput
value={searchText}
onChange={handleInputChange}
placeholder={getString('ce.perspectives.createPerspective.filters.searchText')}
/>
</Container>
{fetching ? (
<Container className={css.valueFetching}>
<Icon name="spinner" size={28} color="blue500" />
</Container>
) : (
renderValues()
)}
</Container>
)
}
export default MultiValueSelectorComponent
|