question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Consider to always provide a template with exposed props

See original GitHub issue

Right now the current process to customise a component is a bit cumbersome. We can leverage some Angular feature to have the same kind of API that we have with Vue InstantSearch (with slots). Here is a trivial example with a Toggle component (see below). With this API we don’t have to manually create a component with the connector, etc… We can directly override the output from the template, it’s very similar to the slots in Vue and render props in React.

Disclaimer: not sure that it’s the “standard” way to let the user have the full control on the render.


Implementation:

import { Component, ContentChild, TemplateRef } from '@angular/core';

@Component({
  selector: 'app-toggle',
  template: `
    <ng-container
      *ngTemplateOutlet="
        template ? template : defaultToggleTemplate;
        context: { on: state, toggle: toggle }
      "
    ></ng-container>

    <ng-template #defaultToggleTemplate let-state="on" let-toggle="toggle">
      <div>inner = '{{ state }}'</div>
      <button (click)="toggle()">Toggle</button>
    </ng-template>
  `,
})
export class ToggleComponent {
  @ContentChild(TemplateRef) public template?: TemplateRef<any>;

  state = false;

  toggle = () => {
    this.state = !this.state;
  };
}

Usage:

<app-toggle></app-toggle>

<app-toggle>
  <ng-template let-state="on" let-toggle="toggle">
    <p>This is a custom render of the component.</p>
    <label>
      <input type="checkbox" (change)="toggle()" hidden />
      <span>{{ state ? '✅' : '❌' }}</span>
      <span>The input is {{ state ? 'checked' : 'unchecked' }}</span>
    </label>
  </ng-template>
</app-toggle>

Here is the live example on CodeSandbox.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
samousscommented, Nov 19, 2018

Yep indeed for use case where we don’t have a widget implemented but the connector exist (e.g. connectAutocomplete). But even with those cases we can reduce the boilerplate code to extend this class. There are some abstractions that leak a bit: like the BEM root element or even the fact that the user have to manually call the connector. We can even think of another solution than the BaseWidget class, not sure it’s the best “Angular” way.

1reaction
tkruggcommented, Nov 19, 2018

It totally makes sense. So would this replace entirely the need for BaseWidget?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Leveraging Render Props in Vue - Medium
Code sharing between your Vue components is an important concept to consider when designing and developing your components. Render props ...
Read more >
How to update a component's prop in ReactJS — oh yes, it's ...
And we will soon know how. Consider the following line of code from a parent component: <MyChild childName={this.state.parentName} />. This is ...
Read more >
Customize a React InstantSearch widget - Algolia
One common exposed prop that you can use is the defaultRefinement . Use it to provide the default refinement when the connected component...
Read more >
Vue best practice for calling a method in a child component
One easy way is to do this: <!-- parent.vue --> <template> <button @click="$refs.myChild.sayHello()">Click me</button> <child-component ...
Read more >
<script setup> | Vue.js
It provides a number of advantages over the normal <script> syntax: ... function in template expressions without having to expose it via the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found