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

feat: Add a parameter to SelectColumn to keep the row selected on repeat clicks in Single mode. #3138

Open
hml-coder opened this issue Jan 8, 2025 · 11 comments
Labels
community:contribution Issue will/can be addressed by community contribution status:in-progress Work is in progress

Comments

@hml-coder
Copy link

🙋 Feature Request

Please consider adding a parameter to the SelectColumn component to maintain the selection of a row when SelectMode is set to Single and the same row is selected repeatedly. The default value can respect the current selection behavior.

😯 Current Behavior

When the the SelectMode is Single and the selected row is clicked, the row is deselected

💁 Possible Solution

// <summary>
/// Controls whether an already selected item in Single selection mode can be deselected by clicking it again.
/// Default is <c>true</c>, allowing deselection. Set to <c>false</c> to keep the current selection unchanged on repeated clicks.
/// </summary>
[Parameter]
public bool AllowSingleModeDeselect { get; set; } = true;
private async Task AddOrRemoveSelectedItemAsync(TGridItem? item)
{
    if (item != null && (Selectable == null || Selectable.Invoke(item)))
    {

        // Check if SelectMode is Single, the item is already selected, and AllowSingleModeDeselect is false
        if (SelectMode == DataGridSelectMode.Single && _selectedItems.Count == 1 && _selectedItems.Contains(item) && !AllowSingleModeDeselect)
        {
            return; // Do nothing if the same item is clicked
        }

        if (SelectedItems.Contains(item))
        {
            _selectedItems.Remove(item);
            SelectAll = false;
            await CallOnSelectAsync(item, false);
        }
        else
        {
            if (SelectMode == DataGridSelectMode.Single)
            {
                foreach (var previous in _selectedItems)
                {
                    await CallOnSelectAsync(previous, false);
                }
                _selectedItems.Clear();
            }

            _selectedItems.Add(item);
            await CallOnSelectAsync(item, true);
        }

        if (SelectedItemsChanged.HasDelegate)
        {
            await SelectedItemsChanged.InvokeAsync(SelectedItems);
        }

        RefreshHeaderContent();
    }

    Task CallOnSelectAsync(TGridItem item, bool isSelected)
    {
        return OnSelect.HasDelegate
            ? OnSelect.InvokeAsync((item, isSelected))
            : Task.CompletedTask;
    }
}

💻 Examples

<SelectColumn TGridItem="TGridItemDto"
              SelectMode="@DataGridSelectMode.Single"
              SelectFromEntireRow="true"
              AllowSingleModeDeselect="false"
/>
@microsoft-github-policy-service microsoft-github-policy-service bot added the triage New issue. Needs to be looked at label Jan 8, 2025
@hml-coder
Copy link
Author

Or, instead of the additional parameter, add another selection mode such as SelectMode.SingleSticky if that approach is cleaner/easier.

@dvoituron
Copy link
Collaborator

indeed, I prefer the SingleSticky mode :-)

@vnbaaij
Copy link
Collaborator

vnbaaij commented Jan 9, 2025

Yes, me too!

@hml-coder as you have almost all the code fot it in place already anyway, fancy doing a PR for this?

@hml-coder
Copy link
Author

For sure .. although it will be my first PR. I'll review it more closely in the next couple of days.

@vnbaaij
Copy link
Collaborator

vnbaaij commented Jan 9, 2025

We'll help you through it!

@vnbaaij vnbaaij added status:in-progress Work is in progress community:contribution Issue will/can be addressed by community contribution and removed triage New issue. Needs to be looked at labels Jan 9, 2025
@hml-coder
Copy link
Author

Thanks. I read the contribution instructions but I think there's more to it such as creating a fork etc. Do you have step by step info?

@hml-coder
Copy link
Author

Code changes work fine with my local version. Copied all Single tests into SingleSticky tests and they run successfully.

Image

@vnbaaij
Copy link
Collaborator

vnbaaij commented Jan 10, 2025

Thanks. I read the contribution instructions but I think there's more to it such as creating a fork etc. Do you have step by step info?

Yes, create a fork first and then create a branch in your fork with the changes. Once you push to your branch, you'll be asked to create a PR

@hml-coder
Copy link
Author

So I fork master since the contribution instructions state:

Before submitting a pull request, be sure to rebase your branch from master.

@vnbaaij
Copy link
Collaborator

vnbaaij commented Jan 10, 2025

That should actually be dev as that is the branch we work from

@hml-coder
Copy link
Author

Thanks. That's great because I forked dev and thought I might need to redo the changes :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community:contribution Issue will/can be addressed by community contribution status:in-progress Work is in progress
Projects
None yet
Development

No branches or pull requests

3 participants