vuejs-challenges icon indicating copy to clipboard operation
vuejs-challenges copied to clipboard

19 - 切换焦点指令

Open shenrui1 opened this issue 1 year ago • 0 comments

// 你的答案

<script setup lang="ts">
import { ref } from 'vue';

const state = ref(false);

/**
 * Implement the custom directive
 * Make sure the input element focuses/blurs when the 'state' is toggled
 *
 */

const changeState = (el, value) => {
  if (value) {
    el.focus();
  } else {
    el.blur();
  }
};

const VFocus = {
  mounted(el, { value }) {
    changeState(el, value);
  },
  updated(el, { value }) {
    changeState(el, value);
  },
};

setInterval(() => {
  state.value = !state.value;
}, 2000);
</script>

<template>
  <input v-focus="state" type="text" />
</template>

shenrui1 avatar Jan 19 '24 13:01 shenrui1