Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Breadcrumbs Types #700

Merged
merged 8 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// @ts-nocheck
import React, { forwardRef } from 'react'
import PropTypes from 'prop-types'
import styled, { css } from 'styled-components'
import { Typography } from '../Typography'
import { Tooltip } from '../Tooltip'
import { breadcrumbs as tokens } from './Breadcrumbs.tokens'

const StyleTypography = styled(Typography)`
type StyledProps = Pick<Props, 'maxWidth'>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pick is one of those things I didn’t know about, that we should probably use in more components…

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommend having a look at the Typescript utility types :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's pretty neat! 😄 @mimarz also taught me Omit<Type, Keys> from Typescript Utility Types where you can remove Keys from Type and re-use it


const StyledTypography = styled(Typography)<StyledProps>`
&:hover {
text-decoration: underline;
color: ${tokens.colors.hover};
Expand All @@ -27,13 +27,25 @@ const StyleTypography = styled(Typography)`
${({ maxWidth }) => css({ maxWidth })}
`

export const Breadcrumb = forwardRef(function Breadcrumb(
{ className, children, maxWidth, ...rest },
type Props = {
/*
* Max label width in pixels,
* truncate long labels based on this width */
maxWidth?: number
/* click handler function */
onClick?: () => void
/** Children is breadcrumb text */
children: string
/** Classname */
className?: string
}

export const Breadcrumb = forwardRef<HTMLDivElement, Props>(function Breadcrumb(
{ children, maxWidth, ...other },
ref,
) {
const props = {
...rest,
className,
...other,
ref,
maxWidth: maxWidth,
}
Expand All @@ -42,39 +54,19 @@ export const Breadcrumb = forwardRef(function Breadcrumb(

const WithTooltip = (
<Tooltip title={children}>
<StyleTypography link variant="body_short" {...props}>
<StyledTypography link variant="body_short" {...props}>
{children}
</StyleTypography>
</StyledTypography>
</Tooltip>
)

return tooltip ? (
WithTooltip
) : (
<StyleTypography link variant="body_short" {...props}>
<StyledTypography link variant="body_short" {...props}>
{children}
</StyleTypography>
</StyledTypography>
)
})

Breadcrumb.displayName = 'eds-breadcrumb'

Breadcrumb.propTypes = {
/*
* Max label width in pixels,
* truncate long labels based on this width
*/
maxWidth: PropTypes.number,
// click handler function
onClick: PropTypes.func,
// Breadcrumb children
children: PropTypes.node.isRequired,
/** @ignore */
className: PropTypes.string,
}

Breadcrumb.defaultProps = {
maxWidth: undefined,
className: '',
onClick: () => {},
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @ts-nocheck
import { tokens } from '@equinor/eds-tokens'

const {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// @ts-nocheck
import React, { forwardRef, useState, Fragment } from 'react'
import PropTypes from 'prop-types'
import styled from 'styled-components'
import { breadcrumbs as tokens } from './Breadcrumbs.tokens'
import { Typography } from '../Typography'
Expand Down Expand Up @@ -38,13 +36,20 @@ const Collapsed = styled(Typography)`
text-decoration: none;
`

export const Breadcrumbs = forwardRef(function Breadcrumbs(
{ className, children, collapse, ...rest },
type Props = {
/*
* Collapses the list of breadcrumbs so that only the first
* and last breadcrumb will be shown, with an ellipsis in between.
*/
collapse?: boolean
} & JSX.IntrinsicElements['nav']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also something we do differently from component to component… React.HTMLAttributes<HTMLNavElement> vs. JSX.IntrinsicElements['nav']

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mimarz and I discussed this yesterday, if I remember correctly the difference was that JSX.IntrinsicElements['nav'] included more props?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vnys Funfact, nav only uses the HTMLElement dom interface

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vnys Funfact, nav only uses the HTMLElement dom interface

Noted 😉


export const Breadcrumbs = forwardRef<HTMLElement, Props>(function Breadcrumbs(
{ children, collapse, ...rest },
ref,
) {
const props = {
...rest,
className,
ref,
}

Expand Down Expand Up @@ -103,20 +108,3 @@ export const Breadcrumbs = forwardRef(function Breadcrumbs(
})

Breadcrumbs.displayName = 'eds-breadcrumbs'

Breadcrumbs.propTypes = {
/*
* Collapses the list of breadcrumbs so that only the first
* and last breadcrumb will be shown, with an ellipsis in between.
*/
collapse: PropTypes.bool,
// Breadcrumbs children
children: PropTypes.node.isRequired,
/** @ignore */
className: PropTypes.string,
}

Breadcrumbs.defaultProps = {
className: '',
collapse: false,
}
7 changes: 0 additions & 7 deletions libraries/core-react/src/Breadcrumbs/index.js

This file was deleted.

12 changes: 12 additions & 0 deletions libraries/core-react/src/Breadcrumbs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Breadcrumbs as BaseComponent } from './Breadcrumbs'
import { Breadcrumb } from './Breadcrumb'

type BreadcrumbsTypes = typeof BaseComponent & {
Breadcrumb: typeof Breadcrumb
}

const Breadcrumbs = BaseComponent as BreadcrumbsTypes

Breadcrumbs.Breadcrumb = Breadcrumb

export { Breadcrumbs }