Skip to content

Commit f8ee503

Browse files
committed
do not allow incorrect indeces
1 parent 7e0e332 commit f8ee503

File tree

4 files changed

+17
-23
lines changed

4 files changed

+17
-23
lines changed

src/hooks/useTagGroup/__tests__/getTagProps.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ describe('getTagProps', () => {
4040
const {result} = renderUseTagGroup()
4141

4242
expect(() =>
43-
result.current.getTagProps({}),
44-
).toThrowErrorMatchingInlineSnapshot(`Pass index to getTagProps!`)
43+
result.current.getTagProps({index: -2}),
44+
).toThrowErrorMatchingInlineSnapshot("Pass correct item index to getTagProps!")
4545
})
4646
})
4747

src/hooks/useTagGroup/__tests__/getTagRemoveProps.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ describe('getTagRemoveProps', () => {
5151
const {result} = renderUseTagGroup()
5252

5353
expect(() =>
54-
result.current.getTagRemoveProps({}),
55-
).toThrowErrorMatchingInlineSnapshot(`Pass index to getTagRemoveProps!`)
54+
result.current.getTagRemoveProps({index: -3}),
55+
).toThrowErrorMatchingInlineSnapshot(`Pass correct item index to getTagRemoveProps!`)
5656
})
5757
})
5858

src/hooks/useTagGroup/index.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,12 @@ const useTagGroup: UseTagGroupInterface = <Item>(
143143

144144
const getTagProps = useCallback(
145145
<Extra extends Record<string, unknown>>(
146-
options?: GetTagPropsOptions & Extra,
146+
options: GetTagPropsOptions & Extra,
147147
) => {
148-
const {
149-
index,
150-
refKey = 'ref',
151-
ref,
152-
onClick,
153-
...rest
154-
} = options ?? ({} as GetTagPropsOptions & Extra)
155-
if (index === undefined) {
156-
throw new Error('Pass index to getTagProps!')
148+
const {index, refKey = 'ref', ref, onClick, ...rest} = options
149+
150+
if (!Number.isInteger(index) || index < 0) {
151+
throw new Error('Pass correct item index to getTagProps!')
157152
}
158153

159154
const latestState = latest.current.state
@@ -183,13 +178,12 @@ const useTagGroup: UseTagGroupInterface = <Item>(
183178

184179
const getTagRemoveProps = useCallback(
185180
<Extra extends Record<string, unknown>>(
186-
options?: GetTagRemovePropsOptions & Extra,
181+
options: GetTagRemovePropsOptions & Extra,
187182
) => {
188-
const {index, onClick, ...rest} =
189-
options ?? ({} as GetTagRemovePropsOptions & Extra)
183+
const {index, onClick, ...rest} = options
190184

191-
if (index === undefined) {
192-
throw new Error('Pass index to getTagRemoveProps!')
185+
if (!Number.isInteger(index) || index < 0) {
186+
throw new Error('Pass correct item index to getTagRemoveProps!')
193187
}
194188

195189
const handleClick = (event: React.MouseEvent) => {

src/hooks/useTagGroup/index.types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export interface UseTagGroupReturnValue<Item> {
5151
}
5252

5353
export interface GetTagPropsOptions extends React.HTMLProps<HTMLElement> {
54-
index?: number
54+
index: number
5555
refKey?: string
5656
ref?: React.MutableRefObject<HTMLElement>
5757
}
@@ -66,7 +66,7 @@ export interface GetTagPropsReturnValue {
6666
}
6767

6868
export interface GetTagRemovePropsOptions extends React.HTMLProps<HTMLElement> {
69-
index?: number
69+
index: number
7070
}
7171

7272
export interface GetTagRemovePropsReturnValue {
@@ -95,11 +95,11 @@ export type GetTagGroupProps = <Extra extends Record<string, unknown> = {}>(
9595
) => GetTagGroupPropsReturnValue & Extra
9696

9797
export type GetTagProps = <Extra extends Record<string, unknown> = {}>(
98-
options?: GetTagPropsOptions & Extra,
98+
options: GetTagPropsOptions & Extra,
9999
) => GetTagPropsReturnValue & Extra
100100

101101
export type GetTagRemoveProps = <Extra extends Record<string, unknown> = {}>(
102-
options?: GetTagRemovePropsOptions & Extra,
102+
options: GetTagRemovePropsOptions & Extra,
103103
) => GetTagRemovePropsReturnValue & Extra
104104

105105
export enum UseTagGroupStateChangeTypes {

0 commit comments

Comments
 (0)