Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/lib/components/FormChipCell/FormChip/formChip.scss
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

&.chips_button {
padding: 8px 7px;
width: max-content;
}

&-background {
Expand Down
5 changes: 3 additions & 2 deletions src/lib/components/FormChipCell/FormChipCell.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import PropTypes from 'prop-types'

import FormChipCellView from './FormChipCellView'

import { CHIP_OPTIONS } from '../../types'
import { CHIP_OPTIONS, VISIBLE_CHIPS_MAX_LENGTH } from '../../types'
import { CLICK, TAB, TAB_SHIFT } from '../../constants'
import { areArraysEqual } from '../../utils/common.util'
import { checkPatternsValidity } from '../../utils/validation.util'
Expand Down Expand Up @@ -388,6 +388,7 @@ let FormChipCell = ({
showHiddenChips={showHiddenChips}
validateFields={validateFields}
validationRules={validationRules}
visibleChipsMaxLength={visibleChipsMaxLength}
/>
</div>
</div>
Expand All @@ -408,7 +409,7 @@ FormChipCell.propTypes = {
shortChips: PropTypes.bool,
validationRules: PropTypes.object,
validator: PropTypes.func,
visibleChipsMaxLength: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
visibleChipsMaxLength: VISIBLE_CHIPS_MAX_LENGTH,
withInitialParentWidth: PropTypes.bool
}

Expand Down
11 changes: 7 additions & 4 deletions src/lib/components/FormChipCell/FormChipCellView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import HiddenChipsBlock from './HiddenChipsBlock/HiddenChipsBlock'
import TextTooltipTemplate from '../TooltipTemplate/TextTooltipTemplate'
import Tooltip from '../Tooltip/Tooltip'

import { CHIP_OPTIONS } from '../../types'
import { CHIP_OPTIONS, VISIBLE_CHIPS_MAX_LENGTH } from '../../types'
import { isEveryObjectValueEmpty } from '../../utils/common.util'
import { uniquenessError } from './formChipCell.util'

Expand Down Expand Up @@ -58,7 +58,8 @@ let FormChipCellView = (
showChips,
showHiddenChips,
validateFields,
validationRules = {}
validationRules = {},
visibleChipsMaxLength = null
},
{ chipsCellRef, chipsWrapperRef, hiddenChipsCounterRef, hiddenChipsPopUpRef }
) => {
Expand All @@ -72,7 +73,8 @@ let FormChipCellView = (
const wrapperClassNames = classnames(
'chips-wrapper',
isEditable && 'fixed-max-width',
chips.visibleChips?.length > 0 && !chipSizeIsRecalculated && 'chip_invisible'
chips.visibleChips?.length > 0 && !chipSizeIsRecalculated && 'chip_invisible',
visibleChipsMaxLength === 'all' && 'chips-wrapper_all-visible'
)
const chipClassNames = classnames(
'chip',
Expand Down Expand Up @@ -230,7 +232,8 @@ FormChipCellView.propTypes = {
showChips: PropTypes.bool.isRequired,
showHiddenChips: PropTypes.bool.isRequired,
validateFields: PropTypes.func.isRequired,
validationRules: PropTypes.object
validationRules: PropTypes.object,
visibleChipsMaxLength: VISIBLE_CHIPS_MAX_LENGTH
}

export default FormChipCellView
9 changes: 8 additions & 1 deletion src/lib/components/FormChipCell/formChipCell.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

& {
height: auto;
min-width: 0;
}

&__wrapper {
Expand All @@ -13,8 +14,14 @@

&-wrapper {
display: flex;
flex-flow: row wrap;
flex-flow: row;
align-items: center;
overflow: hidden;

&.chips-wrapper_all-visible {
flex-wrap: wrap;
overflow: auto;
}
}

&-cell {
Expand Down
47 changes: 29 additions & 18 deletions src/lib/hooks/useChipCell.hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,31 +118,42 @@ export const useChipCell = (isEditMode, visibleChipsMaxLength, withInitialParent
const parentSize = withInitialParentWidth
? chipCellInitialWidth
: chipsCellRef.current?.getBoundingClientRect().width
const hiddenChipsCounterWidth =
(hiddenChipsCounterRef.current?.getBoundingClientRect().width ?? 0) + chipBlockMarginRight

let maxLength = 0
let chipsLengthSum = 0
let chipIndex = 0
const padding = 65

Object.values(chipsSizes).every((chipSize, index) => {
// Check if adding chipSize to maxLength exceeds parentSize
// or if adding chipSize and padding exceeds parentSize when there are multiple chips
if (
maxLength + chipSize > parentSize ||
(Object.values(chipsSizes).length > 1 &&
maxLength + chipSize + chipBlockMarginRight + padding > parentSize)
) {
chipIndex = index
const chipsSizesList = Object.values(chipsSizes)

chipsSizesList.every((chipSize, index) => {
const chipSizeWithMargin = chipSize + chipBlockMarginRight
const isLastChip = index === chipsSizesList.length - 1
const newChipsLengthSum = chipsLengthSum + chipSizeWithMargin
const wouldExceedWithCounter = newChipsLengthSum + hiddenChipsCounterWidth > parentSize
const wouldExceedWithoutCounter = newChipsLengthSum > parentSize

// If we've exceeded the limit
if (wouldExceedWithCounter) {
// Special case: last chip might fit without the counter
if (isLastChip && !wouldExceedWithoutCounter) {
chipsLengthSum = newChipsLengthSum
chipIndex = chipsSizesList.length
return true
}

// Stop here - this chip doesn't fit
chipIndex = index
return false
} else {
maxLength += chipSize
}

if (index === Object.values(chipsSizes).length - 1) {
chipIndex = 8
}
// Chip fits, add it
chipsLengthSum = newChipsLengthSum

return true
if (isLastChip) {
chipIndex = chipsSizesList.length
}

return true
})

setVisibleChipsCount(chipIndex)
Expand Down
5 changes: 5 additions & 0 deletions src/lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ export const CHIP_OPTIONS = PropTypes.shape({

export const CHIPS = PropTypes.arrayOf(CHIP)

export const VISIBLE_CHIPS_MAX_LENGTH = PropTypes.oneOfType([
PropTypes.number,
PropTypes.oneOf(['all'])
])

export const POP_UP_CUSTOM_POSITION = PropTypes.shape({
element: PropTypes.shape({}),
position: PropTypes.oneOf(['top-left', 'top-right', 'bottom-left', 'bottom-right']),
Expand Down
Loading