-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ColorIndicator: Convert component to TypeScript (#41587)
* ColorIndicator: Convert component to TypeScript * Update CHANGELOG.md * Apply suggestions from code review Co-authored-by: Lena Morita <lena@jaguchi.com> * Clearify colorValue in readme file * Remove unused exclude from story * Add forwardref * Update CHANGELOG.md * Remove export * Update CHANGELOG.md Co-authored-by: Lena Morita <lena@jaguchi.com>
- Loading branch information
Showing
10 changed files
with
123 additions
and
65 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
import type { ForwardedRef } from 'react'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { forwardRef } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { WordPressComponentProps } from '../ui/context'; | ||
import type { ColorIndicatorProps } from './types'; | ||
|
||
function UnforwardedColorIndicator( | ||
props: WordPressComponentProps< ColorIndicatorProps, 'span', false >, | ||
forwardedRef: ForwardedRef< HTMLSpanElement > | ||
) { | ||
const { className, colorValue, ...additionalProps } = props; | ||
|
||
return ( | ||
<span | ||
className={ classnames( 'component-color-indicator', className ) } | ||
style={ { background: colorValue } } | ||
ref={ forwardedRef } | ||
{ ...additionalProps } | ||
/> | ||
); | ||
} | ||
|
||
/** | ||
* ColorIndicator is a React component that renders a specific color in a | ||
* circle. It's often used to summarize a collection of used colors in a child | ||
* component. | ||
* | ||
* ```jsx | ||
* import { ColorIndicator } from '@wordpress/components'; | ||
* | ||
* const MyColorIndicator = () => <ColorIndicator colorValue="#0073aa" />; | ||
* ``` | ||
*/ | ||
export const ColorIndicator = forwardRef( UnforwardedColorIndicator ); | ||
|
||
export default ColorIndicator; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import type { ComponentMeta, ComponentStory } from '@storybook/react'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import ColorIndicator from '..'; | ||
|
||
const meta: ComponentMeta< typeof ColorIndicator > = { | ||
component: ColorIndicator, | ||
title: 'Components/ColorIndicator', | ||
argTypes: { | ||
colorValue: { | ||
control: { type: 'color' }, | ||
}, | ||
}, | ||
parameters: { | ||
controls: { | ||
expanded: true, | ||
}, | ||
docs: { source: { state: 'open' } }, | ||
}, | ||
}; | ||
export default meta; | ||
|
||
const Template: ComponentStory< typeof ColorIndicator > = ( { ...args } ) => ( | ||
<ColorIndicator { ...args } /> | ||
); | ||
|
||
export const Default: ComponentStory< typeof ColorIndicator > = Template.bind( | ||
{} | ||
); | ||
Default.args = { | ||
colorValue: '#0073aa', | ||
}; |
13 changes: 0 additions & 13 deletions
13
packages/components/src/color-indicator/test/__snapshots__/index.js.snap
This file was deleted.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
packages/components/src/color-indicator/test/__snapshots__/index.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`ColorIndicator matches the snapshot 1`] = ` | ||
<div> | ||
<span | ||
aria-label="sample label" | ||
class="component-color-indicator" | ||
style="background: rgb(255, 255, 255);" | ||
/> | ||
</div> | ||
`; |
8 changes: 4 additions & 4 deletions
8
...ponents/src/color-indicator/test/index.js → ...onents/src/color-indicator/test/index.tsx
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 |
---|---|---|
@@ -1,19 +1,19 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { shallow } from 'enzyme'; | ||
import { render } from '@testing-library/react'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import ColorIndicator from '../'; | ||
import ColorIndicator from '..'; | ||
|
||
describe( 'ColorIndicator', () => { | ||
it( 'matches the snapshot', () => { | ||
const wrapper = shallow( | ||
const { container } = render( | ||
<ColorIndicator aria-label="sample label" colorValue="#fff" /> | ||
); | ||
|
||
expect( wrapper ).toMatchSnapshot(); | ||
expect( container ).toMatchSnapshot(); | ||
} ); | ||
} ); |
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,12 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import type { CSSProperties } from 'react'; | ||
|
||
export type ColorIndicatorProps = { | ||
/** | ||
* The color of the indicator. Any value from the CSS `background` property | ||
* is supported. | ||
*/ | ||
colorValue: CSSProperties[ 'background' ]; | ||
}; |