Skip to content

Commit

Permalink
Merge pull request #97 from flowforge/feat-table-row-extra-props
Browse files Browse the repository at this point in the history
Add extraProps option to table rows
  • Loading branch information
hardillb authored Nov 14, 2022
2 parents 884cbbf + 81d15f8 commit fbd1ad9
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 2 deletions.
40 changes: 39 additions & 1 deletion docs/DesignLanguage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,39 @@
</div>
<div class="example">
<h5>Example 5: Custom Row Content &amp; Components</h5>
<p style="margin-bottom: 12px;">Note, this method requires components to be created that can parse and render the data structure provided to the row.</p>
<p style="margin-bottom: 12px;">
Note, this method requires components to be created that can parse and render the data structure provided to the row.
</p>
<p style="margin-bottom: 12px;">
The data passed to the sub-component can be manipulated using:
<ul style="margin: 12px 24px;list-style: disc;">
<li>
<pre style="display:inline;">map</pre> - maps row properties to other names
</li>
<li><pre style="display:inline;">extraProps</pre> - passes additional properties into the component</li>
</ul>
</p>
<p style="margin-bottom: 6px;">
<b>Map</b><br />
</p>
<pre style="margin-bottom: 6px;">"map": {
"count": "number",
"name": "user.name",
},</pre>
<p style="margin-bottom: 12px;">
Maps the row properties <code style="display:inline;">number</code> and <code style="display:inline;">user.name</code>
to <code style="display:inline;">number</code> and <code style="display:inline;">name</code> respectively.
</p>
<p style="margin-bottom: 6px;">
<b>extraProps</b><br />
</p>
<pre style="margin-bottom: 6px;">"extraProps": {
"total": this.totalRows,
"disabled": this.tableDisabled,
},</pre>
<p style="margin-bottom: 12px;">
Passes the properties <code style="display:inline;">totalRows</code> and <code style="display:inline;">tableDisabled</code> from wherever columns is defined into the component.
</p>
<p style="margin-bottom: 12px;">This method does still enable searching and sorting out of the box.</p>
<ff-data-table :columns="data.table4.columns" :rows="data.table4.rows"
:show-search="true" :search-fields="['sName', 'number']" search-placeholder="search-fields limits which properties the search applies to."></ff-data-table>
Expand Down Expand Up @@ -803,6 +835,9 @@ export default {
is: markRaw(FFNotificationPill),
map: {
count: 'number'
},
extraProps: {
color: 'red'
}
}
}],
Expand Down Expand Up @@ -837,6 +872,9 @@ export default {
is: 'markRaw(FFNotificationPill)',
map: {
count: 'number'
},
extraProps: {
color: 'red'
}
}
}]
Expand Down
2 changes: 1 addition & 1 deletion src/components/data-table/DataTableRow.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<slot>
<ff-data-table-cell v-for="(col, $column) in columns" :key="col.label" :class="col.class" :style="col.style" :highlight="highlightCell === $column">
<template v-if="col.component">
<component :is="col.component.is" v-bind="getCellData(data, col)"></component>
<component :is="col.component.is" v-bind="{...col.component.extraProps ?? {}, ...getCellData(data, col)}"></component>
</template>
<template v-else-if="!isBool(lookupProperty(data, col.key))">
{{ lookupProperty(data, col.key) }}
Expand Down
81 changes: 81 additions & 0 deletions tests/component/data-table/DataTableRow.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { mount } from '@vue/test-utils'
import DataTableRow from '@/components/data-table/DataTableRow.vue'

const TestComponent = {
template: '{{name}}: {{value}}',
props: ['name', 'value']
}

describe('DataTableRow', () => {
describe('subcomponents', () => {
it('render if set in the column', async () => {
const wrapper = mount(DataTableRow, {
props: {
columns: [{
component: {
is: TestComponent
}
}],
data: {
name: 'Film',
value: 'Star Wars'
}
}
})

expect(wrapper.text()).toMatch('Film: Star Wars')

await wrapper.setProps({
data: {
name: 'TV',
value: 'Black Mirror'
}
})

expect(wrapper.text()).toMatch('TV: Black Mirror')
})

it('support receiving extra properties', async () => {
const wrapper = mount(DataTableRow, {
props: {
columns: [{
component: {
is: TestComponent,
extraProps: {
value: 'Star wars'
}
}
}],
data: {
name: 'Film'
}
}
})

expect(wrapper.text()).toMatch('Film: Star Wars')

await wrapper.setProps({
columns: [{
component: {
is: TestComponent,
extraProps: {
value: 'Return of the Jedi'
}
}
}]
})

expect(wrapper.text()).toMatch('Film: Return of the Jedi')

// Props from data override extraProps
await wrapper.setProps({
data: {
name: 'TV',
value: 'Black Mirror'
}
})

expect(wrapper.text()).toMatch('TV: Black Mirror')
})
})
})

0 comments on commit fbd1ad9

Please sign in to comment.