Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A new callback function added on Crossword Provider #570

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 25 additions & 21 deletions example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
],
"repository": {
"type": "git",
"url": "https://github.com/JaredReisinger/react-crossword"
"url": "https://github.com/JaredReisinger/react-crossword.git"
},
"license": "MIT",
"engines": {
Expand Down
65 changes: 62 additions & 3 deletions src/CrosswordProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,15 @@ export const crosswordProviderPropTypes = {
*/
onCellChange: PropTypes.func,

/**
* callback function called when a cell is selected(e.g. when the user selects a
* cell); called with `(direction, number, row, cols)` arguments, where the `direction`
* is the current direction, `number` is the index of the clue and `row` and `col` are
* the 0-based position of the cell
*/

onCellSelected: PropTypes.func,

/**
* callback function called when a clue is selected
*/
Expand Down Expand Up @@ -265,6 +274,19 @@ export type CrosswordProviderProps = EnhancedProps<
*/
onCellChange?: (row: number, col: number, char: string) => void;

/**
* callback function called when a cell is selected(e.g. when the user selects a
* cell); called with `(direction, number, row, cols)` arguments, where the `direction`
* is the current direction, `number` is the index of the clue and `row` and `col` are
* the 0-based position of the cell
*/
onCellSelected?: (
direction: Direction,
number: string | undefined,
row: number,
col: number
) => void;

/**
* callback function called when a clue is selected
*/
Expand Down Expand Up @@ -339,6 +361,7 @@ const CrosswordProvider = React.forwardRef<
onCrosswordComplete,
onCrosswordCorrect,
onCellChange,
onCellSelected,
onClueSelected,
useStorage,
storageKey,
Expand Down Expand Up @@ -626,9 +649,13 @@ const CrosswordProvider = React.forwardRef<
setCurrentDirection(direction);
setCurrentNumber(candidate[direction] ?? '');

if (onCellSelected) {
onCellSelected(direction, candidate[direction] ?? '', row, col);
}

return candidate;
},
[currentDirection, getCellData]
[currentDirection, getCellData, onCellSelected]
);

const moveRelative = useCallback(
Expand Down Expand Up @@ -714,6 +741,15 @@ const CrosswordProvider = React.forwardRef<
setCurrentDirection(other);
setCurrentNumber(cellData[other] ?? '');
}

if (onCellSelected) {
onCellSelected(
other,
cellData[other] ?? '',
focusedRow,
focusedCol
);
}
break;
}

Expand Down Expand Up @@ -778,6 +814,7 @@ const CrosswordProvider = React.forwardRef<
data,
currentNumber,
moveTo,
onCellSelected,
]
);

Expand Down Expand Up @@ -886,11 +923,15 @@ const CrosswordProvider = React.forwardRef<
}

setCurrentNumber(cellData[direction] ?? '');

if (onCellSelected) {
onCellSelected(direction, cellData[direction] ?? '', row, col);
}
}

focus();
},
[currentDirection, focus, focused, focusedCol, focusedRow]
[currentDirection, focus, focused, focusedCol, focusedRow, onCellSelected]
);

const handleInputClick = useCallback<
Expand All @@ -915,8 +956,25 @@ const CrosswordProvider = React.forwardRef<

setCurrentNumber(cellData[direction] ?? '');
focus();

if (onCellSelected) {
onCellSelected(
direction,
cellData[direction] ?? '',
focusedCol,
focusedRow
);
}
},
[currentDirection, focus, focused, focusedCol, focusedRow, getCellData]
[
currentDirection,
focus,
focused,
focusedCol,
focusedRow,
getCellData,
onCellSelected,
]
);

const handleClueSelected = useCallback(
Expand Down Expand Up @@ -1132,6 +1190,7 @@ CrosswordProvider.defaultProps = {
onCrosswordComplete: undefined,
onCrosswordCorrect: undefined,
onCellChange: undefined,
onCellSelected: undefined,
onClueSelected: undefined,
children: undefined,
};
1 change: 1 addition & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface CrosswordContextType {
handleInputClick: React.MouseEventHandler<HTMLInputElement>;
/** A handler for clue selection. */
handleClueSelected: (direction: Direction, number: string) => void;

/** Provides registration for focus actions */
registerFocusHandler: (focusHandler: FocusHandler | null) => void;

Expand Down