JSFiddle - React, Tailwind, and code Playground
HTML
<body>
<textarea id="text"></textarea>
<a href="#" onclick="formatText('[b][/b]');"><i class="fas fa-bold">xasada</i></a>
<script>
function formatText(tag) {
var tags = tag.split("]", 2).map(function(t) {
return t + "]";
});
var Field = document.getElementById('text');
var val = Field.value;
var selected_txt = val.substring(Field.selectionStart, Field.selectionEnd);
var before_txt = val.substring(0, Field.selectionStart);
var after_txt = val.substring(Field.selectionEnd, val.length);
Field.value = before_txt + tags[0] + selected_txt + tags[1] + after_txt;
}
</script>
</body>