-
Notifications
You must be signed in to change notification settings - Fork 957
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
Showing
15 changed files
with
1,992 additions
and
25 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
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
133 changes: 133 additions & 0 deletions
133
src/framework/ui/overflowMenu/overflowMenu.component.tsx
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,133 @@ | ||
import React from 'react'; | ||
import { | ||
ViewProps, | ||
View, | ||
GestureResponderEvent, | ||
} from 'react-native'; | ||
import { | ||
StyledComponentProps, | ||
StyleType, | ||
styled, | ||
} from '@kitten/theme'; | ||
import { | ||
OverflowMenuItemType, | ||
OverflowMenuItem as OverflowMenuItemComponent, | ||
Props as OverflowMenuItemProps, | ||
} from './overflowMenuItem.component'; | ||
import { | ||
Popover as PopoverComponent, | ||
Props as PopoverProps, | ||
} from '../popover/popover.component'; | ||
import { Omit } from '../service/type'; | ||
|
||
type MenuItemElement = React.ReactElement<OverflowMenuItemProps>; | ||
|
||
interface OverflowMenuProps { | ||
children: React.ReactElement<any>; | ||
items: OverflowMenuItemType[]; | ||
size?: string; | ||
onSelect?: (event: GestureResponderEvent, index: number) => void; | ||
} | ||
|
||
const Popover = styled<PopoverComponent, PopoverProps>(PopoverComponent); | ||
const OverflowMenuItem = | ||
styled<OverflowMenuItemComponent, OverflowMenuItemProps>(OverflowMenuItemComponent); | ||
|
||
export type Props = & StyledComponentProps & OverflowMenuProps & Omit<PopoverProps, 'content'>; | ||
|
||
export class OverflowMenu extends React.Component<Props> { | ||
|
||
private isFirstItem = (index: number): boolean => { | ||
return index === 0; | ||
}; | ||
|
||
private isLastItem = (index: number): boolean => { | ||
return this.props.items.length - 1 === index; | ||
}; | ||
|
||
private isSingleItem = (): boolean => { | ||
return this.props.items.length === 1; | ||
}; | ||
|
||
private getPopoverStyle = (style: StyleType): StyleType => ({ | ||
...style.popover, | ||
borderRadius: style.borderRadius, | ||
}); | ||
|
||
private onSelect = (event: GestureResponderEvent, index: number): void => { | ||
const { visible, onSelect } = this.props; | ||
// FIXME: this is due to popover renders always with the "opacity": 0, | ||
// so popover can listen events even it's "invisible" | ||
if (visible && onSelect) { | ||
onSelect(event, index); | ||
} | ||
}; | ||
|
||
private getMenuItemStyle = (style: StyleType, index: number): StyleType => { | ||
const borderRadius: number = style.menuItem.borderRadius; | ||
|
||
if (this.isFirstItem(index) && !this.isSingleItem()) { | ||
return { | ||
borderTopLeftRadius: borderRadius, | ||
borderTopRightRadius: borderRadius, | ||
}; | ||
} else if (this.isLastItem(index) && !this.isSingleItem()) { | ||
return { | ||
borderBottomLeftRadius: borderRadius, | ||
borderBottomRightRadius: borderRadius, | ||
}; | ||
} else if (this.isSingleItem()) { | ||
return { | ||
borderRadius: borderRadius, | ||
}; | ||
} | ||
}; | ||
|
||
private renderMenuItem = (item: OverflowMenuItemType, index: number): MenuItemElement => { | ||
const { size, themedStyle } = this.props; | ||
const itemStyle: StyleType = this.getMenuItemStyle(themedStyle, index); | ||
|
||
return ( | ||
<OverflowMenuItem | ||
{...item} | ||
size={size} | ||
isLastItem={this.isLastItem(index)} | ||
style={itemStyle} | ||
key={index} | ||
index={index} | ||
onPress={this.onSelect} | ||
/> | ||
); | ||
}; | ||
|
||
private renderComponentChildren = (): MenuItemElement[] => { | ||
return this.props.items.map((item: OverflowMenuItemType, index: number) => | ||
this.renderMenuItem(item, index)); | ||
}; | ||
|
||
private renderMenuContent = (): React.ReactElement<ViewProps> => { | ||
const menuItems: MenuItemElement[] = this.renderComponentChildren(); | ||
|
||
return ( | ||
<View style={this.props.style}> | ||
{menuItems} | ||
</View> | ||
); | ||
}; | ||
|
||
public render(): React.ReactNode { | ||
const { children, themedStyle, ...restProps } = this.props; | ||
const menu: React.ReactElement<ViewProps> = this.renderMenuContent(); | ||
const popoverStyle: StyleType = this.getPopoverStyle(themedStyle); | ||
|
||
return ( | ||
<Popover | ||
{...restProps} | ||
indicatorOffset={2} | ||
content={menu} | ||
style={[popoverStyle, restProps.style]}> | ||
{children} | ||
</Popover> | ||
); | ||
} | ||
} |
Oops, something went wrong.