Lädt...

🔧 Integrating sg-autocomplete Component


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Image description

What is sg-autocomplete Component?

sg-autocomplete component is a user interface element that functions like a text input field, but as the user types, it dynamically suggests relevant options from a predefined json list, allowing them to quickly select a matching value instead of typing the full term manually.

You can configure with any elements from the json file.

You can navigate the autocomplete dropdown using mouse or tab key from keyboard.

Getting Started

Use the below command to add your package in your application

npm i sg-autocomplete

you can consume it in your application as shown below:

<sg-autocomplete autotext="JSON-ELEMENT-NAME"></sg-autocomplete>

Say for Example if your JSON element is like this

{
    url: "https://cdn-icons-png.flaticon.com/512/1804/1804486.png", 
    title: "URL", 
    description:"URL"
  },
  {
    url: "https://i.ibb.co/7XqwsLw/fox.jpg",
    title: "Fox", 
    description:"Fox"
 },

If you want to add "description" as the autocomplete suggestion list, you can do so like this

sg-autocomplete autotext="description"></sg-autocomplete>

Options

Image description

Examples

Image description

Usage

Now we will see how to integrate this libiary in your applications.

a) Vanilla JavaScript

You can include the component in your HTML file in two ways:

1. Using a CDN:

Add a tag in the of your HTML file, pointing to the component's JavaScript file hosted on a CDN.

For example:

<script src="https://cdn.jsdelivr.net/npm/sg-autocomplete/dist/sg-autocomplete/sg-autocomplete.js"></script>

2) Using ES Modules:
If you prefer to use ES Modules, you can import the component using an import statement:

 <script type="module">
   import { defineCustomElements } from './dist/sg- 
   autocomplete/loader/index.es2017.mjs';
   defineCustomElements();
 </script>

Make sure to adjust the paths according to your project structure.

Use the component in your HTML:

You can now use the component in your HTML like any other HTML element:
Code

 <sg-autocomplete autotext="description" dropdown=true >
</sg-autocomplete>

Interact with the component using JavaScript:

Example of Setting the JSON data:

 const autocompleteLst = [
        {url: "https://cdn-icons-png.flaticon.com/512/1804/1804486.png", title: "URL", description:"URL"},
        {url: "https://i.ibb.co/dBCHzXQ/paris.jpg", title: "Paris Eiffel", description:"Paris Eiffel"},
        {url: "https://i.ibb.co/JKB0KPk/pizza.jpg", title: "Pizza Time", description:"Pizza Time"},
        {url: "https://i.ibb.co/VYYPZGk/salmon.jpg", title: "Salmon ", description:"Salmon"},
      ];
      document.querySelector('sg-autocomplete').suggestionlist = autocompleteLst

You can access and manipulate the component using standard DOM APIs:

 const input = document.querySelector('#sg-autocomplete');

Handle events:

  input.addEventListener('click', function(event) {
  // Code to execute when the button is clicked
     console.log('Input Value:'+ event.target.value);
 });

  input.addEventListener("keydown", (e) => {
     console.log(e.key+'Input Value keydown:'+ e.target.value);
  }); 

b) Angular

Step 1: Add an import to main.ts

import { defineCustomElements} from '../node_modules/sg-autocomplete/loader';

And somewhere near the bottom we'll call this function.

defineCustomElements();

Step 2: Next, in app.module.ts add the CUSTOM_ELEMENTS_SCHEMA.

import {CUSTOM_ELEMENTS_SCHEMA} from `@angular/core`;

and then

schemas: [
    CUSTOM_ELEMENTS_SCHEMA
]

Your app.module.ts should look like this:

import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';

@NgModule({
  declarations: [],
  imports: [],
  schemas: [ CUSTOM_ELEMENTS_SCHEMA],
  providers: [],
  bootstrap: [AppComponent]
})

export class AppModule { }

Please Note: schemas: [ CUSTOM_ELEMENTS_SCHEMA ] need to be added to each component where you are using custom HTML tags.

Step 3: Declare the arrayData: In your Angular component's TypeScript file, declare the arrayData that you are passing.

 arrayData  =[{"url": "https://i.ibb.co/10fFGkZ/car-race.jpg", "title": "Car Racing", "description":"Car Racing"} , 
    {"url": "https://i.ibb.co/gM5NNJX/butterfly.jpg", "title": "Butterfly", "description":"Butterfly"},
    {"url": "https://i.ibb.co/ygqHsHV/coffee-milk.jpg", "title": "Coffee with Milk", "description":"Coffee with Milk"}];
  autotext  =this.arrayData ;

Now, Set the arraydata value to the sg-autocomplete component

 ngOnInit() {
     const myElement = this.el.nativeElement.querySelector('sg-autocomplete');
      if (myElement) {
        myElement.suggestionlist =this.autotext;
      }
    }

Step 4: Now, in app.component.html you utilize your new custom element.

   <sg-autocomplete autotext="description" dropdown=true></sg-autocomplete>

Note: Here "description" is the JSON element name that will show it as a dropdown suggestion list.

Step 5: To consume the autocomplete value you can create function access it like this

 getValue() {
      const autocompleteValue = this.el.nativeElement.querySelector('sg-autocomplete') as HTMLInputElement;
    } 

c) React

Step 1:
Now we'll add an import to index.js

import { defineCustomElements} from '../node_modules/sg-autocomplete/loader';

And somewhere near the bottom we'll call this function.

defineCustomElements();

Step 2:
Next, in app.js Pass the json array and utilize the new custom element,

function App() {
  const arrayData  =[{"url": 
    {"url": "https://i.ibb.co/gM5NNJX/butterfly.jpg", "title": "Butterfly", "description":"Butterfly"},
    {"url": "https://i.ibb.co/ygqHsHV/coffee-milk.jpg", "title": "Coffee with Milk", "description":"Coffee with Milk"},
    {"url": "https://i.ibb.co/7XqwsLw/fox.jpg", "title": "Fox", "description":"Fox"},
    ];

  const myElement = useRef(arrayData);
    useEffect(() => {
      if (myElement.current) {
        const element = myElement.current;
        document.querySelector('sg-autocomplete').suggestionlist = element;
      }
    }, []);
  return (
    <div>
          <sg-autocomplete autotext="description" dropdown="true"></sg-autocomplete>
    </div>
  );
}

d) Vue

Add defineCustomElements to one of our main files. Specifically main.js for Vue.

import { defineCustomElements} from '../node_modules/sg-autocompleter/loader';

And somewhere near the bottom we'll call this function.

defineCustomElements();

Next, in App.Vue you consume the custom element.

<template>
  <div>
  <sg-autocomplete autotext="description" dropdown="true"></sg-autocomplete>
  </div>
</template>

<script>
    export default {
      data() {
        return {
        arrayData:[
            {"url": "https://i.ibb.co/gM5NNJX/butterfly.jpg", "title": "Butterfly", "description":"Butterfly"},
            {"url": "https://i.ibb.co/VYYPZGk/salmon.jpg", "title": "Salmon ", "description":"Salmon"},
            {"url": "https://i.ibb.co/10fFGkZ/car-race.jpg", "title": "Car Racing", "description":"Car Racing"}  
           ]
      }
   },
    mounted() {
      document.querySelector('sg-autocomplete').suggestionlist = this.arrayData;
    }
};
</script>

Click Here for Vue application live demo.

Hope this article is useful to you and your project, If you like this article, like & share it with your friends.

Follow Me for more articles.

...

🔧 Integrating sg-json-table component with Angular Application


📈 19.72 Punkte
🔧 Programmierung

🔧 Integrating sg-avatar Component in Angular Application


📈 19.72 Punkte
🔧 Programmierung

🔧 Integrating Copyright Component in Angular Application


📈 19.72 Punkte
🔧 Programmierung

🔧 Integrating sg-image-viewer component in Angular, Vue and React Application


📈 19.72 Punkte
🔧 Programmierung

🔧 Integrating sg-image-viewer component


📈 19.72 Punkte
🔧 Programmierung

🔧 Integrating sg-image-viewer component


📈 19.72 Punkte
🔧 Programmierung

🔧 Integrating CanvasJS Charts in Salesforce Lightning Aura Component


📈 19.72 Punkte
🔧 Programmierung

🔧 How to Build a Rating Component with the React Compound Component Pattern


📈 17.3 Punkte
🔧 Programmierung

🔧 Answer: How I can count how many times my component rendered in a react component


📈 17.3 Punkte
🔧 Programmierung

🔧 Reusable Table Component: Compound Component Pattern + Tailwind CSS


📈 17.3 Punkte
🔧 Programmierung

🔧 "Seamless Component Transitions: Achieving Smooth UI Flow with Single Component DOM Transitioning"


📈 17.3 Punkte
🔧 Programmierung

🔧 How to Build a Dynamic Dropdown Component in React – React Compound Component Pattern Explained


📈 17.3 Punkte
🔧 Programmierung

🔧 React Pattern - Build Better Component with Compound component


📈 17.3 Punkte
🔧 Programmierung

🔧 How to use HarmonyOS NEXT - @Component Custom Component?


📈 17.3 Punkte
🔧 Programmierung

🕵️ Joomla! Component JS Support Ticket (component com_jssupportticket) 1.1.5 SQL Injection


📈 17.3 Punkte
🕵️ Sicherheitslücken

⚠️ [webapps] Joomla! Component JS Support Ticket (component com_jssupportticket) 1.1.5 - SQL Injection


📈 17.3 Punkte
⚠️ PoC

🔧 Using Emotion in a New Project: Component Inheritance vs. Single Styled Component with Props


📈 17.3 Punkte
🔧 Programmierung

🔧 Next.js Component Naming Conventions: Best Practices for File and Component Names


📈 17.3 Punkte
🔧 Programmierung

🔧 How to check if a component is a forward ref component in React?


📈 17.3 Punkte
🔧 Programmierung

🔧 How to check if a component is a class component in React?


📈 17.3 Punkte
🔧 Programmierung

🔧 How to Update One Component's State Based on Another Component's Change in Blazor Server


📈 17.3 Punkte
🔧 Programmierung

🔧 When to Use Unstyled Component Libraries Instead of Pre-Styled UI Component Libraries


📈 17.3 Punkte
🔧 Programmierung

🔧 Integrating AdMob in React Native Expo: A Comprehensive Developer's Guide


📈 11.07 Punkte
🔧 Programmierung

🔧 Agile Augmentation: Integrating Augmented Teams into Agile Workflows


📈 11.07 Punkte
🔧 Programmierung

🔧 Integrating Machine Learning Models into Python Back-End Systems


📈 11.07 Punkte
🔧 Programmierung

🔧 The Importance of Middleware in Integrating CIS and GIS Systems


📈 11.07 Punkte
🔧 Programmierung

🔧 Integrating Daytona into Your Spring Boot Bank Application


📈 11.07 Punkte
🔧 Programmierung

🔧 Python Django Financial Trading Dashboard — Integrating AnyChart JS Charts


📈 11.07 Punkte
🔧 Programmierung

🔧 Step-by-Step Guide to Integrating Third-Party APIs in Laravel Applications


📈 11.07 Punkte
🔧 Programmierung

matomo