HomeOur TeamContact

Segmented control tab

By Utkarsh Lubal
May 06, 2023
1 min read
Segmented control tab

Table Of Contents

01
Introduction
02
Installation
03
Example
04
Tutorial

Introduction

A react native component with the same concept of react native’s SegmantedControlIOS, Primarily built to support both IOS and Android. 💡

Props

NameDescriptionDefaultType
valuestitles of tabs['One', 'Two', 'Three']array
selectedIndexindex of tab item to be selected initially[0]number
selectedIndicesArray of indices of tab items to be selected initially - when multiple prop is true else it will take selectedIndex[0]arrayOf(PropTypes.number)
enabledBoolean to enable or disable the componenttruebool
multipleBoolean which enables the multiple selection optionfalsebool
borderRadiusborderRadius of whole tab5number
tabsContainerStyleexternal styles can be passed to override the default styles of the segmentedControl wrapperbase styles added in SegmentedControlTab.jsobject(styles)
tabsContainerDisableStyleCustom style that can be passed when enable is set to falsedefault style opacity: 0.6object(styles)
tabStyleexternal styles can be passed to override the default styles of the tabsbase styles added in SegmentedControlTab.jsobject(styles)
firstTabStyleexternal styles can be passed to override the default styles of the first tabbase styles added in SegmentedControlTab.jsobject(styles)
lastTabStyleexternal styles can be passed to override the default styles of the last tabbase styles added in SegmentedControlTab.jsobject(styles)
tabTextStyleexternal styles can be passed to override the default styles of the tab titlebase styles added in SegmentedControlTab.jsobject(styles)
activeTabStyleexternal styles can be passed to override the default styles of the active tabbase styles added in SegmentedControlTab.jsobject(styles)
activeTabTextStyleexternal styles can be passed to override the default styles of the active tab textbase styles added in SegmentedControlTab.jsobject(styles)
badgesbadges values to display[1, 2, 3]array
tabBadgeContainerStyleexternal style can be passed to override the default style of the badge containerbase styles added in SegmentedControlTab.jsobject(styles)
activeTabBadgeContainerStyleexternal style can be passed to override the default style of the active badge containerbase styles added in SegmentedControlTab.jsobject(styles)
tabBadgeStyleexternal style can be passed to override the default style of the badge textbase styles added in SegmentedControlTab.jsobject(styles)
activeTabBadgeStyleexternal style can be passed to override the default style of the active badge textbase styles added in SegmentedControlTab.jsobject(styles)
onTabPresscall-back function when a tab is selected() => {}func
allowFontScalingwhether the segment & badge text should allow font scaling (default matches React Native default)truebool
accessibleenables accessibility for each tabtruebool
accessibilityLabelsReads out the given text on each tab press when voice over is enabled. If not set, uses the text passed in as values in props as a fallback[‘Label 1’, ‘Label 2’, ‘Label 3’]array
activeTabOpacityOpacity value to customize tab press1number

Installation

npm install react-native-segmented-control-tab –save

Example

import React, { Component } from 'react'
import {
  StyleSheet,
  Text,
  View,
} from 'react-native'
import SegmentedControlTab from 'react-native-segmented-control-tab'

class SegmentedView extends Component<*, *> {
  constructor() {
    super()
    this.state = {
      selectedIndex: 0,
      selectedIndices: [0],
      customStyleIndex: 0,
    }
  }

    handleSingleIndexSelect = (index: number) => {
      this.setState(prevState => ({ ...prevState, selectedIndex: index }))
    }

    handleMultipleIndexSelect = (index: number) => {
      const { selectedIndices } = this.state

      if (selectedIndices.includes(index)) {
        this.setState(prevState => ({
          ...prevState,
          selectedIndices: selectedIndices.filter((i) => i !== index),
        }))
      } else {
        this.setState(prevState => ({
          ...prevState,
          selectedIndices: [
            ...selectedIndices,
            index,
          ],
        }))
      }
    }

    handleCustomIndexSelect = (index: number) => {
      this.setState(prevState => ({ ...prevState, customStyleIndex: index }))
    }

    render() {
      const { selectedIndex, selectedIndices, customStyleIndex } = this.state
      return (
        <View style={styles.container}>
          <Text style={styles.headerText}>Default segmented control with single selection</Text>
          <SegmentedControlTab
            selectedIndex={selectedIndex}
            tabStyle={styles.tabStyle}
            activeTabStyle={styles.activeTabStyle}
            onTabPress={this.handleSingleIndexSelect}
          />
          <View style={styles.Seperator} />
          <Text style={styles.headerText}>Default segmented control with multiple selection</Text>
          <SegmentedControlTab
            multiple
            selectedIndices={selectedIndices}
            onTabPress={this.handleMultipleIndexSelect}
          />
          <View style={styles.Seperator} />
          <Text style={styles.headerText}>Default segmented with badges</Text>
          <SegmentedControlTab
            badges={[1, 2, 3]}
            selectedIndex={selectedIndex}
            onTabPress={this.handleSingleIndexSelect}
          />
          <View style={styles.Seperator} />
          <Text style={styles.headerText}>Custom segmented control with custom styles</Text>
          <SegmentedControlTab
            values={['one', 'two']}
            selectedIndex={customStyleIndex}
            onTabPress={this.handleCustomIndexSelect}
            borderRadius={0}
            tabsContainerStyle={{ height: 50, backgroundColor: '#F2F2F2' }}
            tabStyle={{ backgroundColor: '#F2F2F2', borderWidth: 0, borderColor: 'transparent' }}
            activeTabStyle={{ backgroundColor: 'white', marginTop: 2 }}
            tabTextStyle={{ color: '#444444', fontWeight: 'bold' }}
            activeTabTextStyle={{ color: '#888888' }}
          />
          {customStyleIndex === 0
                    && <Text style={styles.tabContent}> Tab one</Text>}
          {customStyleIndex === 1
                    && <Text style={styles.tabContent}> Tab two</Text>}

        </View>
      )
    }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: 'white',
    padding: 10,
  },
  headerText: {
    padding: 8,
    fontSize: 14,
    color: '#444444',
  },
  tabContent: {
    color: '#444444',
    fontSize: 18,
    margin: 24,
  },
  Seperator: {
    marginHorizontal: -10,
    alignSelf: 'stretch',
    borderTopWidth: 1,
    borderTopColor: '#888888',
    marginTop: 24,
  },
  tabStyle: {
    borderColor: '#D52C43',
  },
  activeTabStyle: {
    backgroundColor: '#D52C43',
  },

})

export default SegmentedView;

Tutorial

Coming Soon…


Previous Article
Swiper Animated
Utkarsh Lubal

Utkarsh Lubal

Full Stack Developer

Quick Links

Advertise with usAbout UsContact Us

Social Media