| 1 |
import { Children, cloneElement } from 'react'; |
| 2 |
import { css } from '@patternfly/react-styles'; |
| 3 |
import styles from '@patternfly/react-styles/css/components/DragDrop/drag-drop'; |
| 4 |
import { DroppableContext } from './DroppableContext'; |
| 5 |
|
| 6 |
interface DroppableProps extends React.HTMLProps<HTMLDivElement> { |
| 7 |
/** Content rendered inside DragDrop */ |
| 8 |
children?: React.ReactNode; |
| 9 |
/** Class to add to outer div */ |
| 10 |
className?: string; |
| 11 |
/** Name of zone that items can be dragged between. Should specify if there is more than one Droppable on the page. */ |
| 12 |
zone?: string; |
| 13 |
/** Id to be passed back on drop events */ |
| 14 |
droppableId?: string; |
| 15 |
/** Don't wrap the component in a div. Requires passing a single child. */ |
| 16 |
hasNoWrapper?: boolean; |
| 17 |
} |
| 18 |
|
| 19 |
export const Droppable: React.FunctionComponent<DroppableProps> = ({ |
| 20 |
className, |
| 21 |
children, |
| 22 |
zone = 'defaultZone', |
| 23 |
droppableId = 'defaultId', |
| 24 |
hasNoWrapper = false, |
| 25 |
...props |
| 26 |
}: DroppableProps) => { |
| 27 |
const childProps = { |
| 28 |
'data-pf-droppable': zone, |
| 29 |
'data-pf-droppableid': droppableId, |
| 30 |
// if has no wrapper is set, don't overwrite children className with the className prop |
| 31 |
className: |
| 32 |
hasNoWrapper && Children.count(children) === 1 |
| 33 |
? css(styles.droppable, className, (children as React.ReactElement<any>).props.className) |
| 34 |
: css(styles.droppable, className), |
| 35 |
...props |
| 36 |
}; |
| 37 |
|
| 38 |
return ( |
| 39 |
<DroppableContext.Provider value={{ zone, droppableId }}> |
| 40 |
{hasNoWrapper ? ( |
| 41 |
cloneElement(children as React.ReactElement<any>, childProps) |
| 42 |
) : ( |
| 43 |
<div {...childProps}>{children}</div> |
| 44 |
)} |
| 45 |
</DroppableContext.Provider> |
| 46 |
); |
| 47 |
}; |
| 48 |
Droppable.displayName = 'Droppable'; |