JSFiddle - React, Tailwind, and code Playground

HTML

Type something
<input type="text" id="myinput" />

JavaScript

String.prototype.splice = function( idx, rem, s ) {
    return (this.slice(0,idx) + s + this.slice(idx + Math.abs(rem)));
};

String.prototype.addDash = function () {
    return this.splice( -3, 0, ' ' );
};

document.getElementById('myinput').addEventListener('keyup', function (e) {
    this.value = this.value.replace(/\-/gi, '');
    if (this.value.length >= 4) {
        this.value = this.value.addDash();
    }
});