Skip to content

Commit

Permalink
fix(antd/next): fix form-tab typings (#848)
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed May 18, 2020
1 parent 46f0839 commit 2cadf73
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/antd-components/src/form-tab/index.tsx
Expand Up @@ -80,7 +80,7 @@ const addErrorBadge = (

type ExtendsProps = StateMap &
typeof EffectHooks & {
TabPane: React.FC<TabPaneProps>
TabPane: React.FC<IVirtualBoxProps<TabPaneProps>>
}

type ExtendsState = {
Expand Down
33 changes: 24 additions & 9 deletions packages/next-components/src/form-tab/index.tsx
Expand Up @@ -47,9 +47,12 @@ const parseTabItems = (items: any, hiddenKeys?: string[]) => {
}, [])
}


const parseDefaultActiveKey = (hiddenKeys: Array<string> = [], items: any, defaultActiveKey) => {
if(!hiddenKeys.includes(defaultActiveKey))return defaultActiveKey
const parseDefaultActiveKey = (
hiddenKeys: Array<string> = [],
items: any,
defaultActiveKey
) => {
if (!hiddenKeys.includes(defaultActiveKey)) return defaultActiveKey

const index = items.findIndex(item => !hiddenKeys.includes(item.key))
return index >= 0 ? items[index].key : ''
Expand Down Expand Up @@ -77,7 +80,7 @@ const addErrorBadge = (

type ExtendsProps = StateMap &
typeof EffectHooks & {
TabPane: React.FC<ItemProps>
TabPane: React.FC<IVirtualBoxProps<ItemProps>>
}

type ExtendsState = {
Expand All @@ -90,12 +93,20 @@ export const FormTab: React.FC<IVirtualBoxProps<IFormTab>> &
'tab',
({ form, schema, name, path }: ISchemaVirtualFieldComponentProps) => {
const orderProperties = schema.getOrderProperties()
let { hiddenKeys, defaultActiveKey, ...componentProps } = schema.getExtendsComponentProps()
let {
hiddenKeys,
defaultActiveKey,
...componentProps
} = schema.getExtendsComponentProps()
hiddenKeys = hiddenKeys || []
const [{ activeKey, childrenErrors }, setFieldState] = useFieldState<
ExtendsState
>({
activeKey: parseDefaultActiveKey(hiddenKeys, orderProperties, defaultActiveKey),
activeKey: parseDefaultActiveKey(
hiddenKeys,
orderProperties,
defaultActiveKey
),
childrenErrors: []
})

Expand All @@ -113,7 +124,11 @@ export const FormTab: React.FC<IVirtualBoxProps<IFormTab>> &
useEffect(() => {
if (Array.isArray(hiddenKeys)) {
setFieldState({
activeKey: parseDefaultActiveKey(hiddenKeys, orderProperties, defaultActiveKey)
activeKey: parseDefaultActiveKey(
hiddenKeys,
orderProperties,
defaultActiveKey
)
})
}
}, [hiddenKeys.length])
Expand All @@ -126,8 +141,8 @@ export const FormTab: React.FC<IVirtualBoxProps<IFormTab>> &
})
}
})
EffectHooks.onTabActiveKeyChange$().subscribe(({ value, name, path }) => {
if(!itemsRef.current.map(item => item.key).includes(value))return
EffectHooks.onTabActiveKeyChange$().subscribe(({ value, name, path }) => {
if (!itemsRef.current.map(item => item.key).includes(value)) return
matchUpdate(name, path, () => {
setFieldState({
activeKey: value
Expand Down

0 comments on commit 2cadf73

Please sign in to comment.