mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-28 10:50:40 +01:00
fix(Table): v-model causing first column missing (#2890)
This commit is contained in:
@@ -18,7 +18,7 @@
|
|||||||
:class="[ui.th.base, ui.th.padding, ui.th.color, ui.th.font, ui.th.size, column.key === 'select' && ui.checkbox.padding, column.class]"
|
:class="[ui.th.base, ui.th.padding, ui.th.color, ui.th.font, ui.th.size, column.key === 'select' && ui.checkbox.padding, column.class]"
|
||||||
:aria-sort="getAriaSort(column)"
|
:aria-sort="getAriaSort(column)"
|
||||||
>
|
>
|
||||||
<slot v-if="!singleSelect && modelValue && (column.key === 'select' || shouldRenderColumnInFirstPlace(index, 'select'))" name="select-header" :indeterminate="indeterminate" :checked="isAllRowChecked" :change="onChange">
|
<slot v-if="!singleSelect && modelValue && column.key === 'select'" name="select-header" :indeterminate="indeterminate" :checked="isAllRowChecked" :change="onChange">
|
||||||
<UCheckbox
|
<UCheckbox
|
||||||
:model-value="isAllRowChecked"
|
:model-value="isAllRowChecked"
|
||||||
:indeterminate="indeterminate"
|
:indeterminate="indeterminate"
|
||||||
@@ -93,7 +93,7 @@
|
|||||||
/>
|
/>
|
||||||
</td>
|
</td>
|
||||||
<td v-for="(column, subIndex) in columns" :key="subIndex" :class="[ui.td.base, ui.td.padding, ui.td.color, ui.td.font, ui.td.size, column?.rowClass, row[column.key]?.class, column.key === 'select' && ui.checkbox.padding]">
|
<td v-for="(column, subIndex) in columns" :key="subIndex" :class="[ui.td.base, ui.td.padding, ui.td.color, ui.td.font, ui.td.size, column?.rowClass, row[column.key]?.class, column.key === 'select' && ui.checkbox.padding]">
|
||||||
<slot v-if="modelValue && (column.key === 'select' || shouldRenderColumnInFirstPlace(subIndex, 'select')) " name="select-data" :checked="isSelected(row)" :change="(ev: boolean) => onChangeCheckbox(ev, row)">
|
<slot v-if="modelValue && column.key === 'select' " name="select-data" :checked="isSelected(row)" :change="(ev: boolean) => onChangeCheckbox(ev, row)">
|
||||||
<UCheckbox
|
<UCheckbox
|
||||||
:model-value="isSelected(row)"
|
:model-value="isSelected(row)"
|
||||||
v-bind="ui.default.checkbox"
|
v-bind="ui.default.checkbox"
|
||||||
@@ -274,7 +274,30 @@ export default defineComponent({
|
|||||||
setup(props, { emit, attrs: $attrs }) {
|
setup(props, { emit, attrs: $attrs }) {
|
||||||
const { ui, attrs } = useUI('table', toRef(props, 'ui'), config, toRef(props, 'class'))
|
const { ui, attrs } = useUI('table', toRef(props, 'ui'), config, toRef(props, 'class'))
|
||||||
|
|
||||||
const columns = computed(() => props.columns ?? Object.keys(props.rows[0] ?? {}).map(key => ({ key, label: upperFirst(key), sortable: false, class: undefined, sort: defaultSort }) as TableColumn))
|
const columns = computed(() => {
|
||||||
|
const defaultColumns = props.columns ?? (
|
||||||
|
Object.keys(props.rows[0]).map(key => ({
|
||||||
|
key,
|
||||||
|
label: upperFirst(key),
|
||||||
|
sortable: false,
|
||||||
|
class: undefined,
|
||||||
|
sort: defaultSort
|
||||||
|
}))
|
||||||
|
) as TableColumn[]
|
||||||
|
|
||||||
|
const hasColumnSelect = defaultColumns.find(v => v.key === 'select')
|
||||||
|
|
||||||
|
if (hasColumnSelect || !props.modelValue) {
|
||||||
|
return defaultColumns
|
||||||
|
}
|
||||||
|
|
||||||
|
return [{
|
||||||
|
key: 'select',
|
||||||
|
sortable: false,
|
||||||
|
class: undefined,
|
||||||
|
sort: defaultSort
|
||||||
|
}, ...defaultColumns]
|
||||||
|
})
|
||||||
|
|
||||||
const sort = useVModel(props, 'sort', emit, { passive: true, defaultValue: defu({}, props.sort, { column: null, direction: 'asc' }) })
|
const sort = useVModel(props, 'sort', emit, { passive: true, defaultValue: defu({}, props.sort, { column: null, direction: 'asc' }) })
|
||||||
const expand = useVModel(props, 'expand', emit, {
|
const expand = useVModel(props, 'expand', emit, {
|
||||||
@@ -435,13 +458,6 @@ export default defineComponent({
|
|||||||
return expand.value?.openedRows ? expand.value.openedRows.some(openedRow => compare(openedRow, row)) : false
|
return expand.value?.openedRows ? expand.value.openedRows.some(openedRow => compare(openedRow, row)) : false
|
||||||
}
|
}
|
||||||
|
|
||||||
function shouldRenderColumnInFirstPlace(index: number, key: string) {
|
|
||||||
if (!props.columns) {
|
|
||||||
return index === 0
|
|
||||||
}
|
|
||||||
return index === 0 && !props.columns.find(col => col.key === key)
|
|
||||||
}
|
|
||||||
|
|
||||||
function toggleOpened(row: TableRow) {
|
function toggleOpened(row: TableRow) {
|
||||||
expand.value = {
|
expand.value = {
|
||||||
openedRows: isExpanded(row) ? expand.value.openedRows.filter(v => !compare(v, row)) : props.multipleExpand ? [...expand.value.openedRows, row] : [row],
|
openedRows: isExpanded(row) ? expand.value.openedRows.filter(v => !compare(v, row)) : props.multipleExpand ? [...expand.value.openedRows, row] : [row],
|
||||||
@@ -502,7 +518,6 @@ export default defineComponent({
|
|||||||
toggleOpened,
|
toggleOpened,
|
||||||
getAriaSort,
|
getAriaSort,
|
||||||
isExpanded,
|
isExpanded,
|
||||||
shouldRenderColumnInFirstPlace,
|
|
||||||
retriggerSlot
|
retriggerSlot
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user