npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-dynamic-cascading-select

v1.0.1

Published

Vue 3 component for selecting addresses through multi-level dropdowns.

Downloads

16

Readme

Vue 3 component for selecting addresses through multi-level dropdowns.

Props

The component accepts the following props:

  • initialId (number | null) - the initial identifier used to load initial data when the component is mounted.

  • mode (create |'view | edit) - determines the mode of the component. In 'view' mode, data modification is not allowed. In 'edit' and 'create' modes, data can be modified.

  • fetchOptions ((parentId: number | null, query?: string) => Promise<Address[]>) - function to load options for the select. It takes parentId (identifier of the parent item) and query (a string for filtering options).

  • fetchAncestry ((id: number) => Promise<Address[]>) - function to load the entire chain of ancestors for a given id. Used to initialize the component in edit and view modes.

  • showAddButton (boolean) - determines whether the button to create a new item should be displayed.

Events

The component emits the following events:

  • update:selectedId (id: number | null) - event that is emitted when the selected item changes. The id of the selected item or null is passed if the selection is cleared.

  • addNew (index: number | null) - event that is emitted when the button to create a new item is clicked. The index of the current level or null is passed.

Example

# Yarn
yarn add vue-dynamic-cascading-select

# NPM
npm install vue-dynamic-cascading-select --save
import { DynamicCascadingSelect } from 'vue-dynamic-cascading-select'
import 'vue-dynamic-cascading-select/dist/style.css'

const baseUrl = 'https://66be58a374dfc195586f3a53.mockapi.io/api/v1'

const fetchOptions = async (parentId: number | null, query?: string) => {
  try {
    const url = new URL(`${baseUrl}/address`)
    if (parentId != null) {
      url.searchParams.set('parentId', parentId)
    }
    if (query != undefined) {
      url.searchParams.set('query', query)
    }
    const res = await fetch(url)
    if (!res.ok) {
      throw new Error('Network error')
    }
    const data = await res.json()
    return data
  } catch (error) {
    console.error(error)
    return []
  }
}

const fetchAncestry = async (id: number) => {
  const ancestry = []
  let currentId = id
  while (currentId !== null) {
    try {
      const res = await fetch(`${baseUrl}/address/${currentId}`)
      if (!res.ok) {
        throw new Error('Network error')
      }
      const data = await res.json()
      ancestry.unshift(data)
      currentId = data.parentId
    } catch (error) {
      console.error(error)
      break
    }
  }
  return ancestry
}

const handleSelectedId = (id: number | null): void => {
  console.log(id)
}

const handleAddNew = (id: number | null): void => {
  alert(id ?? 'parent')
}
<el-row :gutter="20">
  <el-col :span="8">
    <el-text>View</el-text>
    <dynamic-cascading-select
      mode="view"
      :initial-id="3"
      :fetch-options="fetchOptions"
      :fetch-ancestry="fetchAncestry"
    />
  </el-col>
  <el-col :span="8">
    <el-text>Edit</el-text>
    <dynamic-cascading-select
      mode="edit"
      :initial-id="6"
      :fetch-options="fetchOptions"
      :fetch-ancestry="fetchAncestry"
      @update:selectedId="handleSelectedId"
      @add-new="handleAddNew"
      show-add-button
    />
  </el-col>
  <el-col :span="8">
    <el-text>Create</el-text>
    <dynamic-cascading-select
      mode="create"
      :fetch-options="fetchOptions"
      :fetch-ancestry="fetchAncestry"
      @update:selected-id="handleSelectedId"
      @add-new="handleAddNew"
    />
  </el-col>
</el-row>