components icon indicating copy to clipboard operation
components copied to clipboard

[Feature] Selection CDK Directive

Open amcdnl opened this issue 8 years ago • 8 comments

Selection Directive

A directive that abstracts selection of elements like checkbox, buttons, etc into a higher order directive. This directive could abstract the item list selection component and be used in components like the tree and grid.

Example demo

Features

  • Selection strategies such as single, multiple, multiple ctrl click
  • Handles ctrl / shift click selection
  • Select all ability
  • Work on checkboxes, buttons, etc
  • Strategies like prevent clear selection

API

It would have 3 different directives:

Selection

Parent controller directive.

Inputs
  • Datasource: Datasource of all models to be selected
  • Selections: All currently selected elements
  • Track By: Track by function to identify items
  • Strategy: Single, Multi, Multi Click
  • Clearable: Whether the selection can be cleared or not
  • Disabled: Disable selections
Outputs
  • Selection Change: A event that would output: the item that was just clicked and the list of all selections

Checkbox/Item

MatCheckbox and button/div/etc bindings.

Inputs
  • Model: The model of the item to be selected. Should handle single or array of items.
  • Disabled: Whether the item selection is disabled

Example

<div [matSelections]="datasource" (selectionsChange)="onChange($event)">
   <mat-checkbox [matSelection]="datasource">Select All</mat-checkbox>
   <mat-checkbox [matSelection]="model">Check me</mat-checkbox>
</div>

Considerations

  • Select all ability for items not yet loaded

amcdnl avatar May 13 '18 18:05 amcdnl

Replace selectionsChange to selectionChange

anilarya avatar May 14 '18 03:05 anilarya

Proposed API

Markup

The component will act similar to how the MatSort component works. The cdkSelectionToggle will inject the cdkSelection component and subscribe to changes and update appropriately. When a cdkSelectionToggle selection occurs it will invoke select(this) on the cdkSelection parent and handle the selection logic there.

One complication to that approach is the select between operation. We need a way to reliably get the models for a given start + end. With the registration approach like the mat sort does you can't calculate indexes reliably. On second though, we might need to use ContentChildren for this.

<some-selection-control cdkSelection=”selections” (selectionChange)=”onSelectionChange($event)”>
  <some-option cdkSelectionToggle=”model” role=”option” tabindex=”-1” aria-selected=”true/false">
    Whatever
  </some-option>
</some-selection-control>

The mat-selection-list will inherently use this under the hood.

<mat-selection-list> <!-- Acts like cdkSelectionState -->
  <mat-option>Whatever</mat-option> <!-- Acts like a cdkSelectionToggle -->
</>

CdkSelection Interface

class CdkSelection<T> {
  /** List of all active selections. */
  selections: SelectionModel<T>[];

  /**
   * Different selection strategies to apply.
   * Single: Only one item can be selected at a time.
   * Multiple: Multiple items can be selected.
   * Modifier Multiple: Multiple items can be selected using modifier keys such as ctrl or shift */
  strategy: ‘single’ | ‘multiple’ | ‘modifierMultiple’;

  /** Track by used for selection matching. */
  trackBy: (index, model) => string;

  /** Whether the selections can be cleared after selection. */
  clearable: boolean;

  /** The max number of selections that can be selected */
  maxSelections: number;

  /** Whether the control is disabled or not. */
  disabled: boolean;

  /** Event fired when a selection/deselection occurs. */
  selectionChange: EventEmitter<CdkSelectionChange>;
}

CdkSelectionChange Interface

interface CdkSelectionChange<T> {
   /** The item that was actually clicked/tapped/etc. In modifier scenario this would be the item that was last clicked.
   selection: T;

  /** All of the selections. */
  selections: T[];

  /** The action that was taken on the selection items */
  type: ‘selected’ | ‘deselected’;
}

CdkSelectionOption Interface

interface CdkSelectionOption<T> {
  /** The model to be bound to the selection. Multiples can be passed for scenarios like select all. */
  model: T | T[];

  /** Whether the selection is disabled or not. */
  disabled: boolean;
}

amcdnl avatar Jun 10 '18 16:06 amcdnl

A few API changes from above but here is a first cut demo - https://stackblitz.com/edit/cdk-selection

amcdnl avatar Jun 11 '18 21:06 amcdnl

cc @crisbeto you might be interested in this

jelbourn avatar Jun 11 '18 21:06 jelbourn

I think we discussed this one a while ago on Slack. I'm not sure how much of the boilerplate we'll be able to reduce, compared to just using SelectionModel directly, by doing this since we still need to proxy a bunch of calls to the underlying model.

crisbeto avatar Jun 12 '18 17:06 crisbeto

@crisbeto - Think of this as directive bindings for the selection model class.

amcdnl avatar Jun 13 '18 16:06 amcdnl

Really interesting feature, expecially given the fact that MatCheckbox change event does not expose the original click event which holds information about ctrl, shift etc being pressed or not. Any news about this?

IlCallo avatar Sep 21 '18 15:09 IlCallo

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.