Skip to content

Commit

Permalink
feat: Close CLI when content is empty and pressing backspace
Browse files Browse the repository at this point in the history
  • Loading branch information
andreidmt committed Sep 16, 2020
1 parent 383912e commit da98436
Showing 1 changed file with 17 additions and 22 deletions.
39 changes: 17 additions & 22 deletions src/ui.cli/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ const commandUI = ({
}) => {
const label = blessed.box({
parent,
style: {
bg: "gray",
},
content: "/",
left,
top,
Expand All @@ -27,9 +24,6 @@ const commandUI = ({
keys: true,
vi: true,
mouse: true,
style: {
bg: "gray",
},
width,
height,
top,
Expand All @@ -46,7 +40,11 @@ const commandUI = ({
}

if (key.full === "backspace") {
onChange(value.substr(0, value.length - 1))
if (value === "") {
onCancel()
} else {
onChange(value.substr(0, value.length - 1))
}
}

if (key.full === "escape") {
Expand All @@ -61,30 +59,27 @@ const commandUI = ({
return [
input,
({ value, isVisible }) => {
if (isVisible) {
label.show()
input.show()
} else {
label.hide()
input.hide()
}

input.setContent(value)

/*
* Similar to:
*
* useEffect(() => {
* if (isVisible === true) {}
* ...
* }, [isVisible])
*/

if (isVisible !== input._.isVisible && isVisible === true) {
input.focus()
if (isVisible !== input._.isVisible) {
if (isVisible === true) {
label.show()
input.show()
input.focus()
} else {
label.hide()
input.hide()
}
}

/**
* Persist state data
/*
* Local props, acts like prevProps
*/

input._.value = value
Expand Down

0 comments on commit da98436

Please sign in to comment.