Skip to content

Custom form field #103

@rokkay

Description

@rokkay

Hello, im trying to do a two custom form fields for categories and subcategories in which one of them depends on the other. Im doing this with axios and get method, all fine but when i select the value on the second select the array of values disappear until i select a new subcategory.

Explanation in gif

This is my code:

SharpFormSelectCategoriesField.vue

<template>
  <div>
    <select class="SharpText multiselect" @change="handleChanged" :value="categoriaSeleccionada">
      <option v-for="(categoria, index) in categorias" 
      :key="`categoria-${index}`" 
      :value="`${index}`">
      {{categoria}}
      </option>
    </select>
  </div>
</template>
<script>
import { EventBus } from '../sharp-plugin.js';
export default {
  props: {
    categorias: Array,
    categoriaSeleccionada: String
  },
  mounted() {
      EventBus.$emit("categoria", this.categoriaSeleccionada);
  },
  methods: {
    handleChanged(e) {
      this.$emit("input", e.target.value);
      EventBus.$emit("categoria", e.target.value);
    }
  }
};
</script>

SharpFormSelectSubcategoryField.vue

<template>
  <div>
    <select class="SharpText multiselect" @change="handleChanged" :value="subcategoriaSeleccionada">
      <option v-for="(subcategoria, index) in subcategorias" 
      :value="`${index}`" 
      :key="`subcategoria-${index}`">
      {{subcategoria}}
      </option>
    </select>
  </div>
</template>
<script>
import { EventBus } from '../sharp-plugin.js';
window.axios = require('axios');
export default {
  props: {
    subcategorias: Array,
    subcategoriaSeleccionada: String
  },
  created () {
    EventBus.$on('categoria', idCategoria => {
      console.log(`Categoria: ${idCategoria}`);
      axios.get(`http://test.mtkadventure/api/subcategorias/${idCategoria}`)
      .then(response => (this.subcategorias = response.data));
    });
  },
  methods: {
    handleChanged(e) {
      this.$emit("input", e.target.value);
      console.log(this.subcategorias);
    }
  }
};
</script>

If i comment the this.$emit("input", e.target.value); on the subcategory select this not happens, but the form data cannot be updated. Is this a bug or i need a break? Is there a better way to achive this?

Thanks in advance and excuse me if im being so tedioud but i really like Sharp and want to dominate it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions