Code Monkey home page Code Monkey logo

lwc-enhanced-refs's Introduction

Enhanced lwc:ref

This custom implementation allows a user to use the notation lwc:ref for static elements or data-ref to reference a component in a template. This is a custom implementation of the lwc:ref directive that is not supported by the LWC framework.

Usage

To use the EnhancedRefs mixin, import it from the c/enhancedRefs module and extend it in your component class. The mixin will add a property enhancedRefs to your component that will contain all the references to the elements with the lwc:ref or data-ref attribute.

Example

<!--enhancedRefsTest.html-->
<template>
    <lightning-button label="Test refs" onclick={onClick}></lightning-button>

    <lightning-input label="Static ref" lwc:ref="staticRef" value="Static ref"></lightning-input>

    <template for:each={inputs} for:item="input">
        <lightning-input key={input.key} label={input.label} value={input.value} data-ref={input.key}></lightning-input>
    </template>
</template>

Mixin

// enhancedRefsTest.js
import { LightningElement } from 'lwc';
import { EnhancedRefs } from 'c/enhancedRefs';
export default class EnhancedRefsTest extends EnhancedRefs(LightningElement) {

    inputs = Array.from({ length: 5 }, (_, i) => ({ key: `input${i}`, value: `value ${i}`, label: `Label ${i}` }));

    onClick() {
        console.log(this.enhancedRefs.staticRef.value);
        console.log(JSON.stringify(this.inputs.map(({ key }) => this.enhancedRefs[key].value)))
    }
}

Module

// enhancedRefsTest.js
import { LightningElement } from 'lwc';
import { enhancedRefs } from 'c/enhancedRefs';
export default class EnhancedRefsTest extends LightningElement {

    enhancedRefs = enhancedRefs(this);

    inputs = Array.from({ length: 5 }, (_, i) => ({ key: `input${i}`, value: `value ${i}`, label: `Label ${i}` }));

    onClick() {
        console.log(this.enhancedRefs.staticRef.value);
        console.log(JSON.stringify(this.inputs.map(({ key }) => this.enhancedRefs[key].value)))
    }
}

Installation

  1. Clone the repository
  2. Deploy the c/enhancedRefs module to your org

Deploy the module using the Salesforce CLI

sf project deploy start -x manifest/package.xml
  1. Use the EnhancedRefs mixin or the enhancedRefs function in your component

lwc-enhanced-refs's People

Contributors

hacerx avatar

Watchers

 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.