All files / modules/10-common/components/BranchSelector BranchSelector.tsx

0% Statements 0/23
0% Branches 0/7
0% Functions 0/7
0% Lines 0/22

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                                                                                                                                                                                               
/*
 * 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, { useState } from 'react'
import { Select, ItemListRenderer } from '@blueprintjs/select'
import { Button, MenuItem, Menu } from '@blueprintjs/core'
import cx from 'classnames'
 
import { Text } from '@wings-software/uicore'
import { useStrings } from 'framework/strings'
 
import css from './BranchSelector.module.scss'
 
export interface Branch {
  name: string
}
interface BranchSelectorProps {
  branches: Branch[]
  currentBranch?: Branch
  isReadOnlyMode?: boolean
}
 
const BranchSelect = Select.ofType<Branch>()
 
export const BranchSelector = ({
  branches,
  currentBranch,
  isReadOnlyMode = false
}: BranchSelectorProps): JSX.Element => {
  const [selectedBranch, setSelectedBranch] = useState<Branch | undefined>(currentBranch)
  const { getString } = useStrings()
 
  const handleSelect = (item: Branch, _event?: React.SyntheticEvent<HTMLElement, Event> | undefined) => {
    setSelectedBranch(item)
  }
 
  const itemRenderer = (item: Branch): JSX.Element => {
    const { name } = item
    return (
      <Text lineClamp={1} width="70%">
        {name}
      </Text>
    )
  }
 
  const itemListRender: ItemListRenderer<Branch> = itemListProps => (
    <Menu>
      {selectedBranch ? (
        <Menu.Item
          text={<Text>{getString('common.clearSelection')}</Text>}
          icon="cross"
          onClick={() => setSelectedBranch(undefined)}
          className={css.clear}
        />
      ) : null}
      {itemListProps.items.map((item, i) => itemListProps.renderItem(item, i))}
    </Menu>
  )
 
  return (
    <BranchSelect
      items={branches || []}
      className={css.branchSelect}
      onItemSelect={handleSelect}
      itemRenderer={(item, { handleClick }) => {
        return <MenuItem text={itemRenderer(item)} onClick={handleClick} key={item.name} />
      }}
      itemListRenderer={itemListRender}
      noResults={<Text padding="small">{getString('noSearchResultsFoundPeriod')}</Text>}
      activeItem={selectedBranch}
      inputProps={{
        placeholder: getString('search')
      }}
      disabled={isReadOnlyMode}
    >
      <Button
        className={cx(css.branchSelect, { [css.disable]: isReadOnlyMode })}
        rightIcon="chevron-down"
        data-testid="branch-select"
      >
        {selectedBranch ? (
          <Text lineClamp={1} alwaysShowTooltip={false}>
            {selectedBranch.name}
          </Text>
        ) : (
          <Text>{getString('common.selectBranch')}</Text>
        )}
      </Button>
    </BranchSelect>
  )
}