PolymerElements / iron-selector

Manages a list of elements that can be selected
32 stars 55 forks source link

Add a default-selected property as selection fallback #116

Closed TimvdLippe closed 8 years ago

TimvdLippe commented 8 years ago

Description

With the recent release of carbon-route, a current issue is a missing default routing fallback when for example a 404 is hit. The issue with the request of this feature: https://github.com/PolymerElements/carbon-route/issues/68

During the discussion, we discovered two possible solutions: creating a meta-element to track all route elements or a default fallback for selectors based on the route. After some thinking and prototyping, @rictic and me think that the simplest and most elegant solution is to allow a default/fallback for the selectors.

Proposal

We propose a new property defaultSelected in IronSelectableBehavior. This property is a fallback when _valueToItem results in undefined and this.attrForSelected is set. In that case, there was no match for the specific route.

Example use-case

A full use-case can be found here: https://gist.github.com/TimvdLippe/9bfebc20aec75dd36126b8162c310dff

The usage of iron-pages:

<iron-pages attr-for-selected="data-route" selected="{{data.page}}" default-selected="404">
  <user-page data-route="users" user-info="{{subData}}"></user-page>
  <store-page data-route="store" store-info="{{subData}}"></store-page>
  <404-page data-route="404"></404-page>
</iron-pages>

Compatibility

The implementation of this feature should be fairly easy and backwards-compatible.

One possible implementation could be:

if (value == null) {
  return null;
}
var index = this._valueToIndex(value)
  || (this.defaultSelected && this._valueToIndex(this.defaultSelected));
return this.items[index];
bicknellr commented 8 years ago

This sounds like a good idea; want to send a PR?

leodido commented 8 years ago

+1

TimvdLippe commented 8 years ago

@bicknellr I submitted a PR in #117

TimvdLippe commented 8 years ago

For the record: the property was renamed to fallbackSelection and now available in 1.4.0