-
Notifications
You must be signed in to change notification settings - Fork 834
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add onChange callback to search config object. (#469)
* Add onChange callback to search config object. * Let consumer allow in-memory searching to proceed normally.
- Loading branch information
Showing
6 changed files
with
193 additions
and
2 deletions.
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
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