Skip to content

Commit

Permalink
fix: multi value draggable/sortable pills (#6500)
Browse files Browse the repository at this point in the history
  • Loading branch information
JarrodMFlesch authored May 29, 2024
1 parent 4a51f4d commit e749529
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
10 changes: 7 additions & 3 deletions packages/ui/src/elements/ReactSelect/MultiValue/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,22 @@ import { useDraggableSortable } from '../../DraggableSortable/useDraggableSortab
import './index.scss'

const baseClass = 'multi-value'

export function generateMultiValueDraggableID(optionData, valueFunction) {
return typeof valueFunction === 'function' ? valueFunction(optionData) : optionData.value
}
export const MultiValue: React.FC<MultiValueProps<Option>> = (props) => {
const {
className,
data: { value },
data,
innerProps,
isDisabled,
// @ts-expect-error // TODO Fix this - moduleResolution 16 breaks our declare module
selectProps: { customProps: { disableMouseDown } = {}, isSortable } = {},
selectProps: { customProps: { disableMouseDown } = {}, getOptionValue, isSortable } = {},
} = props

const { attributes, isDragging, listeners, setNodeRef, transform } = useDraggableSortable({
id: typeof value === 'string' && value.toString(),
id: generateMultiValueDraggableID(data, getOptionValue),
disabled: !isSortable,
})

Expand Down
17 changes: 9 additions & 8 deletions packages/ui/src/elements/ReactSelect/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { DraggableSortable } from '../DraggableSortable/index.js'
import { ClearIndicator } from './ClearIndicator/index.js'
import { Control } from './Control/index.js'
import { DropdownIndicator } from './DropdownIndicator/index.js'
import { MultiValue } from './MultiValue/index.js'
import { MultiValue, generateMultiValueDraggableID } from './MultiValue/index.js'
import { MultiValueLabel } from './MultiValueLabel/index.js'
import { MultiValueRemove } from './MultiValueRemove/index.js'
import { SingleValue } from './SingleValue/index.js'
Expand Down Expand Up @@ -172,18 +172,19 @@ const SelectAdapter: React.FC<ReactSelectAdapterProps> = (props) => {
}

const SortableSelect: React.FC<ReactSelectAdapterProps> = (props) => {
const { onChange, value } = props
const { getOptionValue, onChange, value } = props

let ids: string[] = []
if (value)
ids = Array.isArray(value)
? value.map((item) => item?.id ?? `${item?.value}`)
: [value?.id || `${value?.value}`]
let draggableIDs: string[] = []
if (value) {
draggableIDs = (Array.isArray(value) ? value : [value]).map((optionValue) => {
return generateMultiValueDraggableID(optionValue, getOptionValue)
})
}

return (
<DraggableSortable
className="react-select-container"
ids={ids}
ids={draggableIDs}
onDragEnd={({ moveFromIndex, moveToIndex }) => {
let sorted = value
if (value && Array.isArray(value)) {
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/fields/Relationship/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ const RelationshipField: React.FC<RelationshipFieldProps> = (props) => {
disabled={readOnly || formProcessing || drawerIsOpen}
filterOption={enableWordBoundarySearch ? filterOption : undefined}
getOptionValue={(option) => {
if (!option) return undefined
return hasMany && Array.isArray(relationTo)
? `${option.relationTo}_${option.value}`
: option.value
Expand Down

0 comments on commit e749529

Please sign in to comment.