import React, {ChangeEvent, FocusEvent, ReactNode, KeyboardEvent} from "react"; import {ScoreCellValue} from "../Classes/ScoreCell"; import {CellEventResponse} from "./KadiBoard"; import {CellLocation} from "../Classes/PlayerScoreCard"; import {useLongPress} from "./useLongPress"; import {CellFlag, FieldType} from "../../../shared/rulesets"; export type KadiCellDisplayValue = ScoreCellValue | CellFlag.strike; export interface KadiCellProps { location: CellLocation; fieldType: FieldType; playerId: string; value: KadiCellDisplayValue; showResults?: boolean; onCellEdit: (response: CellEventResponse) => void; } interface KadiCellState {} class KadiCell extends React.Component { private readonly standardTimeoutTimeMs: number; constructor(props: KadiCellProps) { super(props); this.standardTimeoutTimeMs = 400; } shouldComponentUpdate( nextProps: Readonly, nextState: Readonly, nextContext: any): boolean { return nextProps.value != this.props.value; } updateCell = (value: ScoreCellValue): void => { const response: CellEventResponse = { value: value, playerId: this.props.playerId, location: this.props.location, }; this.props.onCellEdit(response); }; strikeCell = (): void => { const response: CellEventResponse = { value: CellFlag.strike, playerId: this.props.playerId, location: this.props.location, }; this.props.onCellEdit(response); }; unstrikeCell = (): void => { const response: CellEventResponse = { value: CellFlag.unstrike, playerId: this.props.playerId, location: this.props.location, }; this.props.onCellEdit(response); }; render(): ReactNode { const { fieldType, value, } = this.props; const propsForEditableCell = { timeoutMs: this.standardTimeoutTimeMs, updateCell: this.updateCell, strikeCell: this.strikeCell, value: value as ScoreCellValue, }; if (value === CellFlag.strike) { return ; } else { switch (fieldType) { case FieldType.bonus: case FieldType.subtotal: case FieldType.total: case FieldType.globalTotal: return ( ); case FieldType.bool: return ; case FieldType.multiplier: return ; case FieldType.number: return ; case FieldType.superkadi: return ; } } } } interface StandardKadiCellProps { value: ScoreCellValue, } interface StrikeKadiCellProps { unstrikeCell: () => void, } interface ResultsKadiCellProps extends StandardKadiCellProps { } interface UpdateableKadiCellProps extends StandardKadiCellProps { updateCell: (updateVal: ScoreCellValue) => void, } interface LongPressStrikeKadiCellProps extends StandardKadiCellProps { timeoutMs: number, strikeCell: () => void, } interface GenericResultsKadiCellProps extends ResultsKadiCellProps { classNameString: string; } type EditableKadiCellProps = UpdateableKadiCellProps & LongPressStrikeKadiCellProps; const NumberKadiCell: React.FunctionComponent = ({ strikeCell, updateCell, value , timeoutMs}) => { const [beingEdited, setBeingEdited] = React.useState(false); const [currentEditValue, setCurrentEditValue] = React.useState(""); const strikeCellOnLongPress = useLongPress(strikeCell, timeoutMs); const displayText: string = beingEdited ? currentEditValue : value.toString(); const handleFocus = (e: FocusEvent) => { setBeingEdited(true); }; const handleChange = (e: ChangeEvent) => { setCurrentEditValue(e.target.value); if (e.target.value == "") { strikeCell(); endInput(); } }; const handleBlur = (e: FocusEvent) => { submitInput(e.target.value); }; const handleKeyUp = (e: KeyboardEvent) => { if (e.key === "Enter") { submitInput(e.currentTarget.value); } }; const submitInput = (input: string) => { updateCell(Number(input)); endInput(); }; const endInput = () => { setBeingEdited(false); setCurrentEditValue(""); }; return (
) }; const SuperkadiKadiCell: React.FunctionComponent = ({value, timeoutMs, strikeCell, updateCell}) => { const handleClick = (): void => updateCell(true); const strikeCellOnLongPress = useLongPress(strikeCell, timeoutMs); return (
{value}
) }; const BoolKadiCell: React.FunctionComponent = ({value, timeoutMs, strikeCell, updateCell}) => { const handleClick = (): void => updateCell(true); const strikeCellOnLongPress = useLongPress(strikeCell, timeoutMs); return (
) }; const MultipleKadiCell: React.FunctionComponent = ({value, timeoutMs, strikeCell, updateCell}) => { const handleClick = (): void => updateCell(true); const strikeCellOnLongPress = useLongPress(strikeCell, timeoutMs); return (
{value}
) }; const GenericResultsKadiCell: React.FunctionComponent = ({value, classNameString}) => { return (
{value}
) }; const StrikeKadiCell: React.FunctionComponent = ({unstrikeCell}) => { const updateCell = () => unstrikeCell(); return ( ); }; export default KadiCell;