1- import React , { useEffect , useState } from 'react' ;
1+ import React , { useCallback , useEffect , useState } from 'react' ;
22import './NavigationPills.css' ;
33import { INavigationPills } from './INavigationPills' ;
4- import { Link , useLocation } from 'react-router-dom' ;
4+ import { useLocation } from 'react-router-dom' ;
5+ import Actions from '../../action/Actions' ;
6+ import { Button } from '../../button/Button' ;
57
68const NavigationPills : React . FC < INavigationPills > = ( {
7- items,
89 components,
9- dropdownItems,
1010 isDisable,
1111} ) => {
12- const [ activeIndex , setActiveIndex ] = useState < number > ( 0 ) ;
12+ const [ activeTabKey , setactiveTabKey ] = useState < string > ( components [ 0 ] . value ) ;
1313
1414 const location = useLocation ( ) ;
1515
1616 useEffect ( ( ) => {
1717 if ( location ?. search !== '' ) {
18- const componentIndex = components . findIndex (
19- ( item : any ) => item . key === location ?. search . replace ( '?' , '' ) ,
18+ const component = components . find (
19+ ( item : any ) => item . value === location ?. search . replace ( '?' , '' ) ,
2020 ) ;
2121
22- if ( componentIndex > - 1 ) {
23- handlePillClick ( componentIndex ) ;
22+ if ( component !== null ) {
23+ handlePillClick ( component . value ) ;
2424 }
2525 }
26- } , [ location ] ) ;
26+ } , [ location , components ] ) ;
2727
28- const handlePillClick = ( index : number ) => {
29- setActiveIndex ( index ) ;
28+ const handlePillClick = ( tabKey : string ) => {
29+ setactiveTabKey ( tabKey ) ;
30+ } ;
31+
32+ const getCurrentElementIndex = useCallback ( ( ) => {
33+ const currentComponentIndex = components . findIndex (
34+ ( tab : any ) => tab . value === activeTabKey ,
35+ ) ;
36+
37+ return currentComponentIndex ;
38+ } , [ components , activeTabKey ] ) ;
39+
40+ const isActiveTabFirstPosition = ( ) => {
41+ return getCurrentElementIndex ( ) === 0 ;
42+ } ;
43+
44+ const isActiveTabLastPosition = ( ) => {
45+ return getCurrentElementIndex ( ) + 1 === components . length ;
46+ } ;
47+
48+ const getNextElement = ( ) => {
49+ const currentComponentindex = getCurrentElementIndex ( ) ;
50+ return components [ currentComponentindex + 1 ] . value ;
51+ } ;
52+
53+ const getPreviousElement = ( ) => {
54+ const currentComponentindex = getCurrentElementIndex ( ) ;
55+ return components [ currentComponentindex - 1 ] . value ;
3056 } ;
3157
3258 return (
3359 < div className = "pt-5" >
34- < div className = "d-flex d-xxl-flex d-xl-flex gap-2 d-none " >
35- { items . map ( ( item , index ) => (
36- < Link
37- key = { index }
38- to = "#"
39- className = { `d-flex gap-2 custom-nav-pill ${ isDisable ? 'disabled default' : index === activeIndex ? 'active ' : 'enabled' } ` }
40- onClick = { isDisable ? ( ) => { } : ( ) => handlePillClick ( index ) }
60+ < div className = "d-flex d-xxl-flex d-xl-flex gap-2 d-none" >
61+ { components . map ( ( item : any ) => (
62+ < Button
63+ size = "small"
64+ disabled = { isDisable && item !== activeTabKey }
65+ variant = { item . value === activeTabKey ? 'primary ' : 'tertiary' }
66+ onClick = { ( ) => handlePillClick ( item . value ) }
4167 >
42- { item }
43- </ Link >
68+ { item . label }
69+ </ Button >
4470 ) ) }
4571 </ div >
46- < div className = "d-flex d-xl-none d-lg-flex d-md-flex d-sm-flex d-xs-flex justify-content-between px-2 align-items-center w-100" >
72+ < div className = "d-flex d-xl-none d-lg-flex d-md-flex d-sm-flex d-xs-flex justify-content-between align-items-center w-100" >
4773 < div className = "d-flex justify-content-between w-100 flex-column flex-sm-row" >
74+ < div >
75+ < Actions
76+ label = "Select Page"
77+ items = { components }
78+ onItemClick = {
79+ isDisable ? ( ) => { } : ( value ) => handlePillClick ( value )
80+ }
81+ customCssToggleBtn = { 'custom-nav-btn' }
82+ customCssMenu = { 'custom-nav-action-menu' }
83+ disable = { isDisable }
84+ toggleButtonVariant = { 'secondary' }
85+ // toggleButtonSize={isMobileScreen ? 'medium' : 'small'}
86+ />
87+ </ div >
4888 < div >
4989 < div className = "d-flex align-items-center" >
5090 < div className = "m-0" >
5191 < span
52- className = { `custom-nav-carousel-left-icon ${ activeIndex === 0 ? 'd-none' : '' } ` }
92+ className = { `custom-nav-carousel-left-icon ${ isActiveTabFirstPosition ( ) ? 'd-none' : '' } ` }
5393 aria-hidden = "true"
5494 onClick = {
5595 isDisable
56- ? ( ) => { }
96+ ? ( ) => { }
5797 : ( ) =>
58- activeIndex > 0 && handlePillClick ( activeIndex - 1 )
98+ ! isActiveTabFirstPosition ( ) &&
99+ handlePillClick ( getPreviousElement ( ) )
59100 }
60101 > </ span >
61102 </ div >
62103 < div className = "ps-3 pe-2 m-0 p-0 w-100 text-center" >
63- { items . map ( ( item , index ) => (
64- < div
65- key = { index }
66- className = { `custom-nav-pill ${ index === activeIndex ? 'd-block active' : 'd-none' } ` }
67- >
68- { item }
69- </ div >
70- ) ) }
104+ { components . map (
105+ ( tab : any ) =>
106+ tab . value === activeTabKey && (
107+ < Button
108+ // size={isMobileScreen ? 'medium' : 'small'}
109+ className = "custom-nav-pill"
110+ >
111+ { tab . label }
112+ </ Button >
113+ ) ,
114+ ) }
71115 </ div >
72116 < div className = "m-0" >
73117 < span
74- className = { `custom-nav-carousel-right-icon m-0 ${ activeIndex === items . length - 1 ? 'd-none' : '' } ` }
118+ className = { `custom-nav-carousel-right-icon m-0 ${ isActiveTabLastPosition ( ) ? 'd-none' : '' } ` }
75119 aria-hidden = "true"
76120 onClick = {
77121 isDisable
78- ? ( ) => { }
122+ ? ( ) => { }
79123 : ( ) =>
80- activeIndex < items . length - 1 &&
81- handlePillClick ( activeIndex + 1 )
124+ ! isActiveTabLastPosition ( ) &&
125+ handlePillClick ( getNextElement ( ) )
82126 }
83127 > </ span >
84128 </ div >
@@ -88,11 +132,12 @@ const NavigationPills: React.FC<INavigationPills> = ({
88132 </ div >
89133 < div className = "mt-4" >
90134 { components &&
91- components ?. map ( ( tabComponent : any , index : number ) =>
92- index === activeIndex ? (
135+ activeTabKey !== '' &&
136+ components ?. map ( ( tabComponent : { value : string , component : React . ReactNode } , index : number ) => {
137+ return tabComponent . value === activeTabKey ? (
93138 < div key = { index } > { tabComponent . component } </ div >
94- ) : null ,
95- ) }
139+ ) : null ;
140+ } ) }
96141 </ div >
97142 </ div >
98143 ) ;
0 commit comments