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

Implementing Checkbox Toggle with Scale Components in React Data Grid #2152

Open
henrisco82 opened this issue Sep 29, 2023 · 2 comments
Open
Labels
support Asking for help

Comments

@henrisco82
Copy link

henrisco82 commented Sep 29, 2023

Scale Version
"@telekom/scale-components": "^3.0.0-beta.132",
"@telekom/scale-components-react": "^3.0.0-beta.132"

Framework and version
React
"react": "^17.0.2"

Screenshot 2023-09-29 at 10 10 59 AM

Hello there,

I am trying to use a checkbox (switch) on data grid component in react that displays a list of users. I would like to be able to toggle the switch to activate or deactivate a user by calling a function. In other words, I will like to be able to respond to event when the switch(checkbox) is changed. I have tried using event handlers like onClick, onChange, onScaleChange but the function is not triggered. Please is there a way I can achieve this with Scale?

I have tried implementing it the following way

const rows = activatedUsers.map((user) => ([
    user.email,
    user.roles,
      [{
          label: 'Activate',
          type: 'checkbox',
          onChange: () => {
            console.log('User Activation function here');
          },
        }]
  ]));

  const cols = [
    {
      label: 'Email',
    },
    {
      label: 'Role',
    },
    {
      type: 'checkbox',
      label: 'Activate',
      style: 'switch',
      editable: true,
      sortable: true,
    },
  ];

I have also noticed that the heading is the same as label of the switch. Is it possible to decouple them from each other?

Thanks a lot.

@henrisco82 henrisco82 added the support Asking for help label Sep 29, 2023
@felix-ico
Copy link
Collaborator

Hi @henrisco82, sorry for the late reply.

I'm not sure the checkbox field should be set like that, looking at the docs https://telekom.github.io/scale/?path=/docs/components-data-grid--checkbox-cell the checkbox field is just a boolean (true for active), and not a complex object like in your code snippet. So your rows should be defined as follows:

const rows = activatedUsers.map((user) => ([
    user.email,
    user.roles,
    // if user object has an active property you could do:
    // user.active ? true : false
    // judging by the mapped array name 'activeUsers' i guess they can only be deactivated
    true

The scale-change event listener should be set on the ScaleDataGrid (or on window/document), so it would look something like this:

<ScaleDataGrid
    ...
    onScaleChange={() => {console.log('changed', rows}}
>

and this should log the rows with the updated values (when switched off they will be false).

I based my answer on #811 a little, where a somewhat similar issue was described, perhaps that can help you too.

Regarding the switch label, this seems not possible with the current implementation, and adding this would be a feature request (btw scale users are always welcome to open pull requests for such improvements 🤓).

@tshimber
Copy link
Contributor

tshimber commented Nov 8, 2024

hi @henrisco82 ! was advice above helpful? if so, could you please close the issue and open feature request if needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support Asking for help
Projects
None yet
Development

No branches or pull requests

3 participants