Skip to content
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

Added navigation feature #97

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
315 changes: 173 additions & 142 deletions src/components/index.tsx
Original file line number Diff line number Diff line change
@@ -1,152 +1,183 @@
import 'focus-visible';
import React, { useCallback, useEffect, useRef, useState } from 'react';
import { TimelineItemModel } from '../models/TimelineItemModel';
import { TimelineProps } from '../models/TimelineModel';
import { TimelineProps, TTimelineAPI } from '../models/TimelineModel';
import GlobalContextProvider from './GlobalContext';
import Timeline from './timeline/timeline';
const toReactArray = React.Children.toArray;

const Chrono: React.FunctionComponent<Partial<TimelineProps>> = (
props: Partial<TimelineProps>,
) => {
const [timeLineItems, setItems] = useState<TimelineItemModel[]>([]);
const timeLineItemsRef = useRef<TimelineItemModel[]>();
const [slideShowActive, setSlideshowActive] = useState(false);
const [activeTimelineItem, setActiveTimelineItem] = useState(0);

const {
allowDynamicUpdate = false,
children,
items,
onScrollEnd,
slideShow = false,
theme,
} = props;

const customTheme = Object.assign(
{
primary: '#0f52ba',
secondary: '#ffdf00',
cardBgColor: '#fff',
cardForeColor: '#000',
},
theme,
);

const initItems = () => {
debugger;
return items && items.length
? items.map((item, index) => {
return Object.assign({}, item, {
id: Math.random().toString(16).slice(2),
visible: true,
active: index === 0,
});
})
: Array.from({
length: React.Children.toArray(children).filter(
(item) => (item as any).props.className !== 'chrono-icons',
).length,
}).map<Partial<TimelineItemModel>>((item, index) => ({
id: Math.random().toString(16).slice(2),
visible: true,
active: index === 0,
}));
};

useEffect(() => {
const items = initItems();
timeLineItemsRef.current = items;
setItems(items);
}, [JSON.stringify(allowDynamicUpdate ? items : null)]);

const handleTimelineUpdate = useCallback((actvTimelineIndex: number) => {
setItems((items) =>
items.map((item, index) =>
Object.assign({}, item, {
active: index === actvTimelineIndex,
}),
),
);
setActiveTimelineItem(actvTimelineIndex);

if (items) {
if (items.length - 1 === actvTimelineIndex) {
setSlideshowActive(false);
}
const Chrono = React.forwardRef<TTimelineAPI, Partial<TimelineProps>>(
(props, ref) => {
const {
allowDynamicUpdate = false,
children,
items,
onScrollEnd,
slideShow = false,
theme,
initialActiveTimelineItem = 0,
} = props;

const [timeLineItems, setItems] = useState<TimelineItemModel[]>([]);
const timeLineItemsRef = useRef<TimelineItemModel[]>();
const [slideShowActive, setSlideshowActive] = useState(false);
const [activeTimelineItem, setActiveTimelineItem] = useState(0);

const customTheme = Object.assign(
{
primary: '#0f52ba',
secondary: '#ffdf00',
cardBgColor: '#fff',
cardForeColor: '#000',
},
theme,
);

const initItems = () => {
debugger;
return items && items.length
? items.map((item, index) => {
return Object.assign({}, item, {
id: Math.random().toString(16).slice(2),
visible: true,
active: index === 0,
});
})
: Array.from({
length: React.Children.toArray(children).filter(
(item) => (item as any).props.className !== 'chrono-icons',
).length,
}).map<Partial<TimelineItemModel>>((item, index) => ({
id: Math.random().toString(16).slice(2),
visible: true,
active: index === 0,
}));
};

useEffect(() => {
const items = initItems();
timeLineItemsRef.current = items;
setItems(items);
}, [JSON.stringify(allowDynamicUpdate ? items : null)]);

const handleTimelineUpdate = useCallback((actvTimelineIndex: number) => {
setItems((items) =>
items.map((item, index) =>
Object.assign({}, item, {
active: index === actvTimelineIndex,
}),
),
);
setActiveTimelineItem(actvTimelineIndex);

if (items) {
if (items.length - 1 === actvTimelineIndex) {
setSlideshowActive(false);
}
}
}, []);

// *** begin navigation patch

function navigateTo(index: number): void {
if (index >= 0 && Array.isArray(items) && index < items.length) {
handleTimelineUpdate(index);
}
}

useEffect(
() => {
if (initialActiveTimelineItem > 0) {
navigateTo(initialActiveTimelineItem);
}
},
[]
)

if ((ref as any).hasOwnProperty('current')) {
(ref as any).current = {
navigateTo,
}
}
else if (typeof ref === "function") {
ref({
navigateTo,
})
}

// *** end navigation patch

const restartSlideShow = useCallback(() => {
setSlideshowActive(true);
handleTimelineUpdate(0);
}, []);

const handleOnNext = () => {
if (!timeLineItems.length) {
return;
}
if (activeTimelineItem < timeLineItems.length - 1) {
const newTimeLineItem = activeTimelineItem + 1;

handleTimelineUpdate(newTimeLineItem);
setActiveTimelineItem(newTimeLineItem);
}
};

const handleOnPrevious = () => {
if (activeTimelineItem > 0) {
const newTimeLineItem = activeTimelineItem - 1;

handleTimelineUpdate(newTimeLineItem);
setActiveTimelineItem(newTimeLineItem);
}
};

const handleFirst = () => {
setActiveTimelineItem(0);
handleTimelineUpdate(0);
};

const handleLast = () => {
if (timeLineItems.length) {
const idx = timeLineItems.length - 1;
setActiveTimelineItem(idx);
handleTimelineUpdate(idx);
}
};

let iconChildren = toReactArray(children).filter(
(item) => (item as any).props.className === 'chrono-icons',
);

if (iconChildren.length) {
iconChildren = (iconChildren[0] as any).props.children;
}

return (
<GlobalContextProvider {...props}>
<Timeline
activeTimelineItem={activeTimelineItem}
contentDetailsChildren={toReactArray(children).filter(
(item) => (item as any).props.className !== 'chrono-icons',
)}
iconChildren={iconChildren}
items={timeLineItems}
onFirst={handleFirst}
onLast={handleLast}
onNext={handleOnNext}
onPrevious={handleOnPrevious}
onRestartSlideshow={restartSlideShow}
onTimelineUpdated={useCallback(handleTimelineUpdate, [])}
slideShow={slideShow}
slideShowEnabled={slideShow}
slideShowRunning={slideShowActive}
theme={customTheme}
onScrollEnd={onScrollEnd}
/>
</GlobalContextProvider>
);
}
}, []);

const restartSlideShow = useCallback(() => {
setSlideshowActive(true);
handleTimelineUpdate(0);
}, []);

const handleOnNext = () => {
if (!timeLineItems.length) {
return;
}
if (activeTimelineItem < timeLineItems.length - 1) {
const newTimeLineItem = activeTimelineItem + 1;

handleTimelineUpdate(newTimeLineItem);
setActiveTimelineItem(newTimeLineItem);
}
};

const handleOnPrevious = () => {
if (activeTimelineItem > 0) {
const newTimeLineItem = activeTimelineItem - 1;

handleTimelineUpdate(newTimeLineItem);
setActiveTimelineItem(newTimeLineItem);
}
};

const handleFirst = () => {
setActiveTimelineItem(0);
handleTimelineUpdate(0);
};

const handleLast = () => {
if (timeLineItems.length) {
const idx = timeLineItems.length - 1;
setActiveTimelineItem(idx);
handleTimelineUpdate(idx);
}
};

let iconChildren = toReactArray(children).filter(
(item) => (item as any).props.className === 'chrono-icons',
);

if (iconChildren.length) {
iconChildren = (iconChildren[0] as any).props.children;
}

return (
<GlobalContextProvider {...props}>
<Timeline
activeTimelineItem={activeTimelineItem}
contentDetailsChildren={toReactArray(children).filter(
(item) => (item as any).props.className !== 'chrono-icons',
)}
iconChildren={iconChildren}
items={timeLineItems}
onFirst={handleFirst}
onLast={handleLast}
onNext={handleOnNext}
onPrevious={handleOnPrevious}
onRestartSlideshow={restartSlideShow}
onTimelineUpdated={useCallback(handleTimelineUpdate, [])}
slideShow={slideShow}
slideShowEnabled={slideShow}
slideShowRunning={slideShowActive}
theme={customTheme}
onScrollEnd={onScrollEnd}
/>
</GlobalContextProvider>
);
};
);

export default Chrono;
10 changes: 10 additions & 0 deletions src/demo/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TimelineItemModel } from '../models/TimelineItemModel';
import {
HorizontalBasic,
VerticalBasic,
VerticalBasicNavigation,
VerticalCustomContent,


Expand Down Expand Up @@ -45,6 +46,9 @@ const NewDemo: React.FunctionComponent = () => {
<li>
<Link to="/vertical-basic">Vertical Basic</Link>
</li>
<li>
<Link to="/vertical-basic-navigation">Vertical Basic navigation</Link>
</li>
<li>
<Link to="/vertical-alternating">Vertical Alternating</Link>
</li>
Expand All @@ -70,6 +74,12 @@ const NewDemo: React.FunctionComponent = () => {
<VerticalBasic type={"big-screen"} items={items} />
)}
</Route>
<Route path="/vertical-basic-navigation">
{/* Vertical with no Media */}
{items.length > 0 && (
<VerticalBasicNavigation type={"big-screen"} items={items} />
)}
</Route>
<Route path="/vertical-alternating-mixed">
{items.length > 0 && <VerticalTreeMixed type={"big-screen"} />}
</Route>
Expand Down
Loading