Skip to content

Commit

Permalink
Make it possible to pass mosaicId in
Browse files Browse the repository at this point in the history
  • Loading branch information
nomcopter committed May 21, 2020
1 parent c4e4fe4 commit 73b1903
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -193,6 +193,12 @@ export interface MosaicBaseProps<T extends MosaicKey> {
* default: Simple NonIdealState view
*/
zeroStateView?: JSX.Element;
/**
* Override the mosaicId passed to `react-dnd` to control how drag and drop works with other components
* Note: does not support updating after instantiation
* default: Random UUID
*/
mosaicId?: string;
}

export interface MosaicControlledProps<T extends MosaicKey> extends MosaicBaseProps<T> {
Expand Down
12 changes: 11 additions & 1 deletion src/Mosaic.tsx
Expand Up @@ -46,6 +46,12 @@ export interface MosaicBaseProps<T extends MosaicKey> {
* default: Simple NonIdealState view
*/
zeroStateView?: JSX.Element;
/**
* Override the mosaicId passed to `react-dnd` to control how drag and drop works with other components
* Note: does not support updating after instantiation
* default: Random UUID
*/
mosaicId?: string;
}

export interface MosaicControlledProps<T extends MosaicKey> extends MosaicBaseProps<T> {
Expand Down Expand Up @@ -89,6 +95,10 @@ export class MosaicWithoutDragDropContext<T extends MosaicKey = string> extends
nextProps: Readonly<MosaicProps<MosaicKey>>,
prevState: MosaicState<MosaicKey>,
): Partial<MosaicState<MosaicKey>> | null {
if (nextProps.mosaicId && prevState.mosaicId !== nextProps.mosaicId && process.env.NODE_ENV !== 'production') {
throw new Error('Mosaic does not support updating the mosaicId after instantiation');
}

if (isUncontrolled(nextProps) && nextProps.initialValue !== prevState.lastInitialValue) {
return {
lastInitialValue: nextProps.initialValue,
Expand All @@ -102,7 +112,7 @@ export class MosaicWithoutDragDropContext<T extends MosaicKey = string> extends
state: MosaicState<T> = {
currentNode: null,
lastInitialValue: null,
mosaicId: uuid(),
mosaicId: this.props.mosaicId ?? uuid(),
};

render() {
Expand Down
2 changes: 1 addition & 1 deletion src/MosaicWindow.tsx
Expand Up @@ -14,7 +14,7 @@ import {
DropTarget,
} from 'react-dnd';

import { DEFAULT_CONTROLS_WITH_CREATION, DEFAULT_CONTROLS_WITHOUT_CREATION } from './buttons/defaultToolbarControls';
import { DEFAULT_CONTROLS_WITHOUT_CREATION, DEFAULT_CONTROLS_WITH_CREATION } from './buttons/defaultToolbarControls';
import { Separator } from './buttons/Separator';
import { MosaicContext, MosaicWindowContext } from './contextTypes';
import { MosaicDragItem, MosaicDropData, MosaicDropTargetPosition } from './internalTypes';
Expand Down

0 comments on commit 73b1903

Please sign in to comment.