Skip to content

Commit 758bf62

Browse files
committed
docs: BottomNavigation examples
1 parent 97f3a2e commit 758bf62

36 files changed

+578
-215
lines changed

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
"no-multi-str": "error",
101101
"no-multiple-empty-lines": "error",
102102
"no-negated-condition": "off",
103-
"no-nested-ternary": "error",
103+
"no-nested-ternary": "off",
104104
"no-new": "error",
105105
"no-new-func": "error",
106106
"no-new-object": "error",
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading

app/ns-ui-widgets-category/bottom-navigation/basics/article.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

app/ns-ui-widgets-category/bottom-navigation/basics/basics-page.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

app/ns-ui-widgets-category/bottom-navigation/basics/basics-page.xml

Lines changed: 0 additions & 31 deletions
This file was deleted.

app/ns-ui-widgets-category/bottom-navigation/basics/basics-ts-page.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

app/ns-ui-widgets-category/bottom-navigation/basics/basics-ts-page.xml

Lines changed: 0 additions & 29 deletions
This file was deleted.

app/ns-ui-widgets-category/bottom-navigation/bottom-navigation-page.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
const ListViewLinksModel = require("../../links-view-model");
22
const link = require("../../link");
33
const navigationLinks = [
4-
new link("Basics", "ns-ui-widgets-category/bottom-navigation/basics/basics-page")
5-
// new link("Styling", "ns-ui-widgets-category/tab-view/styling/styling-page"),
6-
// new link("Title Icon Fonts", "ns-ui-widgets-category/tab-view/title-icon-fonts/title-icon-fonts-page"),
7-
// new link("Code Behind", "ns-ui-widgets-category/tab-view/code-behind/code-behind-page"),
8-
// new link("Navigation", "ns-ui-widgets-category/tab-view/navigation/navigation")
4+
new link("Usage", "ns-ui-widgets-category/bottom-navigation/usage/usage-page"),
5+
new link("Usage", "ns-ui-widgets-category/bottom-navigation/styling/styling-page"),
6+
new link("Events", "ns-ui-widgets-category/bottom-navigation/events/events-page"),
7+
new link("Properties", "ns-ui-widgets-category/bottom-navigation/properties/properties-page")
98
];
109
const navigationLinksTsc = [
11-
new link("Basics", "ns-ui-widgets-category/bottom-navigation/basics/basics-ts-page")
12-
// new link("Navigation", "ns-ui-widgets-category/tab-view/navigation/navigation-ts-page")
10+
new link("Usage", "ns-ui-widgets-category/bottom-navigation/usage/usage-ts-page"),
11+
new link("Usage", "ns-ui-widgets-category/bottom-navigation/styling/styling-ts-page"),
12+
new link("Events", "ns-ui-widgets-category/bottom-navigation/events/events-ts-page"),
13+
new link("Properties", "ns-ui-widgets-category/bottom-navigation/properties/properties-page")
1314
];
15+
1416
function onNavigatingTo(args) {
1517
const page = args.object;
1618

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
- `selectedIndexChanged` - Event used to track the tabs interaction though the change of `selectedIndex` property of `BottomNavigation` component. The event data is of type `SelectedIndexChangedEventData` extends `EventData` with two new properties:
2+
- `oldIndex` - Provides the old selected index.
3+
- `newIndwex` - Provides the new selected index.
4+
5+
<snippet id='bottom-navigation-events-js'/>
6+
<snippet id='bottom-navigation-events-tsc'/>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const fromObject = require("tns-core-modules/data/observable").fromObject;
2+
3+
exports.onNavigatingTo = (args) => {
4+
const page = args.object;
5+
const vm = fromObject({
6+
selectedIndex: 1
7+
});
8+
9+
page.bindingContext = vm;
10+
};
11+
12+
// >> bottom-navigation-events-js
13+
exports.onBottomNavLoaded = (args) => {
14+
// Using the loaded event to ger a reference to the BottomNavigation
15+
const bottomNavigation = args.object;
16+
17+
// Using selectedIndexChanged
18+
bottomNavigation.on("selectedIndexChanged", (args) => {
19+
// args is of type SelectedIndexChangedEventData
20+
const oldIndex = args.oldIndex;
21+
const newIndex = args.newIndex;
22+
console.log(`oldIndex: ${oldIndex}; newIndex: ${newIndex}`);
23+
24+
args.object.page.bindingContext.set("selectedIndex", newIndex);
25+
});
26+
};
27+
// << bottom-navigation-events-js
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<Page navigatingTo="onNavigatingTo">
2+
<Page.actionBar>
3+
<ActionBar title="BottomNavigation - Events"/>
4+
</Page.actionBar>
5+
<!-- >> bottom-navigation-events-xml -->
6+
<BottomNavigation selectedIndex="1" loaded="onBottomNavLoaded">
7+
8+
<!-- The bottom tab UI is created via TabStrip (the containier) and TabStripItem (for each tab)-->
9+
<TabStrip>
10+
<TabStripItem title="Home" iconSource="res://baseline_home_black_24pt"></TabStripItem>
11+
<TabStripItem title="Account" iconSource="res://baseline_account_balance_black_24pt"></TabStripItem>
12+
</TabStrip>
13+
14+
<!-- The number of TabContentItem components should corespond to the number of TabStripItem components -->
15+
<TabContentItem>
16+
<GridLayout rows="*, *">
17+
<Label row="0" text="Home Page" class="h2 text-center" color="orange"></Label>
18+
<Label row="1" text="{{ 'newIndex: ' + selectedIndex }}" class="h2 text-center"></Label>
19+
</GridLayout>
20+
</TabContentItem>
21+
<TabContentItem>
22+
<GridLayout rows="*, *">
23+
<Label row="0" text="Account Page" class="h2 text-center"></Label>
24+
<Label row="1" text="{{ 'newIndex: ' + selectedIndex }}" class="h2 text-center"></Label>
25+
</GridLayout>
26+
</TabContentItem>
27+
</BottomNavigation>
28+
<!-- << bottom-navigation-events-xml -->
29+
</Page>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// >> bottom-navigation-events-tsc
2+
import { EventData, fromObject } from "tns-core-modules/data/observable";
3+
import { BottomNavigation, SelectedIndexChangedEventData } from "tns-core-modules/ui/bottom-navigation";
4+
// >> (hide)
5+
import { Page } from "tns-core-modules/ui/page";
6+
7+
export function onNavigatingTo(args: EventData) {
8+
const page = args.object as Page;
9+
const vm = fromObject({
10+
selectedIndex: 1
11+
})
12+
13+
page.bindingContext = vm;
14+
}
15+
// << (hide)
16+
export function onBottomNavLoaded(args: EventData) {
17+
// Using the loaded event to ger a reference to the BottomNavigation
18+
const bottomNavigation = args.object as BottomNavigation;
19+
20+
// Using selectedIndexChanged
21+
bottomNavigation.on(BottomNavigation.selectedIndexChangedEvent , (args: SelectedIndexChangedEventData) => {
22+
const oldIndex: number = args.oldIndex;
23+
const newIndex: number = args.newIndex;
24+
console.log(`oldIndex: ${oldIndex}; newIndex: ${newIndex}`);
25+
26+
(<any>args.object).page.bindingContext.set("selectedIndex", newIndex);
27+
});
28+
};
29+
// << bottom-navigation-events-tsc
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<Page navigatingTo="onNavigatingTo">
2+
<Page.actionBar>
3+
<ActionBar title="BottomNavigation - Events"/>
4+
</Page.actionBar>
5+
<!-- >> bottom-navigation-events-xml -->
6+
<BottomNavigation selectedIndex="1" loaded="onBottomNavLoaded">
7+
8+
<!-- The bottom tab UI is created via TabStrip (the containier) and TabStripItem (for each tab)-->
9+
<TabStrip>
10+
<TabStripItem title="Home" iconSource="res://baseline_home_black_24pt"></TabStripItem>
11+
<TabStripItem title="Account" iconSource="res://baseline_account_balance_black_24pt"></TabStripItem>
12+
</TabStrip>
13+
14+
<!-- The number of TabContentItem components should corespond to the number of TabStripItem components -->
15+
<TabContentItem>
16+
<GridLayout rows="*, *">
17+
<Label row="0" text="Home Page" class="h2 text-center" color="orangered"></Label>
18+
<Label row="1" text="{{ 'newIndex: ' + selectedIndex }}" class="h2 text-center"></Label>
19+
</GridLayout>
20+
</TabContentItem>
21+
<TabContentItem>
22+
<GridLayout rows="*, *">
23+
<Label row="0" text="Account Page" class="h2 text-center"></Label>
24+
<Label row="1" text="{{ 'selectednewIndexIndex: ' + selectedIndex }}" class="h2 text-center"></Label>
25+
</GridLayout>
26+
</TabContentItem>
27+
</BottomNavigation>
28+
<!-- << bottom-navigation-events-xml -->
29+
</Page>

app/ns-ui-widgets-category/bottom-navigation/metadata.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ description: The NativeScript's BottomNavigation component provides a simple way
44
position: 416
55
slug: bottom-navigation
66
---
7+
example-order: usage, styling, properties, events, tips-and-tricks
Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
> **Disclaimer:** The `BottomNavigation` component is currently in **Beta** version. The component is being actively developed - use [the tracking issue](https://github.com/NativeScript/NativeScript/issues/6967) for details about the ongoing implementation.
22
33
The `BottomNavigation` component provides a simple way to navigate between different views while providing common UI for both iOS and Android platforms. The recommended scenario suitable for `BottomNavigation` is a high level navigaiton with 3 to 5 tabs each with separate function. For additional information and details about bottom navigation refer to [the Material Design guidelines](https://material.io/design/components/bottom-navigation.html#usage).
44

@@ -12,27 +12,3 @@ Limitations
1212
- No navigation transitions.
1313
- No navigation gestures (e.g., swipe to navigate).
1414
- No content preloading.
15-
16-
17-
18-
<!--
19-
20-
The `TabView` component provides a simple way to navigate between different views by tapping on some of the tabs or by swiping between the views.
21-
By default the `TabView` will load the view of the first tab, however it's possible to load alternative tabs when the app starts by setting the component’s `selectedIndex` property.
22-
23-
<----snippet id='tab-view-require'/>
24-
<----snippet id='tab-view-import'/>
25-
26-
The general behavior of the `TabView` component is to load its items on demand. This means that every `TabViewItem` view will be loaded when it is shown and will be unloaded when it disappears. Respectively, the `loaded` and `unloaded` events will be fired when navigating between views. However, there are some specifics for each platform (iOS and Android), which are described in the notes below.
27-
28-
> Note (iOS specific): The iOS implementation uses `UITabBarController`. This means that only one `TabViewItem` can be shown at a given time and only one needs to be loaded. When the user selects a new `TabViewItem`, we load the new item and unload the previous one.
29-
30-
> Note (Android specific): The Android implementation uses a `ViewPager` control, which allows using the `swipe` gesture to navigate to the next or previous tab. This means that only one `TabViewItem` can be shown, but multiple `TabViewItems` need to be loaded to the side. If this is not done, you will not be able to see the next `TabViewItem` contents during the swipe. By default, the `ViewPager` control will pre-load one `TabViewItem` on the left and on on the right. Regarding that, if one of the items is already pre-loaded, it will not be loaded again. In NativeScript, we have exposed a property called `androidOffscreenTabLimit`, which allows specifying how many components should be pre-loaded to the sides (if you are setting up `androidOffscreenTabLimit` to 0, and using a nativescript version lower than 5, the Android behavior will match to the iOS behavior).
31-
32-
The iOS and Android UX guidelines regarding the Tab controls differ greatly. The difference is described in the below points:
33-
34-
* The iOS tabs have their tab bar, which will be displayed always on the bottom and does not allow swipe gesture for changing tabs.
35-
* The Android tabs are on top and will enable the swipe navigation between the tabs.
36-
* For Android we have `androidTabsPosition` property which has two options `top`(default value) and `bottom`. Setting up this property to `bottom` allows mimicking Bottom Tab Navigation control(provided by android support library v25 release). Setting the Tabs at the bottom will disable the swipe navigation and the items preloading functionality.
37-
38-
-->
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
- `items: Array<TabContentItem>;` - Gets or sets the items of the BottomNavigation.
3+
4+
- `tabStrip: TabStrip;` - Gets or sets the tab strip of the BottomNavigation.
5+
6+
- `selectedIndex: number;` - Gets or sets the selectedIndex of the BottomNavigation.
7+
8+
- `android: any` /* android.view.View */; - Gets the native [android widget](http://developer.android.com/reference/android/support/v4/view/ViewPager.html) that represents the user interface for this component. Valid only when running on Android OS.
9+
10+
- `ios: any` /* UITabBarController */; - Gets the native iOS [UITabBarController](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITabBarController_Class/) that represents the user interface for this component. Valid only when running on iOS.
11+
12+
<snippet id='bottom-navigation-properties-js'/>
13+
<snippet id='bottom-navigation-properties-tsc'/>
14+
<snippet id='bottom-navigation-properties-tsc-xml'>
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
const Label = require("tns-core-modules/ui/label").Label;
2+
const StackLayout = require("tns-core-modules/ui/layouts/stack-layout").StackLayout;
3+
const Color = require("tns-core-modules/color/color").Color;
4+
// >> bottom-navigation-events-js
5+
const TabStrip = require("tns-core-modules/ui/bottom-navigation").TabStrip;
6+
const TabStripItem = require("tns-core-modules/ui/bottom-navigation").TabStripItem;
7+
const TabContentItem = require("tns-core-modules/ui/bottom-navigation").TabContentItem;
8+
9+
exports.onBottomNavLoaded = (args) => {
10+
// BottomNavigation
11+
const bottomNav = args.object;
12+
/*
13+
Using the items property to assign array of TabContentItem components (with content)
14+
Note: The number of TabContentItem components should be equal to the number of TabStripItem components
15+
*/
16+
const tabContentItemsArray = createTabsContentArray();
17+
bottomNav.items = tabContentItemsArray;
18+
19+
/*
20+
Using the tabStrip property to createa a TabStrip with multiple TabStripItems (tabs)
21+
Note: The number of TabStripItem components should be equal to the number of TabContentItem components
22+
*/
23+
const tabStrip = createTabStrip();
24+
bottomNav.tabStrip = tabStrip;
25+
26+
/*
27+
Using the selectedIndex property
28+
*/
29+
bottomNav.selectedIndex = 1;
30+
31+
/*
32+
Using the nativeView property (correspnding to bottomNav.ios or bottomNav.android depending on the used platform)
33+
*/
34+
console.log("bottomNav.nativeView: ", bottomNav.nativeView);
35+
};
36+
// << bottom-navigation-events-js
37+
38+
function createTabStrip() {
39+
// TabStrip
40+
const tabStrip = new TabStrip();
41+
tabStrip.iosIconRenderingMode = "automatic"; // iOS only (automatic is the default value)
42+
43+
// An array of TabStripItem
44+
const tabStripItems = [];
45+
for (let index = 0; index < 3; index++) {
46+
// Each item is of type TabStripItem
47+
const item = new TabStripItem();
48+
/*
49+
Using TabStripItem title property
50+
*/
51+
item.title = `${index === 0 ? "Home" : (index === 1 ? "Account" : "Search")}`;
52+
/*
53+
Using TabStripItem title property
54+
*/
55+
item.iconSource = index === 0 ? "res://baseline_home_black_24pt" : (index === 1 ? "res://baseline_account_balance_black_24pt" : "res://baseline_search_black_24pt");
56+
tabStripItems.push(item);
57+
}
58+
tabStrip.items = tabStripItems;
59+
60+
return tabStrip;
61+
}
62+
63+
function createTabsContentArray() {
64+
// array of TabContentItem
65+
const arr = [];
66+
for (let index = 0; index < 3; index++) {
67+
// item is of type TabContentItem
68+
const item = new TabContentItem();
69+
// The createContent is a custom method that returns a StackLayout with a Label as a chils
70+
item.view = createContent(index);
71+
arr.push(item);
72+
}
73+
74+
return arr;
75+
}
76+
77+
function createContent(index) {
78+
const label = new Label();
79+
label.text = `${index === 0 ? "Home" : (index === 1 ? "Account" : "Search")}`;
80+
label.className = "h2 text-center";
81+
label.color = new Color("red");
82+
const stack = new StackLayout();
83+
stack.verticalAlignment = "middle";
84+
stack.addChild(label);
85+
86+
return stack;
87+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Page>
2+
<Page.actionBar>
3+
<ActionBar title="BottomNavigation - Usage"/>
4+
</Page.actionBar>
5+
6+
<BottomNavigation loaded="onBottomNavLoaded"></BottomNavigation>
7+
</Page>

0 commit comments

Comments
 (0)