reactgrid icon indicating copy to clipboard operation
reactgrid copied to clipboard

Ref doesn't focus / How to switch focus between grids?

Open hunterwilhelm opened this issue 2 years ago • 1 comments

I want to create a hotkey that switches focus between grids. But I can't get the focus to work programmatically using the ref. There is no focus function on it. I have tried using the parent div for focusing but that doesn't work either.

Here is an example of it not working. It works with the input field but not the react grid components.

https://codesandbox.io/s/reactgrid-ref-not-working-p3rz8p?file=/src/App.tsx

hunterwilhelm avatar Sep 07 '22 00:09 hunterwilhelm

This feature will be supported by reactgrid v5 :) Want to give your input and find out more? Join our RG Discord: https://discord.gg/XMHJrDGp

MichaelMatejko avatar Sep 20 '22 12:09 MichaelMatejko

I found a solution! The element that we need to focus is not the one the ref is attached to, rather it is the child with className rg-hidden-element

I made a function that you can pass the ref of the DataGrid element to and will focus the right one.

import { RefObject } from "react";

/** This function is used for focusing the current data grid */
export function focusDataGridWithRef(ref: RefObject<HTMLDivElement>) {
  const input = ref.current?.querySelector('.rg-hidden-element') as any;
  if (input == null) return;
  if (input.focus instanceof Function) {
    input.focus();
  }
}

edit: If anyone wants a full example, just tag me in a reply in this thread and I'll make a codesandbox for you

hunterwilhelm avatar Oct 24 '22 17:10 hunterwilhelm

@hunterwilhelm Hi, can you help me with a sandbox code for a function that just remove the focus from the grid(like how the grid is in the initial state before clicking any cell)

robertgutu avatar Jan 09 '23 09:01 robertgutu

@robertgutu I am not sure this issue is talking about the task you mentioned. But basically you want to clear grid so there is no "highlighted cell", correct?

Take a look at the focus management API here: https://reactgrid.com/docs/4.0/2-implementing-core-features/7-managing-focus/

Does that help?

hunterwilhelm avatar Jan 09 '23 17:01 hunterwilhelm

@hunterwilhelm I already looked at it and it did not help. I couldn't find what value do I need to set the focus in order to "remove the focus"

robertgutu avatar Jan 10 '23 06:01 robertgutu