@guardian/threads
Version:
70 lines • 2.7 kB
JavaScript
import { __extends } from "tslib";
import { Chip } from './Chip';
// Base class for chips which are based off of text inputs
// We have quite a few of these so this helps in being DRY
var InputChip = /** @class */ (function (_super) {
__extends(InputChip, _super);
function InputChip() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.textInput = null;
_this.setTextInputRef = function (r) {
return (_this.textInput = r);
};
_this.focus = function (focusEnd) {
if (focusEnd) {
if (_this.textInput) {
var input = _this.textInput;
input.focus();
input.selectionStart = input.selectionEnd = input.value.length;
}
}
else {
if (_this.textInput) {
var input = _this.textInput;
input.focus();
input.selectionStart = input.selectionEnd = 0;
}
}
};
_this.focusAt = function (offset) {
if (_this.textInput) {
var input = _this.textInput;
input.focus();
input.selectionStart = input.selectionEnd = offset;
}
};
_this.select = function () {
if (_this.textInput) {
var input = _this.textInput;
input.select();
}
};
_this.onKeyDown = function (e) {
var inputStart = e.currentTarget.selectionStart;
var inputEnd = e.currentTarget.selectionEnd;
var valueLength = _this.props.value.length;
var noValue = valueLength === 0;
var atEnd = inputStart === valueLength;
var noSelection = inputStart === inputEnd;
if (e.key === 'Enter') {
_this.props.focusElement(_this.props.index + 1, false);
}
else if ((e.key === 'Delete' || e.key === 'Backspace') && noValue) {
e.preventDefault();
_this.props.deleteChip(_this.props.index);
}
else if (e.key === 'ArrowLeft' && inputStart === 0 && noSelection) {
e.preventDefault();
_this.props.focusElement(_this.props.index - 1, true);
}
else if (e.key === 'ArrowRight' && atEnd && noSelection) {
e.preventDefault();
_this.props.focusElement(_this.props.index + 1, false);
}
};
return _this;
}
return InputChip;
}(Chip));
export { InputChip };
//# sourceMappingURL=InputChip.js.map