Skip to content
New issue

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

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

Already on GitHub? # to your account

Convert Button components to functional components and expose refs #317

Merged
merged 3 commits into from
Sep 30, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### 0.0.33

- [#317]https://github.com/influxdata/clockface/pull/317): Convert `Button` component family to `FunctionComponent` and wrap with `forwardRef`
- [#316]https://github.com/influxdata/clockface/pull/316): Convert `Popover` component family to `FunctionComponent` and wrap with `forwardRef`
- [#316]https://github.com/influxdata/clockface/pull/316): Convert `AppWrapper` component family to `FunctionComponent` and wrap with `forwardRef`
- [#316]https://github.com/influxdata/clockface/pull/316): Convert `Alert` component family to `FunctionComponent` and wrap with `forwardRef`
Expand Down
92 changes: 41 additions & 51 deletions src/Components/Button/Base/ButtonBase.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Libraries
import React, {Component, MouseEvent, RefObject} from 'react'
import React, {forwardRef, MouseEvent} from 'react'
import classnames from 'classnames'

// Styles
Expand All @@ -12,10 +12,10 @@ import {
ComponentSize,
ButtonShape,
ButtonType,
StandardClassProps,
StandardFunctionProps,
} from '../../../Types'

interface Props extends StandardClassProps {
interface ButtonBaseProps extends StandardFunctionProps {
/** Function to be called on button click */
onClick?: (e?: MouseEvent<HTMLButtonElement>) => void
/** Text to be displayed on hover tooltip */
Expand All @@ -34,72 +34,62 @@ interface Props extends StandardClassProps {
active: boolean
/** Button type of 'button' or 'submit' */
type: ButtonType
/** React Ref object */
refObject?: RefObject<HTMLButtonElement>
}

export class ButtonBase extends Component<Props> {
public static readonly displayName = 'ButtonBase'
export type ButtonBaseRef = HTMLButtonElement

public static defaultProps = {
color: ComponentColor.Default,
size: ComponentSize.Small,
shape: ButtonShape.Default,
status: ComponentStatus.Default,
active: false,
type: ButtonType.Button,
testID: 'button-base',
}

public render() {
const {
onClick,
titleText,
tabIndex,
type,
testID,
children,
refObject,
export const ButtonBase = forwardRef<ButtonBaseRef, ButtonBaseProps>(
(
{
id,
style,
} = this.props
onClick,
children,
tabIndex,
titleText,
className,
active = false,
testID = 'button-base',
type = ButtonType.Button,
size = ComponentSize.Small,
shape = ButtonShape.Default,
color = ComponentColor.Default,
status = ComponentStatus.Default,
},
ref
) => {
const disabled =
status === ComponentStatus.Disabled || status === ComponentStatus.Loading

const buttonBaseClass = classnames(
`cf-button cf-button-${size} cf-button-${color}`,
{
'cf-button-square': shape === ButtonShape.Square,
'cf-button-stretch': shape === ButtonShape.StretchToFit,
'cf-button--loading': status === ComponentStatus.Loading,
'cf-button--disabled': status === ComponentStatus.Disabled,
active,
[`${className}`]: className,
}
)

return (
<button
className={this.className}
disabled={this.disabled}
className={buttonBaseClass}
disabled={disabled}
onClick={onClick}
title={titleText}
tabIndex={!!tabIndex ? tabIndex : 0}
type={type}
data-testid={testID}
id={id}
style={style}
ref={refObject}
ref={ref}
>
{children}
</button>
)
}
)

private get disabled(): boolean {
const {status} = this.props

return (
status === ComponentStatus.Disabled || status === ComponentStatus.Loading
)
}

private get className(): string {
const {color, size, shape, status, active, className} = this.props

return classnames(`cf-button cf-button-${size} cf-button-${color}`, {
'cf-button-square': shape === ButtonShape.Square,
'cf-button-stretch': shape === ButtonShape.StretchToFit,
'cf-button--loading': status === ComponentStatus.Loading,
'cf-button--disabled': status === ComponentStatus.Disabled,
active,
[`${className}`]: className,
})
}
}
ButtonBase.displayName = 'ButtonBase'
249 changes: 0 additions & 249 deletions src/Components/Button/Button.stories.tsx

This file was deleted.

Loading