-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(lib): separate mockTableColumn
- `mockTableColumn` is separated from `Table.vue` and defined in `src/components/table/mockTableColumn.js`. `mockTableColumn` is made as simple as it uses neither `ref` nor `computed` because I do not think reactivity is necessary. Maybe I am wrong but let's see.
- Loading branch information
Showing
2 changed files
with
111 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { h as createElement } from 'vue' | ||
import Emitter from 'tiny-emitter' | ||
|
||
import { getValueByPath, toCssWidth } from '../../utils/helpers' | ||
|
||
export default function mockTableColumn(table, column) { | ||
const eventEmitter = new Emitter() | ||
const defaultProps = { | ||
label: undefined, | ||
customKey: undefined, | ||
field: undefined, | ||
meta: undefined, | ||
width: undefined, | ||
numeric: undefined, | ||
centered: undefined, | ||
searchable: undefined, | ||
sortable: undefined, | ||
visible: true, | ||
subheading: undefined, | ||
customSort: undefined, | ||
customSearch: undefined, | ||
sticky: undefined, | ||
headerSelectable: undefined, | ||
headerClass: undefined, | ||
thAttrs: () => ({}), | ||
tdAttrs: () => ({}) | ||
} | ||
return { | ||
// props | ||
...defaultProps, | ||
...column, | ||
// data | ||
parent: table, | ||
newKey: column.customKey || column.label, | ||
_isTableColumn: true, | ||
// inject | ||
$table: table, | ||
// computed | ||
get thClasses() { | ||
const attrs = this.thAttrs(this) | ||
const classes = [this.headerClass, { | ||
'is-sortable': this.sortable, | ||
'is-sticky': this.sticky, | ||
'is-unselectable': this.isHeaderUnSelectable | ||
}] | ||
if (attrs && attrs.class) { | ||
classes.push(attrs.class) | ||
} | ||
return classes | ||
}, | ||
get thStyle() { | ||
const attrs = this.thAttrs(this) | ||
const style = [this.style] | ||
if (attrs && attrs.style) { | ||
style.push(attrs.style) | ||
} | ||
return style | ||
}, | ||
get rootClasses() { | ||
return [this.cellClass, { | ||
'has-text-right': this.numeric && !this.centered, | ||
'has-text-centered': this.centered, | ||
'is-sticky': this.sticky | ||
}] | ||
}, | ||
get style() { | ||
return { | ||
width: toCssWidth(this.width) | ||
} | ||
}, | ||
get hasDefaultSlot() { | ||
return !!this.$scopedSlots.default | ||
}, | ||
get isHeaderUnSelectable() { | ||
return !this.headerSelectable && this.sortable | ||
}, | ||
// methods | ||
getRootClasses(row) { | ||
const attrs = this.tdAttrs(row, this) | ||
const classes = [this.rootClasses] | ||
if (attrs && attrs.class) { | ||
classes.push(attrs.class) | ||
} | ||
return classes | ||
}, | ||
getRootStyle(row) { | ||
const attrs = this.tdAttrs(row, this) | ||
const style = [] | ||
if (attrs && attrs.style) { | ||
style.push(attrs.style) | ||
} | ||
return style | ||
}, | ||
$on: (...args) => eventEmitter.on(...args), | ||
$once: (...args) => eventEmitter.once(...args), | ||
$off: (...args) => eventEmitter.off(...args), | ||
$emit: (...args) => eventEmitter.emit(...args), | ||
// special fields | ||
_isVue: true, | ||
$scopedSlots: { | ||
default: (props) => { | ||
const vnode = createElement('span', { | ||
innerHTML: getValueByPath(props.row, column.field) | ||
}) | ||
return [vnode] | ||
} | ||
} | ||
} | ||
} |