1
0
mirror of https://github.com/PanJiaChen/vue-element-admin.git synced 2025-08-10 20:39:48 +08:00

feat: Add handle focus to field. (#491)

* feat: Add handle focus to field.

* set example into test view and fix set focus with mounted hook.

* change named attributes.

* change attributes name to handleRequestFocus and handleContentSelection.
This commit is contained in:
Edwin Betancourt 2020-05-08 17:01:06 -04:00 committed by GitHub
parent 6f7b567195
commit 3559c61e89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 7 deletions

View File

@ -18,7 +18,7 @@ export const fieldMixin = {
value = this.valueModel value = this.valueModel
} }
return { return {
value: value value
} }
}, },
computed: { computed: {
@ -36,8 +36,16 @@ export const fieldMixin = {
this.preHandleChange(value) this.preHandleChange(value)
} }
}, },
mounted() {
if (this.metadata.handleRequestFocus) {
this.requestFocus()
}
},
methods: { methods: {
activeFocus() { /**
* Set focus if handle focus attribute is true
*/
requestFocus() {
if (this.$refs[this.metadata.columnName]) { if (this.$refs[this.metadata.columnName]) {
this.$refs[this.metadata.columnName].focus() this.$refs[this.metadata.columnName].focus()
} }
@ -51,7 +59,7 @@ export const fieldMixin = {
this.handleChange(value) this.handleChange(value)
}, },
focusGained(value) { focusGained(value) {
if (this.metadata.isAutoSelection) { if (this.metadata.handleContentSelection) {
// select all the content inside the text box // select all the content inside the text box
if (!this.isEmptyValue(value.target.selectionStart) && if (!this.isEmptyValue(value.target.selectionStart) &&
!this.isEmptyValue(value.target.selectionStart)) { !this.isEmptyValue(value.target.selectionStart)) {

View File

@ -374,8 +374,8 @@ export default {
}, },
methods: { methods: {
focusField() { focusField() {
if (this.isDisplayed && !this.isReadOnly) { if (this.field.handleRequestFocus || (this.field.displayed && !this.field.readonly)) {
this.$refs[this.field.columnName].activeFocus() this.$refs[this.field.columnName].requestFocus()
} }
} }
} }

View File

@ -700,13 +700,18 @@ export default {
} }
} }
this.setTagsViewTitle(uuidRecord) this.setTagsViewTitle(uuidRecord)
this.setFocus() if (this.$route.query.action === 'create-new') {
this.setFocus()
}
const currentRecord = this.getterDataStore.record.find(record => record.UUID === uuidRecord) const currentRecord = this.getterDataStore.record.find(record => record.UUID === uuidRecord)
this.$store.dispatch('currentRecord', currentRecord) this.$store.dispatch('currentRecord', currentRecord)
}, },
async setFocus() { async setFocus() {
return new Promise(resolve => { return new Promise(resolve => {
const fieldFocus = this.getterFieldList.find(itemField => { const fieldFocus = this.getterFieldList.find(itemField => {
if (itemField.handleRequestFocus) {
return true
}
if (Object.prototype.hasOwnProperty.call(this.$refs, itemField.columnName)) { if (Object.prototype.hasOwnProperty.call(this.$refs, itemField.columnName)) {
if (fieldIsDisplayed(itemField) && !itemField.isReadOnly && itemField.isUpdateable && itemField.componentPath !== 'FieldSelect') { if (fieldIsDisplayed(itemField) && !itemField.isReadOnly && itemField.isUpdateable && itemField.componentPath !== 'FieldSelect') {
return true return true

View File

@ -6,7 +6,8 @@ export default [
columnName: 'URL', columnName: 'URL',
definition: { definition: {
name: 'Web', name: 'Web',
isAutoSelection: true, handleRequestFocus: true,
handleContentSelection: true,
displayType: URL.id displayType: URL.id
} }
}, },