8000 fix(b-table): make sure to apply all formatters of field configuration (closes #5672) by jacobmllr95 · Pull Request #5674 · bootstrap-vue/bootstrap-vue · GitHub
[go: up one dir, main page]

Skip to content

fix(b-table): make sure to apply all formatters of field configuration (closes #5672) #5674

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

Merged
merged 4 commits into from
Aug 20, 2020
Merged
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
12 changes: 6 additions & 6 deletions .bundlewatch.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,27 @@
},
{
"path": "./dist/bootstrap-vue.js",
"maxSize": "230 kB"
"maxSize": "235 kB"
},
{
"path": "./dist/bootstrap-vue.min.js",
"maxSize": "100 kB"
"maxSize": "105 kB"
},
{
"path": "./dist/bootstrap-vue.common.js",
"maxSize": "305 kB"
"maxSize": "310 kB"
},
{
"path": "./dist/bootstrap-vue.common.min.js",
"maxSize": "190 kB"
"maxSize": "195 kB"
},
{
"path": "./dist/bootstrap-vue.esm.js",
"maxSize": "300 kB"
"maxSize": "310 kB"
},
{
"path": "./dist/bootstrap-vue.esm.min.js",
"maxSize": "190 kB"
"maxSize": "195 kB"
},
{
"path": "./dist/bootstrap-vue.css",
Expand Down
59 changes: 34 additions & 25 deletions src/components/table/helpers/sanitize-row.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,41 @@
import { keys } from '../../../utils/object'
import { arrayIncludes } from '../../../utils/array'
import { arrayIncludes, isArray } from '../../../utils/array'
import { isFunction } from '../../../utils/inspect'
import { clone, keys, pick } from '../../../utils/object'
import { IGNORED_FIELD_KEYS } from './constants'

// Return a copy of a row after all reserved fields have been filtered out
const sanitizeRow = (row, ignoreFields, includeFields, fieldsObj = {}) =>
keys(row).reduce((obj, key) => {
// Ignore special fields that start with `_`
// Ignore fields in the `ignoreFields` array
// Include only fields in the `includeFields` array
if (
!IGNORED_FIELD_KEYS[key] &&
!(ignoreFields && ignoreFields.length > 0 && arrayIncludes(ignoreFields, key)) &&
!(includeFields && includeFields.length > 0 && !arrayIncludes(includeFields, key))
) {
const f = fieldsObj[key] || {}
const val = row[key]
// `f.filterByFormatted` will either be a function or boolean
// `f.formater` will have already been noramlized into a function ref
const filterByFormatted = f.filterByFormatted
const formatter = isFunction(filterByFormatted)
? /* istanbul ignore next */ filterByFormatted
: filterByFormatted
? /* istanbul ignore next */ f.formatter
: null
obj[key] = isFunction(formatter) ? formatter(val, key, row) : val
const sanitizeRow = (row, ignoreFields, includeFields, fieldsObj = {}) => {
// We first need to format the row based on the field configurations
// This ensures that we add formatted values for keys that may not
// exist in the row itself
const formattedRow = keys(fieldsObj).reduce((result, key) => {
const field = fieldsObj[key]
const { filterByFormatted } = field
const formatter = isFunction(filterByFormatted)
? /* istanbul ignore next */ filterByFormatted
: filterByFormatted
? /* istanbul ignore next */ field.formatter
: null

if (isFunction(formatter)) {
result[key] = formatter(row[key], key, row)
}
return obj
}, {})

return result
}, clone(row))

// Determine the allowed keys:
// - Ignore special fields that start with `_`
// - Ignore fields in the `ignoreFields` array
// - Include only fields in the `includeFields` array
const allowedKeys = keys(formattedRow).filter(
key =>
!IGNORED_FIELD_KEYS[key] &&
!(isArray(ignoreFields) && arrayIncludes(ignoreFields, key)) &&
!(isArray(includeFields) && !arrayIncludes(includeFields, key))
)

return pick(formattedRow, allowedKeys)
}

export default sanitizeRow
28 changes: 28 additions & 0 deletions src/components/table/table-filtering.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,34 @@ describe('table > filtering', () => {
wrapper.destroy()
})

it('should filter for formatted values for keys which are not present in row', async () => {
const wrapper = mount(BTable, {
propsData: {
items: [{ a: 'A', b: 'B' }],
fields: [
{ key: 'a' },
{
key: 'b',
formatter: () => 'Foo',
filterByFormatted: true
},
{
key: 'c',
formatter: () => 'Bar',
filterByFormatted: true
}
],
filter: 'Bar'
}
})
expect(wrapper).toBeDefined()
await waitNT(wrapper.vm)

expect(wrapper.findAll('tbody > tr').length).toBe(1)

wrapper.destroy()
})

it('should show empty filtered message when no matches and show-empty=true', async () => {
const wrapper = mount(BTable, {
propsData: {
Expand Down
0