Last active
May 22, 2018 21:45
-
-
Save mlms13/355bcd31d8b56dd9a839b81701bd2bda to your computer and use it in GitHub Desktop.
A stateful search input that notifies its parent onblur or on the enter key
This file contains hidden or 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
type state = string; | |
type action = | |
| SetValue(string); | |
let component = ReasonReact.reducerComponent("SearchInput"); | |
let valueFromEvent = (evt) => | |
ReactDOMRe.domElementToObj(ReactEventRe.Form.target(evt))##value; | |
let make = (~value, ~onChange, _children) => { | |
...component, | |
initialState: () => value, | |
reducer: (action, _state) => switch action { | |
| SetValue(str) => Update(str) | |
}, | |
render: self => | |
<div className="huey-search"> | |
<i className="huey-search-icon material-icons">{ReasonReact.string("search")}</i> | |
<input | |
className="huey-search-input" | |
onChange={e => self.send(SetValue(valueFromEvent(e)))} | |
onBlur={_e => onChange(self.state)} | |
onKeyDown={e => if (ReactEventRe.Keyboard.which(e) == 13) onChange(self.state)} | |
_type="search" | |
value=self.state | |
/> | |
</div> | |
}; | |
/** | |
* Usage: | |
* <SearchInput value="Foo" onChange={str => alert(str)} /> | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment