-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathreact-search-lunr.story.js
73 lines (64 loc) · 1.6 KB
/
react-search-lunr.story.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import React from 'react'
import PropTypes from 'prop-types'
import { storiesOf } from '@storybook/react'
import { create } from 'reworm'
import ReactSearchLunr from './react-search-lunr'
import moonwalkers from './moonwalkers'
const { get, set } = create({ filter: '' })
class ErrorBoundary extends React.Component {
state = { hasError: false, error: null }
componentDidCatch(error) {
this.setState({ hasError: true, error: error })
}
render() {
if (this.state.hasError) {
return (
<p>
{this.state.error.name} - {this.state.error.message}
</p>
)
}
return this.props.children
}
}
ErrorBoundary.propTypes = {
children: PropTypes.func
}
const renderResults = results =>
results.map(result => (
<p key={result.ref}>
<strong>{result.item.name}</strong> - {result.item.body}
</p>
))
storiesOf('ReactSearchLunr', module)
.add('interactive', () =>
get(s => (
<div>
<input
onChange={e => set({ filter: e.target.value })}
value={s.filter}
/>
<ErrorBoundary key={s.filter}>
<ReactSearchLunr
id="id"
fields={['name', 'body']}
filter={s.filter}
documents={moonwalkers}>
{renderResults}
</ReactSearchLunr>
</ErrorBoundary>
</div>
))
)
.add('initial filter', () => (
<div>
Initial Filter: alan
<ReactSearchLunr
id="id"
fields={['name', 'body']}
filter="alan"
documents={moonwalkers}>
{renderResults}
</ReactSearchLunr>
</div>
))