This repository has been archived by the owner on Apr 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
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
lunik
committed
Nov 5, 2017
1 parent
44232b0
commit 1d1f986
Showing
4 changed files
with
104 additions
and
1 deletion.
There are no files selected for viewing
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
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,34 @@ | ||
import React from 'react' | ||
|
||
export default class Datalist extends React.Component { | ||
constructor (props) { | ||
super(props) | ||
|
||
this.state = { | ||
items: [] | ||
} | ||
|
||
this.initState(props) | ||
} | ||
|
||
initState (props) { | ||
Object.assign(this.state, { | ||
items: props.items | ||
}) | ||
} | ||
|
||
componentWillReceiveProps (props) { | ||
this.initState(props) | ||
} | ||
|
||
render () { | ||
let items = this.state.items.map((item) => ( | ||
<option key={item} value={item}/> | ||
)) | ||
return ( | ||
<datalist id={this.props.id}> | ||
{items} | ||
</datalist> | ||
) | ||
} | ||
} |
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,63 @@ | ||
import React from 'react' | ||
|
||
import Datalist from './index' | ||
|
||
export default class SuggestionInput extends React.Component { | ||
constructor (props) { | ||
super(props) | ||
|
||
this.storageID = '__SearchSuggestions' | ||
this.state = { | ||
items: [] | ||
} | ||
|
||
this.initState(props) | ||
} | ||
|
||
initState (props) { | ||
Object.assign(this.state, {}) | ||
this.load() | ||
} | ||
|
||
componentWillReceiveProps (props) { | ||
this.initState(props) | ||
} | ||
|
||
addItem (item) { | ||
if (this.state.items.indexOf(item) === -1) { | ||
let temp = this.state.items | ||
temp.push(item) | ||
|
||
this.setState({ | ||
items: temp | ||
}) | ||
|
||
this.save() | ||
} | ||
} | ||
|
||
save () { | ||
window.localStorage.setItem(this.storageID, JSON.stringify(this.state.items)) | ||
} | ||
|
||
load () { | ||
try { | ||
let stored = JSON.parse(window.localStorage.getItem(this.storageID)) | ||
|
||
if (!stored) { | ||
throw new Error('LocalStorage empty') | ||
} | ||
|
||
this.state.items = JSON.parse(window.localStorage.getItem(this.storageID)) | ||
} catch (e) { | ||
this.state.items = [] | ||
this.save() | ||
} | ||
} | ||
|
||
render () { | ||
return ( | ||
<Datalist id={this.props.id} items={this.state.items}/> | ||
) | ||
} | ||
} |
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