-
Notifications
You must be signed in to change notification settings - Fork 841
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add onChange callback to search config object. #469
Merged
cjcenizal
merged 2 commits into
elastic:master
from
cjcenizal:in-memory-table-search-callback
Mar 7, 2018
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
137 changes: 137 additions & 0 deletions
137
src-docs/src/views/tables/in_memory/in_memory_search_callback.js
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,137 @@ | ||
import React, { Fragment } from 'react'; | ||
import { formatDate } from '../../../../../src/services/format'; | ||
import { createDataStore } from '../data_store'; | ||
import { | ||
EuiInMemoryTable, | ||
EuiLink, | ||
EuiHealth, | ||
EuiSpacer, | ||
EuiSwitch, | ||
EuiFlexGroup, | ||
EuiFlexItem | ||
} from '../../../../../src/components'; | ||
|
||
/* | ||
Example user object: | ||
|
||
{ | ||
id: '1', | ||
firstName: 'john', | ||
lastName: 'doe', | ||
github: 'johndoe', | ||
dateOfBirth: Date.now(), | ||
nationality: 'NL', | ||
online: true | ||
} | ||
|
||
Example country object: | ||
|
||
{ | ||
code: 'NL', | ||
name: 'Netherlands', | ||
flag: '🇳🇱' | ||
} | ||
*/ | ||
|
||
let debounceTimeoutId; | ||
let requestTimeoutId; | ||
const store = createDataStore(); | ||
|
||
export class Table extends React.Component { | ||
|
||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
items: store.users, | ||
isLoading: false, | ||
}; | ||
} | ||
|
||
onQueryChange = query => { | ||
clearTimeout(debounceTimeoutId); | ||
clearTimeout(requestTimeoutId); | ||
|
||
debounceTimeoutId = setTimeout(() => { | ||
this.setState({ | ||
isLoading: true, | ||
}); | ||
|
||
requestTimeoutId = setTimeout(() => { | ||
const items = store.users.filter(user => { | ||
const normalizedName = `${user.firstName} ${user.lastName}`.toLowerCase(); | ||
const normalizedQuery = query.text.toLowerCase(); | ||
return normalizedName.indexOf(normalizedQuery) !== -1; | ||
}); | ||
|
||
this.setState({ | ||
isLoading: false, | ||
items, | ||
}) | ||
}, 1000); | ||
}, 300); | ||
}; | ||
|
||
render() { | ||
const search = { | ||
onChange: this.onQueryChange, | ||
box: { | ||
incremental: true, | ||
}, | ||
}; | ||
|
||
return ( | ||
<EuiInMemoryTable | ||
items={this.state.items} | ||
loading={this.state.isLoading} | ||
columns={[ | ||
{ | ||
field: 'firstName', | ||
name: 'First Name', | ||
sortable: true | ||
}, | ||
{ | ||
field: 'lastName', | ||
name: 'Last Name' | ||
}, | ||
{ | ||
field: 'github', | ||
name: 'Github', | ||
render: (username) => ( | ||
<EuiLink href={`https://github.com/${username}`} target="_blank">{username}</EuiLink> | ||
) | ||
}, | ||
{ | ||
field: 'dateOfBirth', | ||
name: 'Date of Birth', | ||
dataType: 'date', | ||
render: (date) => formatDate(date, 'dobLong'), | ||
sortable: true | ||
}, | ||
{ | ||
field: 'nationality', | ||
name: 'Nationality', | ||
render: (countryCode) => { | ||
const country = store.getCountry(countryCode); | ||
return `${country.flag} ${country.name}`; | ||
} | ||
}, | ||
{ | ||
field: 'online', | ||
name: 'Online', | ||
dataType: 'boolean', | ||
render: (online) => { | ||
const color = online ? 'success' : 'danger'; | ||
const label = online ? 'Online' : 'Offline'; | ||
return <EuiHealth color={color}>{label}</EuiHealth>; | ||
}, | ||
sortable: true | ||
} | ||
]} | ||
search={search} | ||
pagination={true} | ||
sorting={true} | ||
/> | ||
); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src-docs/src/views/tables/in_memory/in_memory_search_callback_section.js
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,35 @@ | ||
import React from 'react'; | ||
import { | ||
EuiCode | ||
} from '../../../../../src/components'; | ||
import { GuideSectionTypes } from '../../../components'; | ||
import { renderToHtml } from '../../../services'; | ||
|
||
import { Table } from './in_memory_search_callback'; | ||
import { propsInfo } from './props_info'; | ||
|
||
const source = require('!!raw-loader!./in_memory_search_callback'); | ||
const html = renderToHtml(Table); | ||
|
||
export const searchCallbackSection = { | ||
title: 'In-Memory Table - With Search Callback', | ||
source: [ | ||
{ | ||
type: GuideSectionTypes.JS, | ||
code: source, | ||
}, { | ||
type: GuideSectionTypes.HTML, | ||
code: html, | ||
} | ||
], | ||
text: ( | ||
<div> | ||
<p> | ||
The example shows how to configure <EuiCode>EuiInMemoryTable</EuiCode> to display a search bar | ||
and intercept the search value when it changes so you can perform your own search logic. | ||
</p> | ||
</div> | ||
), | ||
props: propsInfo, | ||
demo: <Table/> | ||
}; |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { section } from './in_memory_section'; | ||
export { selectionSection } from './in_memory_selection_section'; | ||
export { searchSection } from './in_memory_search_section'; | ||
export { searchCallbackSection } from './in_memory_search_callback_section'; |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this should return. There are cases where the consumer will want the table to filter the results like normal. This is just a callback that gives the consumer the option to fetch new results if needed.