Drag and Drop players in the editor #11
Merged
maxime.batista
merged 12 commits from editor/place-players
into master
1 year ago
Loading…
Reference in new issue
There is no content yet.
Delete Branch 'editor/place-players'
Deleting a branch is permanent. It CANNOT be undone. Continue?
This pull request adds a court in the tactic editor, with players that can be drag and dropped to the court.
You can also move the players inside the court by dragging them wherever you want
React elements shouldn't be stored in a state. The state represents the data, and a React element represents the rendered view, that isn't properly typed. It also not play well when passing the initial data to the component, and when exposing the state to the consumers. I would highly recommend separating them by mapping the array each time.
export interface RackInput {
id: string,
objects: [ReactElement[], Dispatch<SetStateAction<ReactElement[]>>],
The state is internal to the current component, and shouldn't be exposed as it is to its children. Just pass the objects in two separate props: one for the array, and one for a on change handler. The parent component will then have the liberty to handle object changes as it wants.
export interface RackInput {
id: string,
objects: [ReactElement[], Dispatch<SetStateAction<ReactElement[]>>],
canDetach: (ref: RefObject<HTMLDivElement>) => boolean,
RefObject
is not appropriated because it is a nullable internal to the component. Expose directly the HTML element.useEffect(() => {
const bounds = divRef.current!.getBoundingClientRect();
setCourtPlayers(players.map(player => {
BasketCourt
shouldn't have a state if it is always derived from its props.import React, {useRef} from "react";
React doesn't need to be in scope.
import Draggable, {DraggableBounds} from "react-draggable";
import {PlayerPiece} from "./PlayerPiece";
export interface PlayerOptions {
React calls them properties.
handle={".player-piece"}
nodeRef={ref}
bounds={bounds}
defaultPosition={{x: x, y: y}}
/**
* Percentage of the player's position to the bottom (0 means top, 1 means bottom, 0.5 means middle)
*/
bottom_percentage: number
Ratio is more appropriated for a value between 0 and 1.
const positions = ["1", "2", "3", "4", "5"]
const [allies, setAllies] = useState(
positions.map(pos => <PlayerPiece team="allies" key={pos} text={pos}/>)
The state shouldn't contain React elements.
0e23e8706b
toef80aa3192
1 year agoa6cceb31ea
to170dd58a99
1 year ago170dd58a99
tof58c85ac0a
1 year agob213729e50
toc53a1b024c
1 year agoGreat!
}
export function BasketCourt({players, onPlayerRemove}: BasketCourtProps) {
const divRef = useRef<HTMLDivElement>(null);
Unused reference
* */
export default function CourtPlayer({player, onRemove}: PlayerProps) {
const ref = useRef<HTMLDivElement>(null);
Unused reference
import React, {CSSProperties, useState} from "react";
import {CSSProperties, ReactElement, RefObject, useRef, useState} from "react";
Unused imports
8483ef08dd
into master 1 year agoReviewers
8483ef08dd
.