-
-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
219 additions
and
393 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* This SearchBox is used for fuzzy-find search scenarios, such as the syntax widget tool or | ||
* the package index | ||
*/ | ||
|
||
@bs.send external focus: Dom.element => unit = "focus" | ||
|
||
type state = | ||
| Active | ||
| Inactive | ||
|
||
@react.component | ||
let make = ( | ||
~completionValues: array<string>=[], // set of possible values | ||
~value: string, | ||
~onClear: unit => unit, | ||
~placeholder: string="", | ||
~onValueChange: string => unit, | ||
) => { | ||
let (state, setState) = React.useState(_ => Inactive) | ||
let textInput = React.useRef(Js.Nullable.null) | ||
|
||
let onMouseDownClear = evt => { | ||
ReactEvent.Mouse.preventDefault(evt) | ||
onClear() | ||
} | ||
|
||
let focusInput = () => | ||
textInput.current->Js.Nullable.toOption->Belt.Option.forEach(el => el->focus) | ||
|
||
let onAreaFocus = evt => { | ||
let el = ReactEvent.Focus.target(evt) | ||
let isDiv = Js.Null_undefined.isNullable(el["type"]) | ||
|
||
if isDiv && state === Inactive { | ||
focusInput() | ||
} | ||
} | ||
|
||
let onFocus = _ => { | ||
setState(_ => Active) | ||
} | ||
|
||
let onBlur = _ => { | ||
setState(_ => Inactive) | ||
} | ||
|
||
let onKeyDown = evt => { | ||
let key = ReactEvent.Keyboard.key(evt) | ||
let ctrlKey = ReactEvent.Keyboard.ctrlKey(evt) | ||
|
||
let full = (ctrlKey ? "CTRL+" : "") ++ key | ||
|
||
switch full { | ||
| "Escape" => onClear() | ||
| "Tab" => | ||
if Js.Array.length(completionValues) === 1 { | ||
let targetValue = Belt.Array.getExn(completionValues, 0) | ||
|
||
if targetValue !== value { | ||
ReactEvent.Keyboard.preventDefault(evt) | ||
onValueChange(targetValue) | ||
} else { | ||
() | ||
} | ||
} | ||
| _ => () | ||
} | ||
} | ||
|
||
let onChange = evt => { | ||
ReactEvent.Form.preventDefault(evt) | ||
let value = ReactEvent.Form.target(evt)["value"] | ||
onValueChange(value) | ||
} | ||
|
||
<div | ||
tabIndex={-1} | ||
onFocus=onAreaFocus | ||
onBlur | ||
className={( | ||
state === Active ? "border-fire" : "border-fire-40" | ||
) ++ " flex items-center border rounded-lg py-4 px-5"}> | ||
<Icon.MagnifierGlass | ||
className={(state === Active ? "text-fire" : "text-fire-80") ++ " w-4 h-4"} | ||
/> | ||
<input | ||
value | ||
ref={ReactDOM.Ref.domRef(textInput)} | ||
onFocus | ||
onKeyDown | ||
onChange={onChange} | ||
placeholder | ||
className="text-16 outline-none ml-4 w-full" | ||
type_="text" | ||
/> | ||
<button onFocus className={value === "" ? "hidden" : "block"} onMouseDown=onMouseDownClear> | ||
<Icon.Close className="w-4 h-4 text-fire" /> | ||
</button> | ||
</div> | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.