Skip to content

Commit

Permalink
[Play-528] Typescript Conversion Stat Change (#2293)
Browse files Browse the repository at this point in the history
* converted stat change to typescript

* added jest tests

* removed @flow from top of file
  • Loading branch information
Israel-Molestina authored Feb 7, 2023
1 parent 556468c commit 302c643
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 63 deletions.
63 changes: 0 additions & 63 deletions playbook/app/pb_kits/playbook/pb_stat_change/_stat_change.jsx

This file was deleted.

66 changes: 66 additions & 0 deletions playbook/app/pb_kits/playbook/pb_stat_change/_stat_change.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import React from 'react'
import classnames from 'classnames'

import { buildCss } from '../utilities/props'
import { globalProps } from '../utilities/globalProps'

import Body from '../pb_body/_body'
import Icon from '../pb_icon/_icon'

const statusMap: {neutral: 'neutral', decrease: 'negative' ,increase: 'positive'} = {
increase: 'positive',
decrease: 'negative',
neutral: 'neutral',
}

const iconMap = {
increase: 'arrow-up',
decrease: 'arrow-down',
}

type StatChangeProps = {
change?: 'increase' | 'decrease' | 'neutral',
className?: string,
icon?: string,
id?: string,
value?: string | number,
}

const StatChange = (props: StatChangeProps): React.ReactElement => {
const { change = 'neutral', className, icon, id, value } = props
const status = statusMap[change as keyof typeof statusMap]
let returnedIcon = iconMap[change as keyof typeof iconMap]
if (icon) {
returnedIcon = icon
}

return (
<>
{value &&
<div
className={classnames(
buildCss('pb_stat_change_kit', status),
globalProps(props),
className
)}
id={id}
>
<Body status={status}>
{returnedIcon &&
<>
<Icon
fixed_width
icon={returnedIcon}
/>
{' '}
</>
}
{`${value}%`}
</Body>
</div>
}
</>
)
}

export default StatChange
41 changes: 41 additions & 0 deletions playbook/app/pb_kits/playbook/pb_stat_change/stat_change.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React from 'react'
import { render, screen } from '../utilities/test-utils'

import StatChange from './_stat_change'

test('it renders each status', () => {
render(
<StatChange
change="increase"
value="28.4"
/>
)

const kit = screen.getByText('28.4%')
expect(kit).toHaveClass(`pb_body_kit_positive`)
})

test('it renders preset icon', () => {
render(
<StatChange
change="increase"
value="28.4"
/>
)

const kit = screen.getByLabelText('arrow-up icon')
expect(kit).toBeTruthy
})

test('it renders custom icon', () => {
render(
<StatChange
icon="chart-line-down"
value={6.1}
/>

)

const kit = screen.getByLabelText('chart-line-down icon')
expect(kit).toBeTruthy
})

0 comments on commit 302c643

Please # to comment.