Skip to content

Commit 30e2a5c

Browse files
whitestranger7Ivan Vezhnavetsmalashkevichgreenfrvr
authored
feature: menu group - default expand option (#1389)
* feature(menuGroup): add prop for menuGropu that allow component to be expanded by default * feat(menuGroup): add expanded menuGroup example to showcase * fix(menuGroup): resolve comments * fix: typo fix * remove: remove dead code * update: change initialExpand logic * fix: typescript fix * fix: fire event only when component was mounted * feat: adjusted initially expanded behaviour implementation * chore: removed redundant code * chore: code cleanup Co-authored-by: Ivan Vezhnavets <[email protected]> Co-authored-by: Alexei Malashkevich <[email protected]> Co-authored-by: Artsiom Grintsevich <[email protected]>
1 parent d0ececa commit 30e2a5c

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

Diff for: src/components/ui/menu/menuGroup.component.tsx

+23-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { MenuItemDescriptor } from './menu.service';
2525

2626
export interface MenuGroupProps extends MenuItemProps {
2727
children?: ChildrenWithProps<MenuItemProps>;
28+
initiallyExpanded?: boolean;
2829
}
2930

3031
export type MenuGroupElement = React.ReactElement<MenuGroupProps>;
@@ -59,6 +60,9 @@ const POSITION_OUTSCREEN: Point = Point.outscreen();
5960
* to render to end of the *title*.
6061
* Expected to return an Image.
6162
*
63+
* @property {boolean} initiallyExpanded - Boolean value which defines whether group should be initially expanded.
64+
* If true - menu group will be expanded by default.
65+
*
6266
* @property {TouchableOpacityProps} ...TouchableOpacityProps - Any props applied to TouchableOpacity component.
6367
*
6468
* @overview-example MenuGroups
@@ -68,8 +72,23 @@ export class MenuGroup extends React.Component<MenuGroupProps, State> {
6872
public state: State = {
6973
submenuHeight: 1,
7074
};
75+
76+
private initiallyExpanded: boolean;
77+
7178
private expandAnimation: Animated.Value = new Animated.Value(0);
7279

80+
constructor(props) {
81+
super(props);
82+
this.initiallyExpanded = props.initiallyExpanded;
83+
}
84+
85+
public componentDidUpdate(prevProps: Readonly<MenuGroupProps>, prevState: Readonly<State>, snapshot?: any) {
86+
const submenuHeightChanged = this.state.submenuHeight !== prevState.submenuHeight;
87+
if (submenuHeightChanged && this.hasSubmenu && this.initiallyExpanded) {
88+
this.expandAnimation.setValue(this.state.submenuHeight);
89+
}
90+
}
91+
7392
private get hasSubmenu(): boolean {
7493
return React.Children.count(this.props.children) > 0;
7594
}
@@ -102,6 +121,8 @@ export class MenuGroup extends React.Component<MenuGroupProps, State> {
102121

103122
private onPress = (descriptor: MenuItemDescriptor, event: GestureResponderEvent): void => {
104123
if (this.hasSubmenu) {
124+
this.initiallyExpanded = false;
125+
105126
const expandValue: number = this.expandAnimationValue > 0 ? 0 : this.state.submenuHeight;
106127
this.createExpandAnimation(expandValue).start();
107128
this.props.onPress && this.props.onPress(descriptor, event);
@@ -129,7 +150,7 @@ export class MenuGroup extends React.Component<MenuGroupProps, State> {
129150

130151
return (
131152
<Animated.View style={{ transform: [{ rotate: this.expandToRotateInterpolation }] }}>
132-
<ChevronDown {...evaProps} fill={style.tintColor}/>
153+
<ChevronDown {...evaProps} fill={style.tintColor as string}/>
133154
</Animated.View>
134155
);
135156
};
@@ -150,7 +171,7 @@ export class MenuGroup extends React.Component<MenuGroupProps, State> {
150171

151172
private renderMeasuringGroupedItems = (evaStyle): MeasuringElement => {
152173
return (
153-
<MeasureElement
174+
<MeasureElement
154175
shouldUseTopInsets={ModalService.getShouldUseTopInsets}
155176
onMeasure={this.onSubmenuMeasure}>
156177
{this.renderGroupedItems(evaStyle)}

Diff for: src/showcases/components/menu/menuGroups.component.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export const MenuGroupsShowcase = () => {
3131
<MenuItem title='UI Kitten' accessoryLeft={StarIcon}/>
3232
<MenuItem title='Kitten Tricks' accessoryLeft={StarIcon}/>
3333
</MenuGroup>
34-
<MenuGroup title='Akveo Angular' accessoryLeft={BrowserIcon}>
34+
<MenuGroup title='Akveo Angular' accessoryLeft={BrowserIcon} initiallyExpanded={true}>
3535
<MenuItem title='Nebular' accessoryLeft={StarIcon}/>
3636
<MenuItem title='ngx-admin' accessoryLeft={StarIcon}/>
3737
<MenuItem title='UI Bakery' accessoryLeft={StarIcon}/>

0 commit comments

Comments
 (0)