forked from elastic/eui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding href prop to EuiTab (elastic#2275)
* Tab comp in typescript, href prop and example * Improving code based on comments * Tab comp in typescript, href prop and example * Improving code based on comments * Updating changelog * typescripting * Adding a snapshot test for when href is in use.
- Loading branch information
1 parent
77eea67
commit cf47288
Showing
9 changed files
with
119 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 19 additions & 1 deletion
20
...nents/tabs/__snapshots__/tab.test.js.snap → ...ents/tabs/__snapshots__/tab.test.tsx.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import React, { | ||
MouseEventHandler, | ||
AnchorHTMLAttributes, | ||
ButtonHTMLAttributes, | ||
FunctionComponent, | ||
} from 'react'; | ||
import classNames from 'classnames'; | ||
import { CommonProps, ExclusiveUnion } from '../common'; | ||
import { getSecureRelForTarget } from '../../services'; | ||
|
||
export interface EuiTabProps extends CommonProps { | ||
isSelected?: boolean; | ||
disabled?: boolean; | ||
} | ||
|
||
type EuiTabPropsForAnchor = EuiTabProps & | ||
AnchorHTMLAttributes<HTMLAnchorElement> & { | ||
href?: string; | ||
onClick?: MouseEventHandler<HTMLAnchorElement>; | ||
}; | ||
|
||
type EuiTabPropsForButton = EuiTabProps & | ||
ButtonHTMLAttributes<HTMLButtonElement> & { | ||
onClick?: MouseEventHandler<HTMLButtonElement>; | ||
}; | ||
|
||
export type Props = ExclusiveUnion<EuiTabPropsForAnchor, EuiTabPropsForButton>; | ||
|
||
export const EuiTab: FunctionComponent<Props> = ({ | ||
isSelected, | ||
children, | ||
className, | ||
disabled, | ||
href, | ||
target, | ||
rel, | ||
...rest | ||
}) => { | ||
const classes = classNames('euiTab', className, { | ||
'euiTab-isSelected': isSelected, | ||
'euiTab-isDisabled': disabled, | ||
}); | ||
|
||
// <a> elements don't respect the `disabled` attribute. So if we're disabled, we'll just pretend | ||
// this is a button and piggyback off its disabled styles. | ||
if (href && !disabled) { | ||
const secureRel = getSecureRelForTarget({ href, target, rel }); | ||
|
||
return ( | ||
<a | ||
role="tab" | ||
aria-selected={!!isSelected} | ||
className={classes} | ||
href={href} | ||
target={target} | ||
rel={secureRel} | ||
{...rest as AnchorHTMLAttributes<HTMLAnchorElement>}> | ||
<span className="euiTab__content">{children}</span> | ||
</a> | ||
); | ||
} | ||
|
||
return ( | ||
<button | ||
role="tab" | ||
aria-selected={!!isSelected} | ||
type="button" | ||
className={classes} | ||
disabled={disabled} | ||
{...rest as ButtonHTMLAttributes<HTMLButtonElement>}> | ||
<span className="euiTab__content">{children}</span> | ||
</button> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters