Skip to content

Commit

Permalink
Improve text input focus tabbing, fix React key errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ryyppy committed Dec 30, 2020
1 parent 3dcb13c commit faf0f9a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 22 deletions.
33 changes: 20 additions & 13 deletions components/SyntaxLookupWidget.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 19 additions & 9 deletions components/SyntaxLookupWidget.res
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ module MdxComp = {
`
)

@bs.get
external frontmatter: t => Js.Json.t = "frontmatter"
/* @bs.get */
/* external frontmatter: t => Js.Json.t = "frontmatter" */
}

@module("misc_docs/syntax/decorator_module.mdx")
Expand Down Expand Up @@ -106,6 +106,9 @@ let getAnchor = path => {

module SearchBox = {
@bs.send external focus: Dom.element => unit = "focus"
@bs.send external blur: Dom.element => unit = "blur"

external toDomElement: Js.t<'a> => Dom.element = "%identity"

type state =
| Active
Expand All @@ -129,8 +132,11 @@ module SearchBox = {
let focusInput = () =>
textInput.current->Js.Nullable.toOption->Belt.Option.forEach(el => el->focus)

let onAreaFocus = _evt => {
if state === Inactive {
let onAreaFocus = evt => {
let el = ReactEvent.Focus.target(evt)
let isDiv = Js.Null_undefined.isNullable(el["type"])

if isDiv && state === Inactive {
focusInput()
}
}
Expand Down Expand Up @@ -175,6 +181,7 @@ module SearchBox = {
<div
tabIndex={-1}
onFocus=onAreaFocus
onBlur
className={(
state === Active ? "border-fire" : "border-fire-40"
) ++ " flex items-center border rounded-lg py-4 px-5"}>
Expand All @@ -185,14 +192,13 @@ module SearchBox = {
value
ref={ReactDOM.Ref.domRef(textInput)}
onFocus
onBlur
onKeyDown
onChange={onChange}
placeholder="Enter keywords or syntax..."
className="text-16 outline-none ml-4 w-full"
type_="text"
/>
<button className={value === "" ? "hidden" : "block"} onMouseDown=onMouseDownClear>
<button onFocus className={value === "" ? "hidden" : "block"} onMouseDown=onMouseDownClear>
<Icon.Close className="w-4 h-4 text-fire" />
</button>
</div>
Expand All @@ -218,7 +224,11 @@ module DetailBox = {
React.string(first),
<span className="text-fire"> {React.string(second)} </span>,
React.string(third),
]->React.array
]
->Belt.Array.mapWithIndex((i, el) => {
<span key={Belt.Int.toString(i)}> el </span>
})
->React.array
| more => Belt.Array.map(more, s => React.string(s))->React.array
}

Expand Down Expand Up @@ -347,8 +357,8 @@ let make = () => {
</span>
})
let el =
<div className="first:mt-0 mt-12">
<Category key=title title> {React.array(children)} </Category>
<div key=title className="first:mt-0 mt-12">
<Category title> {React.array(children)} </Category>
</div>
Js.Array2.push(acc, el)->ignore
acc
Expand Down

0 comments on commit faf0f9a

Please sign in to comment.