GithubHelp home page GithubHelp logo

signal-from-input's Introduction

signal-from-input

Small library that provides a typesafe extension to use angular component input fields as Signals

Installation

npm i signal-from-input

Choose the version corresponding to your Angular version:

Angular @signal-from-input
16+ 16.x+

Usage

Suppose you have the following component:

@Component({
    selector: 'app-component',
    ...
})
export class AppComponent {
    @Input() showData: boolean;
    _data: Signal<Data>;

    constructor(private store: Store) {
        this._data = selectSignal(this.store.pipe(
            select(selectData)));
    }
}

In the constructor you want to connect the input to a signal. To do this you can now use the function fromInput. The signature of the function looks like this:

fromInput: <T>(target: T) =>
    <K extends keyof T>(name: K) =>
        Signal<T[K] | undefined>;

The input can now be turned into a signal from the input field using the following call.

fromInput<AppComponent>(this)('showData');

Here the complete example:

import { fromInput } from 'signal-from-input';

@Component({
    selector: 'app-component',
    template: `
        <div *ngIf="showData">Input</div>
        <div *ngIf="_showData()">Input as signal</div>
    `,
})
export class AppComponent {
    @Input() showData: boolean;
    _showData: Signal<boolean>;
    _data: Signal<Data>;

    constructor(private store: Store) {
        this._showData = fromInput<AppComponent>(this)('showData');
        this._data = this.store.pipe(select(selectData));

        // now you can compute signals as you like
        this._computed = computed(() => this._showData ? this._data : ... );
    }
}

signal-from-input's People

Watchers

Roger Rudin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.