1

I am building an autocomplete component. The plan is that I can slot in some markup for what I know the component is going to bind to.

The idea is this could be any object rather than a simple display value and identifier.

I have this working using templates but I am wondering if there is a better approach.

So far it looks like this (options is hard coded for now within the components model):

// Usage:
<autocomplete>
  <template replace-part="item">
    //this is the content for each option within the component
    <b>${option.lastName}<b/>, ${option.firstName}  
  </template>
</autocomplete>

//autocomplete 
<template>
  <input type="text" placeholder="Type 3 characters ...">
  <ul>
    <li repeat.for="option of options">
      <template replaceable part="item"></template>
    </li>
  </ul>
</template>

I don't really like the templating boilerplate, slots are much nicer, is there any way to make slots work like this?

<autocomplete>
    <li repeat.for="option of options">
        ${option.lastName}<b/>, ${option.firstName}
    <li/>
</autocomplete>

//autocomplete 
<template>
  <input type="text" placeholder="Type 3 characters ...">
  <ul>
    <slot></slot>
  </ul>
</template>
4imble
  • 13,979
  • 15
  • 70
  • 125

1 Answers1

1

Slot in Aurelia is the emulation based on standard spec, which mean it doesn't work with repeat situation. repaceable was introduced to handle this scenario and I don't think we have any other options. Sometimes it feels weird but with a little documentation, probably you and your team will be fine. What you can do is for each replacement, what properties it can look for to get the item.

bigopon
  • 1,924
  • 2
  • 14
  • 23