Element: Input Focus

by Theara Yuom

HTML

<script src="//unpkg.com/vue/dist/vue.js"></script>
<script src="//unpkg.com/[email protected]/lib/index.js"></script>
<div id="app">
  <div>
    <el-form :model="form" ref="form" label-width="150px">
      <el-select v-model="form.type" @change="handleInputFocus" filterable ref="type">
        <el-option label="A" value="A"></el-option>
        <el-option label="B" value="B"></el-option>
      </el-select>

      <el-input v-model.number="form.message" ref="message">
      </el-input>

			<el-button @click="handleInputFocus">Set focus to input</el-button>
      <el-button @click="handleSelectFocus">Set focus to select</el-button>
    </el-form>
  </div>
</div>

SCSS

@import url("//unpkg.com/[email protected]/lib/theme-default/index.css");

Babel + JSX

var Main = {
  data() {
    return {
      form:{
				type:'',
				message:'',
			}
    }
  },
	methods:{
		handleSelectFocus(){
			this.$refs.type.$el.querySelector('input').focus();
		},
		handleInputFocus(){
    	this.$nextTick(() => {
				//this.$refs.message.$el.children[0].focus();
				this.$refs.message.$el.querySelector('input').focus();
     	});
		}
	}
}
var Ctor = Vue.extend(Main)
new Ctor().$mount('#app')