Refacto omit to accept multiple keys

pull/2787/head
Jérémy Rialland 2023-08-01 10:17:27 +02:00 committed by Jérémy Rialland
parent cf0750046f
commit 6e2887e3e5
2 changed files with 28 additions and 7 deletions

View File

@ -5,7 +5,7 @@ import {
GenericButtonOrNavLinkProps,
useButtonOrLink,
} from '@/design-system/typography/link'
import { wrapperDebounceEvents } from '@/utils'
import { omit, wrapperDebounceEvents } from '@/utils'
import { FocusStyle } from '../global-style'
@ -41,13 +41,17 @@ export const Button = forwardRef(function Button(
forwardedRef
)
// @ts-ignore
delete buttonOrLinkProps.isDisabled // Remove isDisabled from props casue it's not a valid HTML attribute
// Omit isDisabled and openInSameWindow from props casue it's not a valid HTML attribute
const props = omit(
buttonOrLinkProps as Record<string, unknown>,
'isDisabled',
'openInSameWindow'
)
return (
<StyledButton
// eslint-disable-next-line react/jsx-props-no-spreading
{...buttonOrLinkProps}
{...props}
$size={size}
$light={light}
$color={color}

View File

@ -97,10 +97,27 @@ export function hash(str: string): number {
return hash
}
export function omit<T, K extends keyof T>(obj: T, key: K): Omit<T, K> {
const { [key]: _ignore, ...returnObject } = obj
/**
* Omits the given keys from the object
* @param obj
* @param keys
* @example omit({ a: 1, b: 2, c: 3 }, 'a', 'c') // { b: 2 }
*/
export function omit<T extends object, K extends keyof T>(
obj: T,
...keys: K[]
): Omit<T, K> {
const ret = keys.reduce(
(acc, key) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { [key]: _, ...rest } = acc as T
return returnObject
return rest
},
obj as Omit<T, K>
)
return ret
}
/**