This commit is contained in:
Allen 2018-08-11 15:03:17 +08:00 committed by GitHub
parent 586acaed68
commit 1f51f1a08d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 61 additions and 5 deletions

View File

@ -120,4 +120,7 @@ Custom the csv file separator.
Default is `false`. Give true to avoid to attach the csv header.
#### noAutoBOM - [bool]
Default is `true`.
Default is `true`.
#### exportAll - [bool]
Default is `true`. `false` will only export current data which display on table.

View File

@ -69,7 +69,8 @@ class ToolkitProvider extends statelessDrcorator(React.Component) {
columns: this.props.columns,
data: this.props.data,
bootstrap4: this.props.bootstrap4,
setDependencyModules: this.setDependencyModules
setDependencyModules: this.setDependencyModules,
registerExposedAPI: this.registerExposedAPI
};
if (this.props.search) {
baseProps.search = {

View File

@ -4,13 +4,14 @@ const csvDefaultOptions = {
fileName: 'spreadsheet.csv',
separator: ',',
ignoreHeader: false,
noAutoBOM: true
noAutoBOM: true,
exportAll: true
};
export default Base =>
class CSVOperation extends Base {
handleExportCSV = () => {
const { columns, data, exportCSV } = this.props;
const { columns, exportCSV } = this.props;
const meta = getMetaInfo(columns);
const options = exportCSV === true ?
csvDefaultOptions :
@ -18,6 +19,8 @@ export default Base =>
...csvDefaultOptions,
...exportCSV
};
const data = options.exportAll ? this.props.data : this.getData();
const content = transform(data, meta, this._.get, options);
save(content, options);
}

View File

@ -1,4 +1,10 @@
import Operation from './src/op';
export default Base =>
class StatelessOperation extends Operation.csvOperation(Base) {};
class StatelessOperation extends Operation.csvOperation(Base) {
registerExposedAPI = (...exposedFuncs) => {
exposedFuncs.forEach((func) => {
this[func.name] = func;
});
}
};

View File

@ -15,6 +15,15 @@ class BootstrapTable extends PropsBaseResolver(Component) {
constructor(props) {
super(props);
this.validateProps();
if (props.registerExposedAPI) {
const getData = () => this.getData();
props.registerExposedAPI(getData);
}
}
// Exposed APIs
getData = () => {
return this.props.data;
}
render() {

View File

@ -46,6 +46,40 @@ describe('BootstrapTable', () => {
});
});
describe('getData', () => {
let instance;
beforeEach(() => {
wrapper = shallow(
<BootstrapTable keyField="id" columns={ columns } data={ data } />);
instance = wrapper.instance();
});
it('should return props.data', () => {
expect(instance.getData()).toEqual(data);
});
});
describe('when props.registerExposedAPI is defined', () => {
const registerExposedAPI = jest.fn();
beforeEach(() => {
registerExposedAPI.mockClear();
wrapper = shallow(
<BootstrapTable
keyField="id"
columns={ columns }
data={ data }
registerExposedAPI={ registerExposedAPI }
/>
);
});
it('should call props.registerExposedAPI correctly', () => {
expect(registerExposedAPI).toHaveBeenCalledTimes(1);
expect(registerExposedAPI.mock.calls[0][0].name).toEqual('getData');
});
});
describe('when props.classes was defined', () => {
const classes = 'foo';