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

QChipsInput: add QAutocomplete support #1932

Merged
merged 1 commit into from
Apr 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions dev/components/form/all.vue
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,13 @@
<q-chips-input :dark="dark" color="black" :error="error" :warning="warning" :disable="disable" :readonly="readonly" inverted class="q-ma-sm" @focus="onFocus" @blur="onBlur" @change="val => { options = val; onChange(val) }" @input="onInput" @clear="onClear" float-label="List (onChange)" :value="options" />
<q-chips-input :dark="dark" color="green" :error="error" :warning="warning" :disable="disable" :readonly="readonly" inverted class="q-ma-sm" @focus="onFocus" @blur="onBlur" @change="val => { options = val; onChange(val) }" @input="onInput" @clear="onClear" float-label="List (onChange)" :value="options" />

<q-chips-input :dark="dark" :error="error" :warning="warning" :disable="disable" :readonly="readonly" class="q-ma-sm" @focus="onFocus" @blur="onBlur" @change="onChange" @input="onInput" @clear="onClear" float-label="List autocomplete" v-model="options">
<q-autocomplete :static-data="{field: 'value', list: countries}" @selected="selected" />
</q-chips-input>
<q-chips-input :dark="dark" :error="error" :warning="warning" :disable="disable" :readonly="readonly" class="q-ma-sm" @focus="onFocus" @blur="onBlur" @change="val => { options = val; onChange(val) }" @input="onInput" @clear="onClear" float-label="List autocomplete (onChange)" :value="options">
<q-autocomplete :static-data="{field: 'value', list: countries}" @selected="selected" />
</q-chips-input>

<p class="q-subtitle">Selected option: {{ JSON.stringify(option) }}</p>
<q-select :dark="dark" :error="error" :warning="warning" :disable="disable" :readonly="readonly" :clearable="clearable" class="q-ma-sm" @focus="onFocus" @blur="onBlur" @change="onChange" @input="onInput" @clear="onClear" v-model="option" :options="countries" placeholder="Select" filter />
<q-select :dark="dark" :error="error" :warning="warning" :disable="disable" :readonly="readonly" :clearable="clearable" class="q-ma-sm" @focus="onFocus" @blur="onBlur" @change="onChange" @input="onInput" @clear="onClear" v-model="option" :options="countries" float-label="Select (autofocus filter)" filter autofocus-filter />
Expand Down
10 changes: 8 additions & 2 deletions src/components/autocomplete/QAutocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,14 @@ export default {
anchorClick: false
},
on: {
show: () => this.$emit('show'),
hide: () => this.$emit('hide')
show: () => {
this.__input.selectionOpen = true
this.$emit('show')
},
hide: () => {
this.__input.selectionOpen = false
this.$emit('hide')
}
}
}, [
h(QList, {
Expand Down
74 changes: 69 additions & 5 deletions src/components/chips-input/QChipsInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,15 @@
/>
</div>

<q-spinner
v-if="isLoading"
slot="after"
size="24px"
class="q-if-control"
></q-spinner>

<q-icon
v-if="editable"
v-else-if="editable"
:name="computedAddIcon"
slot="after"
class="q-if-control"
Expand All @@ -70,6 +77,8 @@
@touchstart.native="__clearTimer"
@click.native="add()"
></q-icon>

<slot></slot>
</q-input-frame>
</template>

Expand All @@ -79,13 +88,15 @@ import InputMixin from '../../mixins/input'
import { QInputFrame } from '../input-frame'
import { QChip } from '../chip'
import { getEventKey, stopAndPrevent } from '../../utils/event'
import { QSpinner } from '../spinner'

export default {
name: 'QChipsInput',
mixins: [FrameMixin, InputMixin],
components: {
QInputFrame,
QChip
QChip,
QSpinner
},
props: {
value: {
Expand All @@ -100,12 +111,36 @@ export default {
data () {
return {
input: '',
model: this.value
model: this.value,
watcher: null,
shadow: {
val: this.input,
set: this.add,
loading: false,
selectionOpen: false,
watched: 0,
isDark: () => this.dark,
hasFocus: () => document.activeElement === this.$refs.input,
register: () => {
this.shadow.watched += 1
this.__watcherRegister()
},
unregister: () => {
this.shadow.watched = Math.max(0, this.shadow.watched - 1)
this.__watcherUnregister()
},
getEl: () => this.$refs.input
}
}
},
watch: {
value (v) {
this.model = this.value
this.model = v
}
},
provide () {
return {
__input: this.shadow
}
},
computed: {
Expand All @@ -114,6 +149,9 @@ export default {
? this.model.length
: 0
},
isLoading () {
return this.loading || (this.shadow.watched && this.shadow.loading)
},
computedAddIcon () {
return this.addIcon || this.$q.icon.chipsInput.add
},
Expand Down Expand Up @@ -151,7 +189,7 @@ export default {
clearTimeout(this.timer)
this.focus()

if (!this.editable || !value) {
if (this.isLoading || !this.editable || !value) {
return
}
if (this.model.includes(value)) {
Expand All @@ -177,6 +215,9 @@ export default {
__handleKeyDown (e) {
switch (getEventKey(e)) {
case 13: // ENTER key
if (this.shadow.selectionOpen) {
return
}
stopAndPrevent(e)
return this.add()
case 8: // Backspace key
Expand All @@ -190,7 +231,30 @@ export default {
},
__onClick () {
this.focus()
},
__watcher (value) {
if (this.shadow.watched) {
this.shadow.val = value
}
},
__watcherRegister () {
if (!this.watcher) {
this.watcher = this.$watch('input', this.__watcher)
}
},
__watcherUnregister (forceUnregister) {
if (
this.watcher &&
(forceUnregister || !this.shadow.watched)
) {
this.watcher()
this.watcher = null
this.shadow.selectionOpen = false
}
}
},
beforeDestroy () {
this.__watcherUnregister(true)
}
}
</script>
2 changes: 1 addition & 1 deletion src/components/input/QInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export default {
return this.type === 'textarea'
},
isLoading () {
return this.loading || this.shadow.loading
return this.loading || (this.shadow.watched && this.shadow.loading)
},
keyboardToggle () {
return this.$q.platform.is.mobile &&
Expand Down