Skip to content

Commit

Permalink
feat: add hide-arrows property (#74)
Browse files Browse the repository at this point in the history
will be tested after the pre-release by @hasantezcan
  • Loading branch information
hasantezcan authored Apr 19, 2022
1 parent fab8806 commit 3269f6b
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 18 deletions.
23 changes: 23 additions & 0 deletions __tests__/helpers.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,27 @@ describe('helpers', () => {

expect(result).toEqual(expected);
});

it('should return false if hideArrows property is true', async () => {
const itemCount = 10;
const itemsToShow = 3;
const infinite = true;
const current = 1;
const hideArrows = true;

const expected = {
left: false,
right: false,
};

const result = helpers.getShowArrow({
itemCount,
itemsToShow,
infinite,
current,
hideArrows,
});

expect(result).toEqual(expected);
});
});
14 changes: 10 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@trendyol-js/react-carousel",
"version": "3.0.0-beta.4",
"version": "3.0.0-beta.5",
"description": "Lightweight carousel component for react",
"main": "dist/cjs/index.js",
"module": "dist/es/index.js",
Expand Down Expand Up @@ -46,7 +46,7 @@
"@rollup/plugin-replace": "^2.3.1",
"@testing-library/jest-dom": "^5.3.0",
"@testing-library/react": "^10.0.2",
"@types/jest": "^25.2.1",
"@types/jest": "^25.2.3",
"@types/react": "^16.9.26",
"autoprefixer": "^9.7.5",
"eslint": "^6.8.0",
Expand Down
1 change: 1 addition & 0 deletions src/components/carousel/defaultProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ export const defaultProps: Required<CarouselProps> = {
autoSwipe: null,
navigation: null,
triggerClickOn: Number.MIN_SAFE_INTEGER,
hideArrows: false,
};
20 changes: 18 additions & 2 deletions src/components/carousel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ export const Carousel: FunctionComponent<CarouselProps> = (userProps: CarouselPr
});
const [current, setCurrent] = useState(0);
const [showArrow, setShowArrow] = useState(
getShowArrow(props.children.length, props.show, props.infinite, current),
getShowArrow({
itemCount: props.children.length,
itemsToShow: props.show,
infinite: props.infinite,
current,
hideArrows: props.hideArrows,
}),
);
const prevChildren = usePrevious<Item[]>(userProps.children);
const [page, setPage] = useState<number>(0);
Expand Down Expand Up @@ -149,7 +155,16 @@ export const Carousel: FunctionComponent<CarouselProps> = (userProps: CarouselPr
});

setCurrent(isNavigation && typeof target === 'number' ? target : next);
setShowArrow(getShowArrow(elements.length, props.show, props.infinite, next));
setShowArrow(
getShowArrow({
itemCount: elements.length,
itemsToShow: props.show,
infinite: props.infinite,
current: next,
hideArrows: props.hideArrows,
}),
);

setTimeout(() => {
if (props.infinite) {
const cleanedItems = isNavigation
Expand Down Expand Up @@ -288,6 +303,7 @@ export interface CarouselProps {
infinite?: boolean;
className?: string;
useArrowKeys?: boolean;
hideArrows?: boolean;
a11y?: { [key: string]: string };
dynamic?: boolean;
paginationCallback?: ((direction: SlideDirection) => any) | null;
Expand Down
33 changes: 24 additions & 9 deletions src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,38 @@ export const getCurrent = (
return slideTo;
};

interface GetShowArrowProps {
itemCount: number;
itemsToShow: number;
infinite: boolean;
current: number;
hideArrows: boolean;
}

export const getShowArrow = (
items: number,
show: number,
infinite: boolean,
current: number,
props: GetShowArrowProps,
): { left: boolean; right: boolean } => {
const isItemsMore = items > show;
const { itemCount, itemsToShow, infinite, current, hideArrows = false } = props;

if (hideArrows) {
return {
left: false,
right: false,
};
}

const hasMoreItems = itemCount > itemsToShow;

if (infinite) {
return {
left: isItemsMore,
right: isItemsMore,
left: hasMoreItems,
right: hasMoreItems,
};
}

return {
left: isItemsMore && current !== 0,
right: isItemsMore && current + show < items,
left: hasMoreItems && current !== 0,
right: hasMoreItems && current + itemsToShow < itemCount,
};
};

Expand Down
2 changes: 1 addition & 1 deletion src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const getWindowWidth = () => {
}

return window.innerWidth;
}
};

export const useWindowWidthChange = (callBack: (changed: number) => any) => {
const [windowWidth, setWindowWidth] = useState(getWindowWidth());
Expand Down
1 change: 1 addition & 0 deletions website/docs/carousel.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Creates carousel component.
| responsive | boolean | false | false | enables the feature that adjusts items width according to screen size dynamically |
| className | string | false | "" | same as react's className property |
| useArrowKeys | boolean | false | false | enables sliding when press arrow keys |
| hideArrows | boolean | false | false | hide arrow keys |
| a11y | Array | false | {} | accessibility attributes |
| dynamic | Boolean | false | false | if items are stateful, specify this option as true. Also give unique key to each item (like id) |
| rightArrow | ReactElement | false | null | custom right arrow component |
Expand Down

0 comments on commit 3269f6b

Please sign in to comment.