123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <template lang="pug">
- .searcher
- span.input-icon.fa.fa-search(@click='onClickOptions')
- input.search-input(v-model='search' :placeholder='placeholder' autofocus)
- .dropdown-options(:class="{'input-show': showOptions }")
- ul.input-options
- li.input-option(v-for='option in getOptions()' :key='option.id' @click='onSelectOption(option)') {{ option.name }}
- </template>
- <script>
- export default {
- props: {
- placeholder: {
- type: String,
- default: 'Buscar'
- },
- autofocus: {
- type: Boolean,
- default: false
- },
- items: {
- type: Array,
- default: [],
- required: true
- },
- keys: {
- type: Array,
- default: [],
- required: true
- }
- },
- watch: {
- search(value, lastValue) {
- value = value.trim()
- if (!value && value.length != lastValue.length) {
- this.selectedOption = null
- }
- this.showOptions = !!value && !this.selectedOption
- this.performSearch(value)
- }
- },
- methods: {
- getOptions() {
- return this.results.length == 0 ? this.items : this.results
- },
- performSearch(value) {
- this.results = []
- if (this.selectedOption) {
- return
- }
-
- for (let item of this.items) {
- for (let field in item) {
- if (typeof item[field] !== 'string') {
- continue
- }
- if (this.keys.length !== 0 && this.keys.indexOf(field) === -1) {
- continue
- }
- if (item[field].toLowerCase().indexOf(value.toLowerCase()) !== -1) {
- this.results.push(item)
- break
- }
- }
- }
- },
- onClickOptions() {
- this.showOptions = !this.showOptions || !!this.search
- },
- onSelectOption(item) {
- this.selectedOption = item
- this.search = item.name
- this.results = []
-
- this.$emit('onSelect', item)
- }
- },
- data() {
- return {
- search: '',
- results: [],
- selectedOption: null,
- showOptions: false
- }
- }
- }
- </script>
- <style lang="sass">
- @import '../../assets/variables'
- .searcher
- width: 100%
- height: 35px
- position: relative
- .input-icon
- position: absolute
- top: 14px
- right: 10px
- font-size: 12pt
- color: $app-dark-color
- &:hover
- cursor: pointer
- .search-input
- width: 100%
- height: 100%
- border-radius: 0
- font-size: 12pt
- font-weight: normal
- .dropdown-options
- display: none
- position: absolute
- background: $app-light-color
- border: 1px solid $app-separator-color
- box-shadow: 0 3px 5px $app-separator-color
- z-index: 10
- top: 45px
- left: 0
- right: 0
- bottom: -325px
- padding: 0
- animation-duration: 500ms
- overflow-y: auto
- &.input-show
- display: block
- ul
- li
- height: 35px
- display: block
- text-align: left
- margin: 10px 0 0 10px
- font-size: 12pt
- font-weight: normal
- border-bottom: 1px solid $app-border-color
- &:last-child
- margin-bottom: 10px
- &:hover
- border-bottom: 2px solid $app-main-color
- cursor: pointer
- </style>
|