-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. change Props naming 2. changed behavior Previously, only the transparency of the button was adjusted depending on the state but now the actual position is adjusted. 3. Change state management. Previously, the active was managed as a "state", modified to manage it as a "props". 4. path name changed FloatingActionButtonWrapper> FAB
- Loading branch information
1 parent
0982887
commit 6bddf3d
Showing
5 changed files
with
166 additions
and
135 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 |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import {Animated, Easing, StyleProp, View, ViewStyle} from 'react-native'; | ||
import {ButtonSize, IconButton} from '../IconButton'; | ||
import {DoobooTheme, withTheme} from '../theme'; | ||
import {Icon, IconName} from '../Icon'; | ||
import React, {ReactElement, useEffect, useRef, useState} from 'react'; | ||
|
||
import styled from '@emotion/native'; | ||
|
||
export const StyledIcon = styled(Icon)` | ||
color: ${({theme}) => theme.textContrast}; | ||
`; | ||
|
||
const FABItemWrapper = styled.View` | ||
margin: 10px; | ||
`; | ||
|
||
export interface FABItem { | ||
icon: IconName; | ||
id: string; | ||
} | ||
|
||
export interface FABProps<Item extends FABItem> { | ||
isActive: boolean; | ||
ItemList: Item[]; | ||
onPressFAB: () => void; | ||
onPressListItem: (item?: Item) => void; | ||
renderFAB?: () => ReactElement; | ||
renderFabItem?: (item: Item, idx: number) => ReactElement; | ||
size: ButtonSize; | ||
style?: StyleProp<ViewStyle>; | ||
} | ||
|
||
function FloatingActionButtons<Item extends FABItem = FABItem>({ | ||
isActive, | ||
theme, | ||
ItemList, | ||
onPressFAB, | ||
onPressListItem, | ||
renderFAB, | ||
renderFabItem, | ||
size = 'large', | ||
style, | ||
}: FABProps<Item> & { | ||
theme: DoobooTheme; | ||
}): ReactElement { | ||
const fadeAnim = useRef(new Animated.Value(0)); | ||
|
||
useEffect(() => { | ||
return Animated.timing(fadeAnim.current, { | ||
toValue: isActive ? 1 : 0, | ||
useNativeDriver: true, | ||
easing: Easing.quad, | ||
duration: 200, | ||
}).start(); | ||
}, [fadeAnim, isActive]); | ||
|
||
useEffect(() => { | ||
Animated.timing(fadeAnim.current, { | ||
toValue: 1, | ||
duration: 200, | ||
easing: Easing.linear, | ||
useNativeDriver: true, | ||
}).start(); | ||
}, [fadeAnim]); | ||
|
||
return ( | ||
<View | ||
style={[ | ||
{ | ||
position: 'absolute', | ||
right: 10, | ||
bottom: 10, | ||
zIndex: 999, | ||
flex: 1, | ||
}, | ||
style, | ||
]}> | ||
<Animated.View> | ||
{isActive && | ||
ItemList.map((item, idx) => { | ||
return ( | ||
<FABItemWrapper | ||
key={item.id} | ||
style={{ | ||
position: 'absolute', | ||
transform: [{translateY: -80 * (idx + 1)}], | ||
}}> | ||
{renderFabItem ? ( | ||
renderFabItem(item, idx) | ||
) : ( | ||
<IconButton | ||
testID={item.id} | ||
size={size} | ||
icon={ | ||
<StyledIcon theme={theme} size={24} name={item.icon} /> | ||
} | ||
onPress={() => onPressListItem(item)} | ||
/> | ||
)} | ||
</FABItemWrapper> | ||
); | ||
})} | ||
</Animated.View> | ||
<FABItemWrapper> | ||
<Animated.View> | ||
{renderFAB ? ( | ||
renderFAB() | ||
) : ( | ||
<IconButton | ||
testID={'main_fab'} | ||
size={size} | ||
icon={ | ||
isActive ? ( | ||
<StyledIcon size={24} name="cross-light" /> | ||
) : ( | ||
<StyledIcon size={24} name="add-light" /> | ||
) | ||
} | ||
onPress={onPressFAB} | ||
/> | ||
)} | ||
</Animated.View> | ||
</FABItemWrapper> | ||
</View> | ||
); | ||
} | ||
|
||
export const FAB = withTheme(FloatingActionButtons); |
This file was deleted.
Oops, something went wrong.
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,32 +1,54 @@ | ||
import {FAB, FABItem, FABProps} from '../../main'; | ||
import {fireEvent, render} from '@testing-library/react-native'; | ||
import React, {ReactElement} from 'react'; | ||
import {RenderAPI, fireEvent, render} from '@testing-library/react-native'; | ||
|
||
import {ReactElement} from 'react'; | ||
import {IconButton} from '../IconButton'; | ||
import {View} from 'react-native'; | ||
import {createComponent} from '../../test/testUtils'; | ||
|
||
const Component = (props: FABProps<FABItem>): ReactElement => | ||
createComponent(<FAB {...props} />); | ||
|
||
describe('[FAB]', () => { | ||
it('test mainFAB onPress callback', async () => { | ||
it('should render', async () => { | ||
let count = 0; | ||
let item1: FABItem = {icon: 'bell-solid', id: 'item1'}; | ||
let item: FABItem = {icon: 'bell-solid', id: 'item1'}; | ||
let resItem: FABItem; | ||
|
||
const {getByTestId} = render( | ||
Component({ | ||
ButtonList: [item1], | ||
ItemList: [item], | ||
isActive: true, | ||
size: 'large', | ||
onPressListItem: (item1) => { | ||
onPressListItem: (item) => { | ||
count += 1; | ||
resItem = item1; | ||
resItem = item; | ||
}, | ||
onPressFAB: () => {}, | ||
}), | ||
); | ||
|
||
expect(count).toBe(0); | ||
fireEvent.press(getByTestId('item1')); | ||
expect(count).toBe(1); | ||
expect(resItem.id).toBe(item1.id); | ||
expect(resItem.id).toBe(item.id); | ||
}); | ||
|
||
it('should render customFAB', async () => { | ||
const testingLib = render( | ||
Component({ | ||
ItemList: [{icon: 'bell-solid', id: 'item1'}], | ||
isActive: true, | ||
size: 'large', | ||
onPressListItem: (item1) => {}, | ||
onPressFAB: () => {}, | ||
renderFAB: () => <View />, | ||
renderFabItem: (item, idx) => <View />, | ||
}), | ||
); | ||
|
||
const json = testingLib.toJSON(); | ||
|
||
expect(json).toBeTruthy(); | ||
}); | ||
}); |
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