Skip to content
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

Added field whitelisting #79

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ The `rows` prop expects an array of objects, such as the following.
### Excluding fields

By default, React Dynamic Data Table will render a table containing all fields present
in the `rows` prop. To exclude specific fields, you can use the `fieldsToExclude` props.
in the `rows` prop.

#### Blacklisting

To exclude specific fields, you can use the `fieldsToExclude` prop.

In the example below, the `email` field will be excluded.

Expand All @@ -68,6 +72,23 @@ In the example below, all ID fields will be excluded.

The `fieldsToExclude` prop expects an array of strings or regex expressions that represent the fields to exclude.

#### Whitelisting

To only allow specific fields, you can use the `fieldstoInclude` prop.

In the example below, the `email` field will be allowed, all other fields will be removed.

```JSX
<DynamicDataTable
rows={this.state.users}
fieldsToInclude={['email']}
/>
```

The `fieldsToInclude` prop expects an array of strings. It does **not** support regex expressions.

**Note:** Using both a black and white list will result in all fields not appearing on the whitelist being removed, and any fields remaining that are also on the blacklist, also being removed.

### Mapping fields

By default, React Dynamic Data Table creates table headers based on the field name,
Expand Down
60 changes: 42 additions & 18 deletions dist/DynamicDataTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ require("core-js/modules/es6.array.is-array");

require("core-js/modules/es6.array.find-index");

require("core-js/modules/es6.array.index-of");

require("core-js/modules/es6.array.filter");

require("core-js/modules/es6.array.index-of");

require("core-js/modules/es6.regexp.constructor");

require("core-js/modules/es6.regexp.replace");
Expand Down Expand Up @@ -135,11 +135,16 @@ function (_Component) {
value: function getFields() {
var rows = this.props.rows;
var _this$props2 = this.props,
fieldsToInclude = _this$props2.fieldsToInclude,
fieldsToExclude = _this$props2.fieldsToExclude,
fieldMap = _this$props2.fieldMap,
fieldOrder = _this$props2.fieldOrder;
var fields = [];

if (!fieldsToInclude) {
fieldsToInclude = [];
}

if (!fieldsToExclude) {
fieldsToExclude = [];
}
Expand Down Expand Up @@ -181,42 +186,59 @@ function (_Component) {
}
}

if (fieldsToInclude.length > 0) {
for (var _i = 0; _i < fields.length; _i++) {
var _field = fields[_i];
var shouldExclude = false;

if (fieldsToInclude.indexOf(_field.name) === -1) {
shouldExclude = true;
}

if (shouldExclude) {
fields.splice(_i, 1);
_i--;
continue;
}
}
}

var regExpsToExclude = fieldsToExclude.filter(function (field) {
return field.constructor && field.constructor === RegExp;
});

for (var _i = 0; _i < fields.length; _i++) {
var _field = fields[_i];
var shouldExclude = false; // Field exclusion
for (var _i2 = 0; _i2 < fields.length; _i2++) {
var _field2 = fields[_i2];
var _shouldExclude = false; // Field exclusion

if (fieldsToExclude.indexOf(_field.name) !== -1) {
shouldExclude = true;
if (fieldsToExclude.indexOf(_field2.name) !== -1) {
_shouldExclude = true;
} else {
for (var _j = 0; _j < regExpsToExclude.length; _j++) {
if (regExpsToExclude[_j].test(_field.name)) {
shouldExclude = true;
if (regExpsToExclude[_j].test(_field2.name)) {
_shouldExclude = true;
break;
}
}
}

if (shouldExclude) {
fields.splice(_i, 1);
_i--;
if (_shouldExclude) {
fields.splice(_i2, 1);
_i2--;
continue;
} // Field mapping


if (fieldMap.hasOwnProperty(_field.name)) {
fields[_i].label = fieldMap[_field.name];
if (fieldMap.hasOwnProperty(_field2.name)) {
fields[_i2].label = fieldMap[_field2.name];
}
}

if (fieldOrder.length) {
var orderedFields = Array(fieldOrder.length);

var _loop = function _loop(_i2) {
var field = fields[_i2];
var _loop = function _loop(_i3) {
var field = fields[_i3];
var j = fieldOrder.findIndex(function (query) {
if (query.constructor) {
switch (query.constructor) {
Expand Down Expand Up @@ -248,8 +270,8 @@ function (_Component) {
orderedFields.push(field);
};

for (var _i2 = 0; _i2 < fields.length; _i2++) {
var _ret = _loop(_i2);
for (var _i3 = 0; _i3 < fields.length; _i3++) {
var _ret = _loop(_i3);

if (_ret === "continue") continue;
}
Expand Down Expand Up @@ -658,6 +680,7 @@ function (_Component) {

DynamicDataTable.propTypes = {
rows: _propTypes["default"].array,
fieldsToInclude: _propTypes["default"].array,
fieldsToExclude: _propTypes["default"].array,
fieldMap: _propTypes["default"].object,
fieldOrder: _propTypes["default"].array,
Expand Down Expand Up @@ -698,6 +721,7 @@ DynamicDataTable.propTypes = {
};
DynamicDataTable.defaultProps = {
rows: [],
fieldsToInclude: [],
fieldsToExclude: [],
fieldMap: {},
fieldOrder: [],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@langleyfoxall/react-dynamic-data-table",
"version": "7.7.0",
"version": "7.8.0",
"description": "Re-usable data table for React with sortable columns, pagination and more.",
"keywords": [
"react",
Expand Down
24 changes: 23 additions & 1 deletion src/DynamicDataTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,14 @@ class DynamicDataTable extends Component {

getFields() {
const { rows } = this.props;
let { fieldsToExclude, fieldMap, fieldOrder } = this.props;
let { fieldsToInclude, fieldsToExclude, fieldMap, fieldOrder } = this.props;

const fields = [];

if (!fieldsToInclude) {
fieldsToInclude = [];
}

if (!fieldsToExclude) {
fieldsToExclude = [];
}
Expand Down Expand Up @@ -119,6 +123,22 @@ class DynamicDataTable extends Component {
}
}

if (fieldsToInclude.length > 0) {
for (let i = 0; i < fields.length; i++) {
const field = fields[i];
let shouldExclude = false;
if (fieldsToInclude.indexOf(field.name) === -1) {
shouldExclude = true;
}

if (shouldExclude) {
fields.splice(i, 1);
i--;
continue;
}
}
}

const regExpsToExclude = fieldsToExclude.filter(field => field.constructor && field.constructor === RegExp);

for (let i = 0; i < fields.length; i++) {
Expand Down Expand Up @@ -548,6 +568,7 @@ class DynamicDataTable extends Component {

DynamicDataTable.propTypes = {
rows: PropTypes.array,
fieldsToInclude: PropTypes.array,
fieldsToExclude: PropTypes.array,
fieldMap: PropTypes.object,
fieldOrder: PropTypes.array,
Expand Down Expand Up @@ -591,6 +612,7 @@ DynamicDataTable.propTypes = {

DynamicDataTable.defaultProps = {
rows: [],
fieldsToInclude: [],
fieldsToExclude: [],
fieldMap: {},
fieldOrder: [],
Expand Down