Skip to content

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

@hml-coder

Description

@hml-coder

🙋 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"
/>

Metadata

Metadata

Assignees

No one assigned

    Labels

    closed:doneWork is finishedcommunity:contributionIssue will/can be addressed by community contribution

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions