Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(VTreeview): match loading state with opened node #20404

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

J-Sek
Copy link
Contributor

@J-Sek J-Sek commented Aug 28, 2024

Description

fixes #19390

Currently I need to add value to the initial items to make the loading animation stick to the clicked node. But it should respect item-value.

Needs to be coordinated with #20364 Updated

Markup:

<template>
  <v-card class="ma-4">
    <v-toolbar class="bg-indigo text-h5">
      <span class="pl-6">User Directory</span>
      <v-spacer />
      <v-btn @click="reset" prepend-icon="mdi-reload">Reset</v-btn>
    </v-toolbar>

    <div class="pa-4">
      <v-treeview
        :items="items"
        v-model:opened="open"
        :load-children="loadChildren"
        density="compact"
        item-title="name"
        item-value="id"
        open-on-click
        transition
      >
        <template v-slot:prepend="{ item }">
          <v-icon
            v-if="!item.children"
            :icon="item.isAlbum ? 'mdi-image' : 'mdi-account'"
          />
        </template>
      </v-treeview>
    </div>
  </v-card>
</template>

<script>
  const pause = ms => new Promise(resolve => setTimeout(resolve, ms))

  export default {
    data: () => ({
      open: [],
      users: [],
      albums: [],
    }),

    computed: {
      items() {
        return [
          { id: 11, name: 'Users', children: this.users },
          { id: 12, name: 'Albums', children: this.albums },
        ]
      },
    },

    methods: {
      reset() {
        this.users = []
        this.albums = []
        this.open = []
      },
      async loadChildren(item) {
        switch (item.name) {
          case 'Users': await this.fetchUsers(); break;
          case 'Albums': await this.fetchAlbums(); break;
          default: break;
        }
      },
      async fetchUsers(item) {
        await pause(1000)
        return fetch('https://jsonplaceholder.typicode.com/users')
          .then(res => res.json())
          .then(
            newItems =>
              (this.users = newItems
                .slice(0, 5)
                .map(x => ({ id: x.id, name: x.name })))
          )
      },
      async fetchAlbums(item) {
        await pause(1000)
        return fetch('https://jsonplaceholder.typicode.com/albums')
          .then(res => res.json())
          .then(
            newItems =>
              (this.albums = newItems
                .slice(0, 10)
                .map(x => ({ id: x.id, name: x.title, isAlbum: true })))
          )
      },
    },
  }
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant