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

feat(next/antd): support dataSource disabled props in SelectTable #2902

Merged
merged 18 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
9afb77b
feat(next/antd): support disabled props from field in ArrayBase.Addition
ifblooms Jan 10, 2022
4a7de19
Merge branch 'alibaba:formily_next' into formily_next
ifblooms Jan 14, 2022
b700bf9
fix(antd/next): fix Cascader readPretty bug
ifblooms Jan 14, 2022
e6e6bf9
Merge branch 'alibaba:formily_next' into formily_next
ifblooms Feb 11, 2022
e497703
fix(antd/next): fix SelectTable rowSelection bug
ifblooms Feb 11, 2022
5a5cace
Merge branch 'alibaba:formily_next' into formily_next
ifblooms Feb 15, 2022
51c5abc
feat(next): support checkStrictly props in SelectTable
ifblooms Feb 15, 2022
9d45c03
fix(antd/next): fix FormItem useOverflow bug
ifblooms Feb 16, 2022
a4065f7
Merge branch 'alibaba:formily_next' into formily_next
ifblooms Feb 16, 2022
30cc023
Merge branch 'alibaba:formily_next' into formily_next
ifblooms Feb 21, 2022
32646be
feat(next/antd): add options param in SelectTable onChange props
ifblooms Feb 21, 2022
1ffee63
Merge branch 'alibaba:formily_next' into formily_next
ifblooms Feb 23, 2022
78d2add
fix(next/antd): compatible noData for SelectTable
ifblooms Feb 23, 2022
2265cb1
feat(next/antd): add valueType props in SelectTable
ifblooms Feb 25, 2022
d0b2d89
Merge branch 'alibaba:formily_next' into formily_next
ifblooms Feb 25, 2022
8266f21
Merge branch 'alibaba:formily_next' into formily_next
ifblooms Mar 2, 2022
a23dd1a
feat(next/antd): support dataSource disabled props in SelectTable
ifblooms Mar 3, 2022
6c69e49
Merge branch 'alibaba:formily_next' into formily_next
ifblooms Mar 3, 2022
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
20 changes: 18 additions & 2 deletions packages/antd/src/select-table/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { SearchProps } from 'antd/lib/input'
import { useFilterOptions } from './useFilterOptions'
import { useFlatOptions } from './useFlatOptions'
import { useSize } from './useSize'
import { useCheckSlackly } from './useCheckSlackly'
import { getUISelected, getOutputData } from './utils'
import { usePrefixCls } from '../__builtins__'

Expand Down Expand Up @@ -176,7 +177,7 @@ export const SelectTable: ComposedSelectTable = observer((props) => {
}

const onRowClick = (record) => {
if (disabled || readOnly) {
if (disabled || readOnly || record?.disabled) {
return
}
const selectedRowKey = record?.[primaryKey]
Expand All @@ -196,6 +197,21 @@ export const SelectTable: ComposedSelectTable = observer((props) => {
selectedRowKeys.includes(item?.[primaryKey])
)
}
if (rowSelection?.checkStrictly !== false) {
onInnerChange(selectedRowKeys, records)
} else {
onSlacklyChange(selectedRowKeys)
}
}

// Antd TreeData SlacklyChange for onRowClick
const onSlacklyChange = (currentSelected: any[]) => {
let { selectedRowKeys, records } = useCheckSlackly(
currentSelected,
selected,
primaryKey,
flatDataSource
)
onInnerChange(selectedRowKeys, records)
}

Expand Down Expand Up @@ -230,7 +246,7 @@ export const SelectTable: ComposedSelectTable = observer((props) => {
...rowSelection,
getCheckboxProps: (record) => ({
...(rowSelection?.getCheckboxProps?.(record) as any),
disabled,
disabled: disabled || record?.disabled,
}), // antd
selectedRowKeys: selected,
onChange: onInnerChange,
Expand Down
68 changes: 68 additions & 0 deletions packages/antd/src/select-table/useCheckSlackly.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { getTreeKeys, hasSelectedKey, completedKeys } from './utils'

/**
* 判断该字段的 indeterminate 属性
* @param record 当前字段
* @param selected 已选中的字段值集合
* @param primaryKey 键名称
* @returns indeterminate 属性值
*/
const getIndeterminate = (record: any, selected: any[], primaryKey: string) => {
if (selected?.includes(record[primaryKey])) {
return undefined
}
return hasSelectedKey(record.children, selected, primaryKey) || undefined
}

interface ICheckSlackly {
(
currentSelected: any[],
allSelected: any[],
primaryKey: string,
flatDataSource: any[]
): {
selectedRowKeys: any[]
records: any[]
}
}

const useCheckSlackly: ICheckSlackly = (
currentSelected, // onChange 返回的 keys
allSelected, // Table UI 展示的 keys
primaryKey,
flatDataSource
) => {
const isSelected = currentSelected.length > allSelected.length // 判断是选中还是取消
const currentKey = [...currentSelected, ...allSelected].find(
(key) => !(currentSelected.includes(key) && allSelected.includes(key)) // 当前变化key不同时存在于两个selected
)
const currentRecords = flatDataSource.find(
(item) => item[primaryKey] === currentKey
)
const currentTreeKeys = getTreeKeys([currentRecords], primaryKey)
let newSelectedRowKeys = []
if (isSelected) {
// 选中当前key及其子keys
newSelectedRowKeys = [...new Set([...allSelected, ...currentTreeKeys])]
} else {
// 移除当前key及其子keys
newSelectedRowKeys = allSelected.filter(
(key) => !currentTreeKeys.includes(key)
)
}

newSelectedRowKeys = completedKeys(
flatDataSource,
newSelectedRowKeys,
primaryKey
)

return {
selectedRowKeys: newSelectedRowKeys,
records: flatDataSource.filter((item) =>
newSelectedRowKeys.includes(item[primaryKey])
),
}
}

export { useCheckSlackly, getIndeterminate }
25 changes: 15 additions & 10 deletions packages/antd/src/select-table/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import { useFlatOptions } from './useFlatOptions'
*/
const getTreeKeys = (tree: any[], primaryKey: string) =>
isArr(tree)
? tree.reduce(
(prev, current) => [
? tree.reduce((prev, current) => {
if (current?.disabled) {
return prev
}
return [
...prev,
current[primaryKey],
...getTreeKeys(current?.children, primaryKey),
],
[]
)
]
}, [])
: []

/**
Expand All @@ -41,10 +43,11 @@ const hasSelectedKey = (tree: any[], selected: any[], primaryKey: string) => {
* @returns 是否全部被选中
*/
const isAllSelected = (list: any[], selected: any[], primaryKey: string) => {
const selectedList = list.filter((item) =>
const validList = list.filter((item) => !item?.disabled)
const selectedList = validList.filter((item) =>
selected?.includes(item[primaryKey])
)
return selectedList.length === list.length
return selectedList.length === validList.length
}

/**
Expand All @@ -69,8 +72,10 @@ const completedKeys = (
primaryKey
)
if (isAllSelected(item.children, allSelectedKeys, primaryKey)) {
// 如果该元素的子元素全部选中,则也选中该项(即包含全选子元素的父元素)
allSelectedKeys = [...new Set([...allSelectedKeys, item[primaryKey]])]
// 如果该元素的子元素全部选中,且该元素未禁用,则也选中该项(即包含全选子元素的父元素)
if (!item?.disabled) {
allSelectedKeys = [...new Set([...allSelectedKeys, item[primaryKey]])]
}
} else {
// 如果该元素的子元素未全部选中,则移除该项
allSelectedKeys = allSelectedKeys.filter(
Expand Down Expand Up @@ -175,7 +180,7 @@ const getOutputData = (
})
} else if (valueType === 'path') {
outputValue = getSelectedPath(dataSource, keys, primaryKey)
outputOptions = options
outputOptions = [...options]
} else {
// valueType === 'all'
outputValue = [...keys]
Expand Down
5 changes: 2 additions & 3 deletions packages/next/src/select-table/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export const SelectTable: ComposedSelectTable = observer((props) => {
}

const onRowClick = (record) => {
if (disabled || readOnly) {
if (disabled || readOnly || record?.disabled) {
return
}
const selectedRowKey = record?.[primaryKey]
Expand All @@ -203,7 +203,6 @@ export const SelectTable: ComposedSelectTable = observer((props) => {
if (rowSelection?.checkStrictly !== false) {
onInnerChange(selectedRowKeys, records)
} else {
// fusion
onSlacklyChange(selectedRowKeys)
}
}
Expand Down Expand Up @@ -259,7 +258,7 @@ export const SelectTable: ComposedSelectTable = observer((props) => {
getProps: (record, index) => ({
...(rowSelection?.getProps?.(record, index) as any),
indeterminate: getIndeterminate(record, selected, primaryKey), // 父子关联模式indeterminate值
disabled,
disabled: disabled || record?.disabled,
}), // fusion
selectedRowKeys: selected,
onChange:
Expand Down
5 changes: 3 additions & 2 deletions packages/next/src/select-table/useTitleAddon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { Checkbox } from '@alifd/next'
const newCheckbox =
(selected, flatDataSource, primaryKey, disabled, readOnly, onChange) =>
() => {
const allDataSourceKeys = flatDataSource.map((item) => item?.[primaryKey])
const allDataSource = flatDataSource.filter((item) => !item.disabled)
const allDataSourceKeys = allDataSource.map((item) => item?.[primaryKey])
const indeterminate = !!(
selected?.length && selected.length !== allDataSourceKeys.length
)
Expand All @@ -18,7 +19,7 @@ const newCheckbox =
onChange={(checked) => {
if (!readOnly) {
if (checked || indeterminate) {
onChange?.(allDataSourceKeys, flatDataSource)
onChange?.(allDataSourceKeys, allDataSource)
} else {
onChange?.([], [])
}
Expand Down
25 changes: 15 additions & 10 deletions packages/next/src/select-table/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import { useFlatOptions } from './useFlatOptions'
*/
const getTreeKeys = (tree: any[], primaryKey: string) =>
isArr(tree)
? tree.reduce(
(prev, current) => [
? tree.reduce((prev, current) => {
if (current?.disabled) {
return prev
}
return [
...prev,
current[primaryKey],
...getTreeKeys(current?.children, primaryKey),
],
[]
)
]
}, [])
: []

/**
Expand All @@ -41,10 +43,11 @@ const hasSelectedKey = (tree: any[], selected: any[], primaryKey: string) => {
* @returns 是否全部被选中
*/
const isAllSelected = (list: any[], selected: any[], primaryKey: string) => {
const selectedList = list.filter((item) =>
const validList = list.filter((item) => !item?.disabled)
const selectedList = validList.filter((item) =>
selected?.includes(item[primaryKey])
)
return selectedList.length === list.length
return selectedList.length === validList.length
}

/**
Expand All @@ -69,8 +72,10 @@ const completedKeys = (
primaryKey
)
if (isAllSelected(item.children, allSelectedKeys, primaryKey)) {
// 如果该元素的子元素全部选中,则也选中该项(即包含全选子元素的父元素)
allSelectedKeys = [...new Set([...allSelectedKeys, item[primaryKey]])]
// 如果该元素的子元素全部选中,且该元素未禁用,则也选中该项(即包含全选子元素的父元素)
if (!item?.disabled) {
allSelectedKeys = [...new Set([...allSelectedKeys, item[primaryKey]])]
}
} else {
// 如果该元素的子元素未全部选中,则移除该项
allSelectedKeys = allSelectedKeys.filter(
Expand Down Expand Up @@ -175,7 +180,7 @@ const getOutputData = (
})
} else if (valueType === 'path') {
outputValue = getSelectedPath(dataSource, keys, primaryKey)
outputOptions = options
outputOptions = [...options]
} else {
// valueType === 'all'
outputValue = [...keys]
Expand Down