Skip to content

Fix/tab controller item width #888

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added demo/src/assets/icons/add.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demo/src/assets/icons/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demo/src/assets/icons/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demo/src/assets/icons/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demo/src/assets/icons/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions demo/src/configurations.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Assets, Typography, Spacings} from 'react-native-ui-lib'; // eslint-disable-line

Assets.loadAssetsGroup('icons.demo', {
add: require('./assets/icons/add.png'),
close: require('./assets/icons/close.png'),
dashboard: require('./assets/icons/dashboard.png'),
image: require('./assets/icons/image.png'),
Expand Down
66 changes: 33 additions & 33 deletions demo/src/screens/componentScreens/TabControllerScreen/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, {Component} from 'react';
import {ActivityIndicator} from 'react-native';
import {
Assets,
TabController,
Colors,
View,
Expand Down Expand Up @@ -28,16 +29,21 @@ const TABS = [
];

class TabControllerScreen extends Component {
state = {
asCarousel: true,
centerSelected: false,
fewItems: false,
selectedIndex: 0,
items: _.chain(TABS)
.map((tab) => ({label: tab, key: tab}))
.value(),
key: Date.now()
};
constructor(props) {
super(props);


this.state = {
asCarousel: true,
centerSelected: false,
fewItems: false,
selectedIndex: 0,
key: Date.now()
};

const items = this.generateTabItems();
this.state.items = items;
}

componentDidMount() {
// this.slow();
Expand All @@ -53,33 +59,25 @@ class TabControllerScreen extends Component {
}, 10);
}

addTab = () => {
const {tabsCount} = this.state;
onAddItem = () => {
console.warn('Add Item');
}

if (tabsCount < 6) {
this.setState({
tabsCount: tabsCount + 1,
key: Date.now(),
selectedIndex: tabsCount
});
generateTabItems = (fewItems = this.state.fewItems, centerSelected = this.state.centerSelected) => {
let items = _.chain(TABS)
.take(fewItems ? 3 : TABS.length)
.map((tab) => ({label: tab, key: tab}))
.value();

if (!centerSelected) {
items = [...items, {icon: Assets.icons.demo.add, key: 'add', ignore: true, width: 60, onPress: this.onAddItem}];
}
};
return items;
}

toggleItemsCount = () => {
const {fewItems} = this.state;

let items;
if (fewItems) {
items = _.chain(TABS)
.map((tab) => ({label: tab, key: tab}))
.value();
} else {
items = _.chain(TABS)
.take(3)
.map((tab) => ({label: tab, key: tab}))
.value();
}

const items = this.generateTabItems(!fewItems);
this.setState({fewItems: !fewItems, items, key: Date.now()});
};

Expand All @@ -91,8 +89,10 @@ class TabControllerScreen extends Component {
};

toggleCenterSelected = () => {
const {fewItems, centerSelected} = this.state;
this.setState({
centerSelected: !this.state.centerSelected,
items: this.generateTabItems(fewItems, !centerSelected),
centerSelected: !centerSelected,
key: Date.now()
});
};
Expand Down
30 changes: 20 additions & 10 deletions src/components/tabController/TabBarItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,19 @@ export default class TabBarItem extends PureComponent {
onPress: _.noop
};

state = {};
itemWidth = this.props.width;
itemRef = React.createRef();
constructor(props) {
super(props);

this.itemWidth = this.props.width;
this.state = {};
this.itemRef = React.createRef();

if (this.itemWidth) {
const {index, onLayout} = this.props;
onLayout({width: this.itemWidth}, index);
}
}


onStateChange = event([
{
Expand Down Expand Up @@ -148,11 +158,11 @@ export default class TabBarItem extends PureComponent {
opacity
};

// if (this.itemWidth) {
// style.flex = undefined;
// style.width = this.itemWidth;
// style.paddingHorizontal = undefined;
// }
if (this.props.width) {
style.flex = undefined;
style.width = this.itemWidth;
style.paddingHorizontal = undefined;
}

return style;
}
Expand Down Expand Up @@ -234,7 +244,7 @@ export default class TabBarItem extends PureComponent {
onPress={this.onPress}
testID={testID}
>
{icon && <Reanimated.Image source={icon} style={[styles.tabItemIcon, this.getIconStyle()]}/>}
{icon && <Reanimated.Image source={icon} style={[label && styles.tabItemIconWithLabel, this.getIconStyle()]}/>}
{!_.isEmpty(label) && (
<Reanimated.Text style={[styles.tabItemLabel, this.getLabelStyle()]}>
{uppercase ? _.toUpper(label) : label}
Expand All @@ -257,7 +267,7 @@ const styles = StyleSheet.create({
tabItemLabel: {
...Typography.text80
},
tabItemIcon: {
tabItemIconWithLabel: {
marginRight: 10
},
badge: {
Expand Down