Skip to content

Commit

Permalink
feat(pieces): customize pieces via render prop [skip release]
Browse files Browse the repository at this point in the history
  • Loading branch information
willb335 committed Jul 17, 2018
1 parent a74e96b commit 36a66f3
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 174 deletions.
1 change: 0 additions & 1 deletion src/Chessboard/Board.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ class Board extends Component {
orientation={context.orientation}
id={context.id}
setTouchState={context.setTouchState}
renderPieces={context.renderPieces}
wasManuallyDropped={context.wasManuallyDropped}
/>
) : null}
Expand Down
20 changes: 13 additions & 7 deletions src/Chessboard/CustomDragLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { DragLayer } from 'react-dnd';

import { renderChessPieces } from './RenderPieces';
import { renderChessPiece } from './Piece';

class CustomDragLayer extends Component {
static propTypes = {
Expand All @@ -15,7 +15,8 @@ class CustomDragLayer extends Component {
width: PropTypes.number,
pieces: PropTypes.object,
id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
wasPieceTouched: PropTypes.bool
wasPieceTouched: PropTypes.bool,
sourceSquare: PropTypes.string
};

render() {
Expand All @@ -26,16 +27,21 @@ class CustomDragLayer extends Component {
id,
currentOffset,
wasPieceTouched,
pieces
pieces,
sourceSquare
} = this.props;

return isDragging && item.board === id ? (
<div style={layerStyles}>
<div style={getItemStyles(currentOffset, wasPieceTouched)}>
{renderChessPieces(
{ width, pieces, piece: item.piece },
{ svgStyles: { width: width / 8, height: width / 8 } }
)}
{renderChessPiece({
width,
pieces,
piece: item.piece,
isDragging,
customDragLayerStyles: { opacity: 1 },
sourceSquare
})}
</div>
</div>
) : null;
Expand Down
15 changes: 7 additions & 8 deletions src/Chessboard/PhantomPiece.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'; // eslint-disable-line no-unused-vars
import PropTypes from 'prop-types';

import { renderChessPieces } from './RenderPieces';
import { renderChessPiece } from './Piece';

PhantomPiece.propTypes = {
width: PropTypes.number,
Expand All @@ -10,13 +10,12 @@ PhantomPiece.propTypes = {
};

function PhantomPiece({ width, pieces, phantomPieceValue }) {
return renderChessPieces(
{ width, pieces, piece: phantomPieceValue },
{
imgStyles: phantomPieceStyles(width),
svgStyles: phantomPieceStyles(width)
}
);
return renderChessPiece({
width,
pieces,
piece: phantomPieceValue,
phantomPieceStyles: phantomPieceStyles(width)
});
}

export default PhantomPiece;
Expand Down
146 changes: 129 additions & 17 deletions src/Chessboard/Piece.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,66 @@
import React, { Component } from 'react'; // eslint-disable-line no-unused-vars
import React, { Component } from 'react';
import { DragSource } from 'react-dnd';
import PropTypes from 'prop-types';
import { getEmptyImage } from 'react-dnd-html5-backend';

import { ItemTypes } from './helpers';

/* eslint react/prop-types: 0 */
export const renderChessPiece = ({
currentSquare,
targetSquare,
waitForTransition,
getSquareCoordinates,
piece,
width,
pieces,
transitionDuration,
isDragging,
sourceSquare,
customDragLayerStyles = {},
phantomPieceStyles = {}
}) => {
const renderChessPieceArgs = {
// set the width and height to the square's width and height
width: width / 8,
height: width / 8,
isDragging,
// undefined when dragging
currentSquare,
sourceSquare
};

return (
<div
data-testid={`${piece}-${currentSquare}`}
style={{
...pieceStyles(
{
isDragging,
transitionDuration,
waitForTransition,
currentSquare,
targetSquare,
sourceSquare,
getSquareCoordinates
},
getTranslation
),
...phantomPieceStyles,
...customDragLayerStyles
}}
>
{typeof pieces[piece] === 'function' ? (
pieces[piece](renderChessPieceArgs)
) : (
<svg viewBox={`1 1 43 43`} width={width / 8} height={width / 8}>
<g>{pieces[piece]}</g>
</svg>
)}
</div>
);
};

class Piece extends Component {
static propTypes = {
piece: PropTypes.string,
Expand All @@ -22,8 +78,7 @@ class Piece extends Component {
sourceSquare: PropTypes.string,
targetSquare: PropTypes.string,
waitForTransition: PropTypes.bool,
setTouchState: PropTypes.func,
renderPieces: PropTypes.func
setTouchState: PropTypes.func
};

componentDidMount() {
Expand All @@ -50,24 +105,22 @@ class Piece extends Component {
transitionDuration,
isDragging,
connectDragSource,
renderPieces,
sourceSquare
} = this.props;

return connectDragSource(
renderPieces &&
renderPieces({
currentSquare,
targetSquare,
waitForTransition,
getSquareCoordinates,
piece,
width,
pieces,
transitionDuration,
isDragging,
sourceSquare
})
renderChessPiece({
currentSquare,
targetSquare,
waitForTransition,
getSquareCoordinates,
piece,
width,
pieces,
transitionDuration,
isDragging,
sourceSquare
})
);
}
}
Expand Down Expand Up @@ -111,10 +164,12 @@ const pieceSource = {
return onDrop(props.currentSquare, dropResults.target);
}
// set new position

setPosition(piece, currentSquare, dropResults.target);
}
}
};

function collect(connect, monitor) {
return {
connectDragSource: connect.dragSource(),
Expand All @@ -125,3 +180,60 @@ function collect(connect, monitor) {
}

export default DragSource(ItemTypes.PIECE, pieceSource, collect)(Piece);

const isActivePiece = (currentSquare, targetSquare) =>
targetSquare && targetSquare === currentSquare;

const getTransitionCoordinates = ({
getSquareCoordinates,
sourceSq,
targetSq
}) => {
const transitionCoordinates = getSquareCoordinates(sourceSq, targetSq);
const { sourceSquare, targetSquare } = transitionCoordinates;

return `translate(${sourceSquare.x - targetSquare.x}px, ${sourceSquare.y -
targetSquare.y}px)`;
};

const getTranslation = ({
waitForTransition,
currentSquare,
targetSquare,
sourceSquare,
getSquareCoordinates
}) => {
return (
isActivePiece(currentSquare, targetSquare) &&
waitForTransition &&
getTransitionCoordinates({
getSquareCoordinates,
sourceSq: sourceSquare,
targetSq: targetSquare
})
);
};

const pieceStyles = (
{
isDragging,
transitionDuration,
waitForTransition,
currentSquare,
targetSquare,
sourceSquare,
getSquareCoordinates
},
getTranslation
) => ({
opacity: isDragging ? 0 : 1,
transform: getTranslation({
waitForTransition,
currentSquare,
targetSquare,
sourceSquare,
getSquareCoordinates
}),
transition: `transform ${transitionDuration}ms`,
zIndex: 5
});
124 changes: 0 additions & 124 deletions src/Chessboard/RenderPieces.js

This file was deleted.

Loading

0 comments on commit 36a66f3

Please sign in to comment.