-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
三少
committed
Sep 17, 2021
1 parent
baac1a6
commit 745a5c1
Showing
11 changed files
with
305 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#!/bin/sh | ||
. "$(dirname "$0")/_/husky.sh" | ||
|
||
yarn lint-staged | ||
npm test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { View, Block } from '@tarojs/components' | ||
import * as utils from '../wxs/utils' | ||
import Icon from '../icon' | ||
import Info from '../info' | ||
import { TabbarItemProps } from '../../../types/tabbar-item' | ||
|
||
export default function Index( | ||
props: TabbarItemProps & { | ||
index: number | ||
active?: number | ||
activeColor?: string | ||
inactiveColor?: string | ||
onChange?: (data?: string | number) => void | ||
}, | ||
) { | ||
const { | ||
icon, | ||
name, | ||
iconPrefix = 'van-icon', | ||
dot, | ||
info = null, | ||
renderIconactive, | ||
renderIcon, | ||
index, | ||
active, | ||
activeColor, | ||
inactiveColor, | ||
onChange, | ||
children, | ||
style, | ||
className, | ||
onClick, | ||
...others | ||
} = props | ||
|
||
const _click = function () { | ||
if (onChange) { | ||
const _active = name || index | ||
if (_active !== active) { | ||
onChange(_active) | ||
} | ||
} | ||
|
||
onClick?.(name || index) | ||
} | ||
|
||
return onChange ? ( | ||
<View | ||
className={ | ||
utils.bem('tabbar-item', { | ||
active: active === (name || index), | ||
}) + | ||
' custom-class' + | ||
` ${className}` | ||
} | ||
style={utils.style([ | ||
{ | ||
color: active === (name || index) ? activeColor : inactiveColor, | ||
}, | ||
style, | ||
])} | ||
{...others} | ||
onClick={_click} | ||
> | ||
<View className="van-tabbar-item__icon"> | ||
{icon ? ( | ||
<Icon | ||
info={null} | ||
name={icon} | ||
classPrefix={iconPrefix} | ||
className="van-tabbar-item__icon__inner" | ||
></Icon> | ||
) : ( | ||
<Block> | ||
{active === (name || index) ? renderIconactive : renderIcon} | ||
</Block> | ||
)} | ||
<Info dot={dot} info={info} className="van-tabbar-item__info"></Info> | ||
</View> | ||
<View className="van-tabbar-item__text">{children}</View> | ||
</View> | ||
) : ( | ||
<></> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import Taro from '@tarojs/taro' | ||
import { | ||
useState, | ||
isValidElement, | ||
cloneElement, | ||
useEffect, | ||
useCallback, | ||
} from 'react' | ||
import toArray from 'rc-util/lib/Children/toArray' | ||
import { View, Block } from '@tarojs/components' | ||
import * as utils from '../wxs/utils' | ||
import { getRect } from '../common/utils' | ||
import { TabbarProps } from '../../../types/tabbar' | ||
import { TabbarItemProps } from '../../../types/tabbar-item' | ||
|
||
function parseTabList(children: React.ReactNode): any[] { | ||
return toArray(children) | ||
.map((node: React.ReactElement<TabbarItemProps>) => { | ||
if (isValidElement(node)) { | ||
const key = node.key !== undefined ? String(node.key) : undefined | ||
return { | ||
key, | ||
...node.props, | ||
node, | ||
} | ||
} | ||
|
||
return null | ||
}) | ||
.filter((tab) => tab) | ||
} | ||
|
||
export default function Index(props: TabbarProps) { | ||
const [state, setState]: any = useState({ | ||
height: 50, | ||
current: 0, | ||
}) | ||
const { height, current } = state | ||
|
||
const { | ||
active, | ||
activeColor, | ||
inactiveColor, | ||
border = true, | ||
fixed = true, | ||
safeAreaInsetBottom = true, | ||
zIndex = 1, | ||
placeholder, | ||
onChange, | ||
style, | ||
className, | ||
children, | ||
...others | ||
} = props | ||
const _change = useCallback( | ||
function (data) { | ||
setState((pre: any) => { | ||
return { | ||
...pre, | ||
current: data, | ||
} | ||
}) | ||
onChange?.(data) | ||
}, | ||
[onChange], | ||
) | ||
const tabs = parseTabList(children) | ||
const newChildren: any = tabs.map((tab, index) => { | ||
return cloneElement(tab.node, { | ||
key: index, | ||
index: index, | ||
active: current, | ||
activeColor: activeColor, | ||
inactiveColor: inactiveColor, | ||
onChange: _change, | ||
}) | ||
}) | ||
|
||
useEffect( | ||
function () { | ||
setState((pre: any) => { | ||
return { | ||
...pre, | ||
current: active, | ||
} | ||
}) | ||
}, | ||
[active], | ||
) | ||
|
||
useEffect( | ||
function () { | ||
if (!fixed || !placeholder) { | ||
return | ||
} | ||
Taro.nextTick(() => { | ||
getRect(null, '.van-tabbar').then((res: any) => { | ||
setState((pre: any) => { | ||
return { | ||
...pre, | ||
height: res.height, | ||
} | ||
}) | ||
}) | ||
}) | ||
}, | ||
[fixed, placeholder], | ||
) | ||
// useEffect( | ||
// function () { | ||
// if (!Array.isArray(children) || !children.length) { | ||
// return | ||
// } | ||
// children.forEach((child) => child.updateFromParent()) | ||
// }, | ||
// // eslint-disable-next-line react-hooks/exhaustive-deps | ||
// [active, activeColor, inactiveColor], | ||
// ) | ||
|
||
return ( | ||
<Block> | ||
<View | ||
className={ | ||
(border ? 'van-hairline--top-bottom' : '') + | ||
' ' + | ||
utils.bem('tabbar', { | ||
fixed, | ||
safe: safeAreaInsetBottom, | ||
}) + | ||
' custom-class' + | ||
` ${className}` | ||
} | ||
style={utils.style([zIndex ? 'z-index: ' + zIndex : '', style])} | ||
{...others} | ||
> | ||
{newChildren} | ||
</View> | ||
{fixed && placeholder && ( | ||
<View style={'height: ' + height + 'px;'}></View> | ||
)} | ||
</Block> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { ComponentClass, ReactNode } from 'react' | ||
import { StandardProps } from '@tarojs/components' | ||
|
||
export interface TabbarItemProps extends Omit<StandardProps, 'onClick'> { | ||
info: null | string | ||
name: null | string | number | ||
icon?: string | ||
dot?: boolean | ||
iconPrefix?: string | ||
renderIconactive?: ReactNode | ||
renderIcon?: ReactNode | ||
children?: ReactNode | ||
onClick?: (data: string | number) => void | ||
} | ||
|
||
declare const TabbarItem: ComponentClass<TabbarItemProps> | ||
|
||
export { TabbarItem } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { ComponentClass, ReactNode } from 'react' | ||
import { StandardProps } from '@tarojs/components' | ||
|
||
export interface TabbarProps extends StandardProps { | ||
active?: number | ||
activeColor?: string | ||
inactiveColor?: string | ||
fixed?: boolean | ||
placeholder?: boolean | ||
border?: boolean | ||
zIndex?: number | ||
safeAreaInsetBottom?: boolean | ||
children?: ReactNode | ||
onChange?: (data?: string | number) => void | ||
} | ||
|
||
declare const Tabbar: ComponentClass<TabbarProps> | ||
|
||
export { Tabbar } |