Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <template lang="pug">
  2.   .input-modal
  3.  
  4.     .input-group(ref="inputGroup")
  5.         .input-group-prepend(v-if="inputIcon || imgIcon")
  6.           span.input-group-text
  7.             i(v-if="inputIcon" :class="inputIcon")
  8.             img(v-if="imgIcon" :src="imgIcon")
  9.        
  10.         input.form-control(
  11.           type="text" :placeholder="placeholder"
  12.           v-model="input" :class="[classInput, {'is-invalid': errors.has(name)}]"
  13.           @focus="focus(true)" autocomplete="off"
  14.           v-validate="validations" :name="name"
  15.           )
  16.         .invalid-feedback(v-if="errors.has(name)") {{ errors.first(name) }}
  17.  
  18.     <!-- modal -->
  19.     transition(name="fade")
  20.       .modal(v-show="focused" ref="modal")
  21.         .modal-dialog.modal-lg(role="document")
  22.           .modal-content
  23.             .modal-header.bg-primary.text-white.justify-content-between
  24.               h6.modal-title Pilih Nama
  25.               button.btn.btn-link.p-0(@focus="focus(false)")
  26.                 .h5.m-0.text-white x
  27.  
  28.             .modal-body
  29.               .row
  30.                 .col-md-6.col-12
  31.                   ul.list
  32.                     li.list-item(
  33.                       v-for="name in data"
  34.                       :key="name.username"
  35.                       @click="select(name.username)"
  36.                       )
  37.                       .name
  38.                         span.mr-3 {{ name.username }}
  39.                         span.text-muted {{ name.name }}
  40. </template>
  41.  
  42. <script>
  43. import { mapGetters } from "vuex";
  44.  
  45. export default {
  46.   inject: ["$validator"],
  47.   props: {
  48.     inputIcon: { type: String, required: false, default: "" },
  49.     imgIcon: { type: String, required: false, default: "" },
  50.     placeholder: { type: String, required: false, default: "" },
  51.     classInput: { type: String, required: false, default: "" },
  52.     username: { type: String, required: true },
  53.     validations: { type: [String, Object], default: "" },
  54.     name: { type: String, required: true }
  55.   },
  56.  
  57.   data: () => ({
  58.     focused: false,
  59.     input: "",
  60.     selected: ""
  61.   }),
  62.  
  63.   computed: {
  64.     ...mapGetters({
  65.       data: "names/nameAll"
  66.     })
  67.   },
  68.  
  69.   watch: {
  70.     errors: function(error) {
  71.       this.$emit("has-error");
  72.     }
  73.   },
  74.  
  75.   created() {
  76.     this.input = this.username || "";
  77.     console.log("username", this.username);
  78.     // if (this.username !== "") this.select(this.username);
  79.   },
  80.  
  81.   mounted() {
  82.     console.log("username mount", this.username);
  83.     // if (this.username !== "") this.select(this.username);
  84.   },
  85.  
  86.   updated() {
  87.     this.$emit("input", this.selected);
  88.   },
  89.  
  90.   methods: {
  91.     focus(bol) {
  92.       this.focused = bol;
  93.     },
  94.     select(username) {
  95.       console.log("select", username);
  96.       this.input = this.nameInfo(username);
  97.       this.selected = username;
  98.       this.focus(false);
  99.     },
  100.     nameInfo(username) {
  101.       console.log("nameInfo", this.data); // nameInfo kosong pertama kali load page
  102.       let names = this.data.filter(name => name.username == username);
  103.  
  104.       // console.log("filter", names);
  105.       if (names.length) return `${names[0].name} (${names[0].username})`;
  106.     }
  107.   }
  108. };
  109. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement