Skip to content

Commit

Permalink
[Enhancement] Fixed issue gregnb#447 (Search box - Default Text)
Browse files Browse the repository at this point in the history
Added searchPlaceholder option
  • Loading branch information
alielkhateeb committed Aug 21, 2019
1 parent 8438fe3 commit f6aa0bd
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ The component accepts the following props:
|**`filter`**|boolean|true|Show/hide filter icon from toolbar
|**`search`**|boolean|true|Show/hide search icon from toolbar
|**`searchText`**|string||Initial search text
|**`searchPlaceholder`**|string||Search text placeholder. [Example](https://github.com/gregnb/mui-datatables/blob/master/examples/customize-search-placeholder/index.js)
|**`print`**|boolean|true|Show/hide print icon from toolbar
|**`download`**|boolean|true|Show/hide download icon from toolbar
|**`downloadOptions`**|object||Options to change the output of the CSV file. Default options: `{filename: 'tableDownload.csv', separator: ','}`
Expand Down
96 changes: 96 additions & 0 deletions examples/customize-search-placeholder/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import React, { Fragment } from 'react';
import ReactDOM from 'react-dom';
import MUIDataTable from '../../src/';

class Example extends React.Component {
state = {
searchText: 'Computer'
};

render() {
const columns = [
{
name: 'Name',
options: {
filter: true,
display: 'excluded',
},
},
{
label: 'Modified Title Label',
name: 'Title',
options: {
filter: true,
},
},
{
name: 'Location',
options: {
filter: false,
},
},
{
name: 'Age',
options: {
filter: true,
},
},
{
name: 'Salary',
options: {
filter: true,
sort: false,
},
},
];

const data = [
['Gabby George', 'Business Analyst', 'Minneapolis', 30, '$100,000'],
['Aiden Lloyd', 'Business Consultant', 'Dallas', 55, '$200,000'],
['Jaden Collins', 'Attorney', 'Santa Ana', 27, '$500,000'],
['Franky Rees', 'Business Analyst', 'St. Petersburg', 22, '$50,000'],
['Aaren Rose', 'Business Consultant', 'Toledo', 28, '$75,000'],
['Blake Duncan', 'Business Management Analyst', 'San Diego', 65, '$94,000'],
['Frankie Parry', 'Agency Legal Counsel', 'Jacksonville', 71, '$210,000'],
['Lane Wilson', 'Commercial Specialist', 'Omaha', 19, '$65,000'],
['Robin Duncan', 'Business Analyst', 'Los Angeles', 20, '$77,000'],
['Mel Brooks', 'Business Consultant', 'Oklahoma City', 37, '$135,000'],
['Harper White', 'Attorney', 'Pittsburgh', 52, '$420,000'],
['Kris Humphrey', 'Agency Legal Counsel', 'Laredo', 30, '$150,000'],
['Frankie Long', 'Industrial Analyst', 'Austin', 31, '$170,000'],
['Brynn Robbins', 'Business Analyst', 'Norfolk', 22, '$90,000'],
['Justice Mann', 'Business Consultant', 'Chicago', 24, '$133,000'],
['Addison Navarro', 'Business Management Analyst', 'New York', 50, '$295,000'],
['Jesse Welch', 'Agency Legal Counsel', 'Seattle', 28, '$200,000'],
['Eli Mejia', 'Commercial Specialist', 'Long Beach', 65, '$400,000'],
['Gene Leblanc', 'Industrial Analyst', 'Hartford', 34, '$110,000'],
['Danny Leon', 'Computer Scientist', 'Newark', 60, '$220,000'],
['Lane Lee', 'Corporate Counselor', 'Cincinnati', 52, '$180,000'],
['Jesse Hall', 'Business Analyst', 'Baltimore', 44, '$99,000'],
['Danni Hudson', 'Agency Legal Counsel', 'Tampa', 37, '$90,000'],
['Terry Macdonald', 'Commercial Specialist', 'Miami', 39, '$140,000'],
['Justice Mccarthy', 'Attorney', 'Tucson', 26, '$330,000'],
['Silver Carey', 'Computer Scientist', 'Memphis', 47, '$250,000'],
['Franky Miles', 'Industrial Analyst', 'Buffalo', 49, '$190,000'],
['Glen Nixon', 'Corporate Counselor', 'Arlington', 44, '$80,000'],
['Gabby Strickland', 'Business Process Consultant', 'Scottsdale', 26, '$45,000'],
['Mason Ray', 'Computer Scientist', 'San Francisco', 39, '$142,000'],
];

const options = {
filter: true,
filterType: 'dropdown',
responsive: 'stacked',
page: 1,
searchPlaceholder: 'This is a search placeholder',
};

return (
<Fragment>
<MUIDataTable title={'ACME Employee list'} data={data} columns={columns} options={options} />
</Fragment>
);
}
}

ReactDOM.render(<Example />, document.getElementById('app-root'));
1 change: 1 addition & 0 deletions src/MUIDataTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ class MUIDataTable extends React.Component {
customSearch: PropTypes.func,
search: PropTypes.bool,
searchText: PropTypes.string,
searchPlaceholder: PropTypes.string,
print: PropTypes.bool,
viewColumns: PropTypes.bool,
download: PropTypes.bool,
Expand Down
1 change: 1 addition & 0 deletions src/components/TableSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class TableSearch extends React.Component {
onChange={this.handleTextChange}
fullWidth={true}
inputRef={el => (this.searchField = el)}
placeholder={options.searchPlaceholder}
/>
<IconButton className={classes.clearIcon} onClick={onHide}>
<ClearIcon />
Expand Down
13 changes: 13 additions & 0 deletions test/MUIDataTableSearch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ describe('<TableSearch />', function() {
assert.strictEqual(actualResult.find(TextField).props().value, 'nextText');
});

it('should render a search bar with placeholder when searchPlaceholder is set', () => {
const options = { textLabels, searchPlaceholder: 'TestingPlaceholder' };
const onSearch = () => {};
const onHide = () => {};

const mountWrapper = mount(
<TableSearch onSearch={onSearch} onHide={onHide} options={options} />,
);
const actualResult = mountWrapper.find(TextField);
assert.strictEqual(actualResult.length, 1);
assert.strictEqual(actualResult.props().placeholder, 'TestingPlaceholder');
});

it('should trigger handleTextChange prop callback when calling method handleTextChange', () => {
const options = { onSearchChange: () => true, textLabels };
const onSearch = spy();
Expand Down
2 changes: 1 addition & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const webpack = require('webpack');

module.exports = {
entry: {
app: "./examples/customize-filter/index.js"
app: "./examples/customize-search-placeholder/index.js"
},
stats: "verbose",
context: __dirname,
Expand Down

0 comments on commit f6aa0bd

Please sign in to comment.