Skip to content

Commit 3269f6b

Browse files
authored
feat: add hide-arrows property (#74)
will be tested after the pre-release by @hasantezcan
1 parent fab8806 commit 3269f6b

File tree

8 files changed

+80
-18
lines changed

8 files changed

+80
-18
lines changed

__tests__/helpers.spec.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,27 @@ describe('helpers', () => {
171171

172172
expect(result).toEqual(expected);
173173
});
174+
175+
it('should return false if hideArrows property is true', async () => {
176+
const itemCount = 10;
177+
const itemsToShow = 3;
178+
const infinite = true;
179+
const current = 1;
180+
const hideArrows = true;
181+
182+
const expected = {
183+
left: false,
184+
right: false,
185+
};
186+
187+
const result = helpers.getShowArrow({
188+
itemCount,
189+
itemsToShow,
190+
infinite,
191+
current,
192+
hideArrows,
193+
});
194+
195+
expect(result).toEqual(expected);
196+
});
174197
});

package-lock.json

Lines changed: 10 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@trendyol-js/react-carousel",
3-
"version": "3.0.0-beta.4",
3+
"version": "3.0.0-beta.5",
44
"description": "Lightweight carousel component for react",
55
"main": "dist/cjs/index.js",
66
"module": "dist/es/index.js",
@@ -46,7 +46,7 @@
4646
"@rollup/plugin-replace": "^2.3.1",
4747
"@testing-library/jest-dom": "^5.3.0",
4848
"@testing-library/react": "^10.0.2",
49-
"@types/jest": "^25.2.1",
49+
"@types/jest": "^25.2.3",
5050
"@types/react": "^16.9.26",
5151
"autoprefixer": "^9.7.5",
5252
"eslint": "^6.8.0",

src/components/carousel/defaultProps.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ export const defaultProps: Required<CarouselProps> = {
2020
autoSwipe: null,
2121
navigation: null,
2222
triggerClickOn: Number.MIN_SAFE_INTEGER,
23+
hideArrows: false,
2324
};

src/components/carousel/index.tsx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,13 @@ export const Carousel: FunctionComponent<CarouselProps> = (userProps: CarouselPr
4343
});
4444
const [current, setCurrent] = useState(0);
4545
const [showArrow, setShowArrow] = useState(
46-
getShowArrow(props.children.length, props.show, props.infinite, current),
46+
getShowArrow({
47+
itemCount: props.children.length,
48+
itemsToShow: props.show,
49+
infinite: props.infinite,
50+
current,
51+
hideArrows: props.hideArrows,
52+
}),
4753
);
4854
const prevChildren = usePrevious<Item[]>(userProps.children);
4955
const [page, setPage] = useState<number>(0);
@@ -149,7 +155,16 @@ export const Carousel: FunctionComponent<CarouselProps> = (userProps: CarouselPr
149155
});
150156

151157
setCurrent(isNavigation && typeof target === 'number' ? target : next);
152-
setShowArrow(getShowArrow(elements.length, props.show, props.infinite, next));
158+
setShowArrow(
159+
getShowArrow({
160+
itemCount: elements.length,
161+
itemsToShow: props.show,
162+
infinite: props.infinite,
163+
current: next,
164+
hideArrows: props.hideArrows,
165+
}),
166+
);
167+
153168
setTimeout(() => {
154169
if (props.infinite) {
155170
const cleanedItems = isNavigation
@@ -288,6 +303,7 @@ export interface CarouselProps {
288303
infinite?: boolean;
289304
className?: string;
290305
useArrowKeys?: boolean;
306+
hideArrows?: boolean;
291307
a11y?: { [key: string]: string };
292308
dynamic?: boolean;
293309
paginationCallback?: ((direction: SlideDirection) => any) | null;

src/helpers/index.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,23 +80,38 @@ export const getCurrent = (
8080
return slideTo;
8181
};
8282

83+
interface GetShowArrowProps {
84+
itemCount: number;
85+
itemsToShow: number;
86+
infinite: boolean;
87+
current: number;
88+
hideArrows: boolean;
89+
}
90+
8391
export const getShowArrow = (
84-
items: number,
85-
show: number,
86-
infinite: boolean,
87-
current: number,
92+
props: GetShowArrowProps,
8893
): { left: boolean; right: boolean } => {
89-
const isItemsMore = items > show;
94+
const { itemCount, itemsToShow, infinite, current, hideArrows = false } = props;
95+
96+
if (hideArrows) {
97+
return {
98+
left: false,
99+
right: false,
100+
};
101+
}
102+
103+
const hasMoreItems = itemCount > itemsToShow;
104+
90105
if (infinite) {
91106
return {
92-
left: isItemsMore,
93-
right: isItemsMore,
107+
left: hasMoreItems,
108+
right: hasMoreItems,
94109
};
95110
}
96111

97112
return {
98-
left: isItemsMore && current !== 0,
99-
right: isItemsMore && current + show < items,
113+
left: hasMoreItems && current !== 0,
114+
right: hasMoreItems && current + itemsToShow < itemCount,
100115
};
101116
};
102117

src/hooks/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const getWindowWidth = () => {
66
}
77

88
return window.innerWidth;
9-
}
9+
};
1010

1111
export const useWindowWidthChange = (callBack: (changed: number) => any) => {
1212
const [windowWidth, setWindowWidth] = useState(getWindowWidth());

website/docs/carousel.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Creates carousel component.
2323
| responsive | boolean | false | false | enables the feature that adjusts items width according to screen size dynamically |
2424
| className | string | false | "" | same as react's className property |
2525
| useArrowKeys | boolean | false | false | enables sliding when press arrow keys |
26+
| hideArrows | boolean | false | false | hide arrow keys |
2627
| a11y | Array | false | {} | accessibility attributes |
2728
| dynamic | Boolean | false | false | if items are stateful, specify this option as true. Also give unique key to each item (like id) |
2829
| rightArrow | ReactElement | false | null | custom right arrow component |

0 commit comments

Comments
 (0)