Skip to content

Commit

Permalink
feat(next/antd): add options param in SelectTable onChange props (#2842)
Browse files Browse the repository at this point in the history
  • Loading branch information
ifblooms authored Feb 21, 2022
1 parent 28a5853 commit 89d8026
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 34 deletions.
1 change: 0 additions & 1 deletion packages/antd/docs/components/SelectTable.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ export default () => {
x-component-props={{
bordered: false,
showSearch: true,
optionFilterProp: 'name',
optionAsValue: true,
}}
enum={[
Expand Down
1 change: 0 additions & 1 deletion packages/antd/docs/components/SelectTable.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ export default () => {
x-component-props={{
bordered: false,
showSearch: true,
optionFilterProp: 'name',
optionAsValue: true,
}}
enum={[
Expand Down
22 changes: 12 additions & 10 deletions packages/antd/src/select-table/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export interface ISelectTableProps extends TableProps<any> {
filterOption?: IFilterOption
filterSort?: IFilterSort
onSearch?: (keyword: string) => void
onChange?: (value: any) => void
onChange?: (value: any, options: any) => void
value?: any
}

Expand Down Expand Up @@ -146,15 +146,17 @@ export const SelectTable: ComposedSelectTable = observer((props) => {
if (readOnly) {
return
}
let outputValue = optionAsValue
? records.map((item) => {
const validItem = { ...item }
delete validItem['__formily_key__']
return validItem
})
: selectedRowKeys
outputValue = mode === 'single' ? outputValue[0] : outputValue
onChange?.(outputValue)
let outputOptions = records.map((item) => {
const validItem = { ...item }
delete validItem['__formily_key__']
return validItem
})
let outputValue = optionAsValue ? outputOptions : selectedRowKeys
if (mode === 'single') {
outputValue = outputValue[0]
outputOptions = outputOptions[0]
}
onChange?.(outputValue, outputOptions)
}

const onRowClick = (record) => {
Expand Down
1 change: 0 additions & 1 deletion packages/next/docs/components/SelectTable.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ export default () => {
x-component-props={{
hasBorder: false,
showSearch: true,
optionFilterProp: 'name',
optionAsValue: true,
}}
enum={[
Expand Down
1 change: 0 additions & 1 deletion packages/next/docs/components/SelectTable.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ export default () => {
x-component-props={{
hasBorder: false,
showSearch: true,
optionFilterProp: 'name',
optionAsValue: true,
}}
enum={[
Expand Down
25 changes: 14 additions & 11 deletions packages/next/src/select-table/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export interface ISelectTableColumnProps extends ColumnProps {
key: React.ReactText
}

export interface ISelectTableProps extends Omit<TableProps, 'primaryKey'> {
export interface ISelectTableProps
extends Omit<TableProps, 'primaryKey' | 'onChange'> {
mode?: 'multiple' | 'single'
dataSource?: any[]
optionAsValue?: boolean
Expand All @@ -30,7 +31,7 @@ export interface ISelectTableProps extends Omit<TableProps, 'primaryKey'> {
filterOption?: IFilterOption
filterSort?: IFilterSort
onSearch?: (keyword: string) => void
onChange?: (value: any) => void
onChange?: (value: any, options: any) => void
value?: any
rowSelection?: TableProps['rowSelection'] & {
checkStrictly?: boolean
Expand Down Expand Up @@ -149,15 +150,17 @@ export const SelectTable: ComposedSelectTable = observer((props) => {
if (readOnly) {
return
}
let outputValue = optionAsValue
? records.map((item) => {
const validItem = { ...item }
delete validItem['__formily_key__']
return validItem
})
: selectedRowKeys
outputValue = mode === 'single' ? outputValue[0] : outputValue
onChange?.(outputValue)
let outputOptions = records.map((item) => {
const validItem = { ...item }
delete validItem['__formily_key__']
return validItem
})
let outputValue = optionAsValue ? outputOptions : selectedRowKeys
if (mode === 'single') {
outputValue = outputValue[0]
outputOptions = outputOptions[0]
}
onChange?.(outputValue, outputOptions)
}

const onRowClick = (record) => {
Expand Down
22 changes: 13 additions & 9 deletions packages/next/src/select-table/useCheckSlackly.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { isArr } from '@formily/shared'

/**
* 获取该字段Checkbox的indeterminate属性
* @param record 字段项
Expand Down Expand Up @@ -29,15 +31,17 @@ const getCheckedProps = (record: any, primaryKey: string, selected: any[]) => {
* @param primaryKey 键名称
* @returns 键值数组集合
*/
const getTreeKeys = (tree: any[] = [], primaryKey: string) =>
tree.reduce(
(prev, current) => [
...prev,
current[primaryKey],
...getTreeKeys(current?.children, primaryKey),
],
[]
)
const getTreeKeys = (tree: any[], primaryKey: string) =>
isArr(tree)
? tree.reduce(
(prev, current) => [
...prev,
current[primaryKey],
...getTreeKeys(current?.children, primaryKey),
],
[]
)
: []

/**
* 获取最终选中值(添加选中所有子元素的父元素,或移除未选中所有子元素的父元素)
Expand Down

0 comments on commit 89d8026

Please sign in to comment.