Vue implements an Input component that gets the key display shortcut key effect

Vue implements an Input component that gets the key display shortcut key effect

I encountered a requirement to customize shortcut keys on the page, which required a place to set and display shortcut keys. I looked around Element UI and found that there was no component that could be used with slight modifications, so I wrote one myself.
This only has the function of displaying shortcut keys. The actual binding of shortcut keys depends on the shortcut key data returned and is processed by another component. Currently only Chrome environment has been tested.

The effect is as follows:

Key Points

Although it looks like an Input, it actually displays a label effect inside the component and also requires a delete button. This requires putting the HTML code in the input box. The browser's Input component is obviously not suitable, so you can only imitate the effect of an Input component yourself.

focus, blur, selected highlight effects

Non-Input components do not have effects such as focus, blur, and selected highlight effects. Fortunately, browsers have reserved implementation methods, and netizens have already provided solutions online. By adding the tabindex="0" attribute to the div, the div can obtain these effects.

The tabindex attribute specifies the order of the Tab keys. If you write 0, the components will be selected in the default order. If you write -1, they will never be selected. Because it is in the form of an Input component, it is reasonable that it can be obtained by Tab.

Then add CSS to get the focus border effect, and move the mouse to display the text type pointer

.shortcut-key-input {
 cursor: text;
 transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
}
.shortcut-key-input:focus {
 border-color: #188cff;
 box-shadow: 0 0 4px rgba(24, 140, 255, 0.38);
}

Text prompts

When there is no content, it needs to be the same as Input, and a text prompt can be displayed by default. This also puts a div inside and controls it with Vue. If the output label variable has data, this element will not be displayed.

Cursor flashing effect

This is relatively easy to handle. Put a pseudo-element in the Input class, add this pseudo-element when the focus is acquired, and then give this element a CSS3 animation, and you will have the effect of a flashing cursor.

@keyframes Blink {
 0% { opacity: 0; }
 100% { opacity: 1; }
}
.shortcut-key-input.cursor::after {
 content: "|";
 animation: Blink 1.2s ease 0s infinite;
 font-size: 18px;
 position: absolute;
 top: 1px;
 left: 8px;
}

Key capture

Key capture mainly relies on the keydown event. The returned event will mark whether alt, ctrl (control) and other information are pressed, so key combination can be achieved by relying on this information.
Because each key press will trigger an event, the function key events must be blocked. The code only implements a combination of non-function keys. If a multi-function key is needed, you can create another variable to judge the continuous key press situation and then handle it.

  handleKeydown(e) {
   const { altKey, ctrlKey, shiftKey, key, code } = e;
   if (!CODE_CONTROL.includes(key)) {
    if (!this.keyRange.includes(code)) return;
    let controlKey = "";
    [
     { key: altKey, text: "Alt" },
     { key: ctrlKey, text: "Ctrl" },
     { key: shiftKey, text: "Shift" }
    ].forEach(curKey => {
     if (curKey.key) {
      if (controlKey) controlKey += "+";
      controlKey += curKey.text;
     }
    });
    if (key) {
     if (controlKey) controlKey += "+";
     controlKey += key.toUpperCase();
    }
    this.addHotkey({ text: controlKey, controlKey: { altKey, ctrlKey, shiftKey, key, code } });
   }
   e.preventDefault();
  },

CODE_CONTROL is another preset key code set for easy processing. Originally keyCode was used, but keyCode has been deprecated and code is recommended.
addHotkey is a function added to the corresponding variable, which mainly determines whether there are duplicate shortcut keys.
Then an external verification interface is reserved to determine whether there are duplications when there are multiple shortcut keys.
There is also a max interface that can limit the number of shortcut keys for each component.

  addHotkey(data) {
   if (this.list.length && this.list.some(item => data.text === item.text)) return;
   if (this.list.length && this.list.length.toString() === this.max.toString()) return;
   if (!this.verify(data)) return;
   this.list.push(data);
  }

Online Preview

https://codesandbox.io/s/vue-hotkeyinput-90m2k

The above is the details of Vue's implementation of an Input component that obtains the effect of a key display shortcut. For more information about Vue's display shortcut keys, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Vue2.0 collapsible list v-for loop display example
  • Vue+elementUI component recursively implements foldable dynamic rendering multi-level sidebar navigation
  • Using Vue.js recursive components to implement a collapsible tree menu (demo)
  • Vue folding display multi-line text component implementation code

<<:  Vue uses vue-quill-editor rich text editor and uploads pictures to the server

>>:  How are Vue components parsed and rendered?

Recommend

A brief talk about calculated properties and property listening in Vue

Table of contents 1. Computed properties Syntax: ...

Analysis of two implementation methods for adding static routing in Linux

Command to add a route: 1.Route add route add -ne...

Example of implementing colored progress bar animation using CSS3

Brief Tutorial This is a CSS3 color progress bar ...

How to build gitlab on centos6

Preface The original project was placed on the pu...

How to deal with too many Docker logs causing the disk to fill up

I have a server with multiple docker containers d...

Implementation of a simple login page for WeChat applet (with source code)

Table of contents 1. Picture above 2. User does n...

Tutorial on how to install htop on CentOS 8

If you are looking to monitor your system interac...

How to implement responsive layout in vue-cli

When we are doing front-end development, we will ...

Several solutions for CSS record text icon alignment

It is very common to see images and text displaye...

JS implements a simple todoList (notepad) effect

The notepad program is implemented using the thre...

Detailed explanation of the JVM series memory model

Table of contents 1. Memory model and runtime dat...

Analysis of the causes of accidents caused by Unicode signature BOM

Maybe you are using include files here, which is u...

Causes and solutions for slow MySQL queries

There are many reasons for slow query speed, the ...

How to create, start, and stop a Docker container

1. A container is an independently running applic...

How does MySQL ensure master-slave consistency?

Table of contents The basic principle of MySQL ma...