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(element): support useRecord for ArrayBase #2313

Merged
merged 1 commit into from
Oct 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions packages/element/docs/guide/array-cards.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,7 @@
### ArrayCards.useIndex

> 读取当前渲染行索引的 Hook

### ArrayCards.useRecord

> 读取当前渲染记录的 Hook
4 changes: 4 additions & 0 deletions packages/element/docs/guide/array-collapse.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,7 @@
### ArrayCollapse.useIndex

> 读取当前渲染行索引的 Hook

### ArrayCollapse.useRecord

> 读取当前渲染记录的 Hook
4 changes: 4 additions & 0 deletions packages/element/docs/guide/array-items.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,7 @@
### ArrayItems.useIndex

> 读取当前渲染行索引的 Hook

### ArrayItems.useRecord

> 读取当前渲染记录的 Hook
4 changes: 4 additions & 0 deletions packages/element/docs/guide/array-table.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,7 @@
### ArrayTable.useIndex

> 读取当前渲染行索引的 Hook

### ArrayTable.useRecord

> 读取当前渲染记录的 Hook
17 changes: 12 additions & 5 deletions packages/element/src/array-base/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export type ArrayBaseMixins = {
Index?: typeof ArrayBaseIndex
useArray?: typeof useArray
useIndex?: typeof useIndex
useRecord?: typeof useRecord
}

export interface IArrayBaseProps {
Expand All @@ -44,6 +45,7 @@ export interface IArrayBaseProps {

export interface IArrayBaseItemProps {
index: number
record: any
}

export interface IArrayBaseContext {
Expand All @@ -58,17 +60,22 @@ export interface IArrayBaseContext {

const ArrayBaseSymbol: InjectionKey<IArrayBaseContext> =
Symbol('ArrayBaseContext')
const ItemSymbol: InjectionKey<Ref<number>> = Symbol('ItemContext')
const ItemSymbol: InjectionKey<IArrayBaseItemProps> = Symbol('ItemContext')

const useArray = () => {
return inject(ArrayBaseSymbol, null)
}

const useIndex = (index?: number) => {
const indexRef = inject(ItemSymbol)
const { index: indexRef } = toRefs(inject(ItemSymbol))
return indexRef ?? ref(index)
}

const useRecord = (record?: number) => {
const { record: recordRef } = toRefs(inject(ItemSymbol))
return recordRef ?? ref(record)
}

const isObjectValue = (schema: Schema) => {
if (Array.isArray(schema?.items)) return isObjectValue(schema.items[0])

Expand Down Expand Up @@ -155,10 +162,9 @@ const ArrayBaseInner = defineComponent<IArrayBaseProps>({

const ArrayBaseItem = defineComponent({
name: 'ArrayBaseItem',
props: ['index'],
props: ['index', 'record'],
setup(props: IArrayBaseItemProps, { slots }) {
const { index } = toRefs(props)
provide(ItemSymbol, index)
provide(ItemSymbol, props)
return () => {
return h(Fragment, {}, slots)
}
Expand Down Expand Up @@ -422,4 +428,5 @@ export const ArrayBase = composeExport(ArrayBaseInner, {
useArray: useArray,
useIndex: useIndex,
useKey: useKey,
useRecord: useRecord,
})
2 changes: 2 additions & 0 deletions packages/element/src/array-cards/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ const ArrayCardsInner = observer(
key: getKey(item, index),
props: {
index,
record: item,
},
},
{
Expand Down Expand Up @@ -241,6 +242,7 @@ export const ArrayCards = composeExport(ArrayCardsInner, {
MoveUp: ArrayBase.MoveUp,
useArray: ArrayBase.useArray,
useIndex: ArrayBase.useIndex,
useRecord: ArrayBase.useRecord,
})

export default ArrayCards
4 changes: 4 additions & 0 deletions packages/element/src/array-collapse/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export const ArrayCollapseInner = observer(
{
props: {
index,
record: item,
},
},
{
Expand Down Expand Up @@ -182,6 +183,7 @@ export const ArrayCollapseInner = observer(
{
props: {
index,
record: item,
},
},
{
Expand Down Expand Up @@ -237,6 +239,7 @@ export const ArrayCollapseInner = observer(
{
props: {
index,
record: item,
},
},
{
Expand Down Expand Up @@ -379,6 +382,7 @@ export const ArrayCollapse = composeExport(ArrayCollapseInner, {
MoveUp: ArrayBase.MoveUp,
useArray: ArrayBase.useArray,
useIndex: ArrayBase.useIndex,
useRecord: ArrayBase.useRecord,
})

export default ArrayCollapse
2 changes: 2 additions & 0 deletions packages/element/src/array-items/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const ArrayItemsInner = observer(
key,
props: {
index,
record: item,
},
},
{
Expand Down Expand Up @@ -175,6 +176,7 @@ export const ArrayItems = composeExport(ArrayItemsInner, {
MoveUp: ArrayBase.MoveUp,
useArray: ArrayBase.useArray,
useIndex: ArrayBase.useIndex,
useRecord: ArrayBase.useRecord,
})

export default ArrayItems
4 changes: 3 additions & 1 deletion packages/element/src/array-table/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ const getArrayTableSources = (
}

const parseArrayTable = (schema: Schema['items']) => {
if (!schema) return []
const sources: ObservableColumnSource[] = []
const items = isArr(schema) ? schema : ([schema] as Schema[])
return items.reduce((columns, schema) => {
Expand Down Expand Up @@ -172,7 +173,7 @@ const getArrayTableColumns = (

const children = h(
ArrayBase.Item,
{ props: { index }, key: `${key}${index}` },
{ props: { index, record: props.row }, key: `${key}${index}` },
{
default: () =>
h(
Expand Down Expand Up @@ -561,6 +562,7 @@ export const ArrayTable = composeExport(ArrayTableInner, {
MoveUp: ArrayBase.MoveUp,
useArray: ArrayBase.useArray,
useIndex: ArrayBase.useIndex,
useRecord: ArrayBase.useRecord,
})

export default ArrayTable