From 44cf2cd5585d4dfa639556be82f501e0515afb0e Mon Sep 17 00:00:00 2001 From: Daniel Martic Date: Fri, 8 Feb 2019 15:11:47 +0100 Subject: [PATCH] Converted Tag to use Hooks #860 --- src/Tag/index.jsx | 170 +++++++++++++++++++++++----------------------- src/Tag/tests.jsx | 4 +- 2 files changed, 86 insertions(+), 88 deletions(-) diff --git a/src/Tag/index.jsx b/src/Tag/index.jsx index e22522ee..876ae75c 100644 --- a/src/Tag/index.jsx +++ b/src/Tag/index.jsx @@ -13,100 +13,98 @@ import PropTypes from 'prop-types'; import { IconButton, Text } from '..'; import { generateId } from '../utils'; -import ThemeContext from '../Theming/ThemeContext'; -import { createCssMap } from '../Theming'; +import { useTheme } from '../Theming'; +const componentName = 'Tag'; -export default class Tag extends React.Component +const Tag = props => { - static contextType = ThemeContext; + const { + children, + id = generateId( 'Tag' ), + isDisabled, + isReadOnly, + label, + onClick, + } = props; - static propTypes = - { - /** - * Tag label (JSX node; overrides label prop) - */ - children : PropTypes.node, - /** - * CSS class name - */ - className : PropTypes.string, - /** - * CSS class map - */ - cssMap : PropTypes.objectOf( PropTypes.string ), - /** - * Component id - */ - id : PropTypes.string, - /** - * Display as disabled - */ - isDisabled : PropTypes.bool, - /** - * Display as read-only - */ - isReadOnly : PropTypes.bool, - /** - * Tag label (string) - */ - label : PropTypes.string, - /** - * onClick callback function for delete icon - */ - onClick : PropTypes.func, - }; + const cssMap = useTheme( componentName, props ); + + let labelText = children || label; - static defaultProps = + if ( typeof labelText === 'string' ) { - children : undefined, - className : undefined, - cssMap : undefined, - id : undefined, - isDisabled : false, - isReadOnly : false, - label : undefined, - onClick : undefined, - }; + labelText = ( + + { labelText } + + ); + } - static displayName = 'Tag'; + return ( +
+ { labelText } + + onClick && onClick( { id } ) + } /> +
+ ); +}; - render() - { - const { - children, - cssMap = createCssMap( this.context.Tag, this.props ), - id = generateId( 'Tag' ), - isDisabled, - isReadOnly, - label, - onClick, - } = this.props; +Tag.propTypes = +{ + /** + * Tag label (JSX node; overrides label prop) + */ + children : PropTypes.node, + /** + * CSS class name + */ + className : PropTypes.string, + /** + * CSS class map + */ + cssMap : PropTypes.objectOf( PropTypes.string ), + /** + * Component id + */ + id : PropTypes.string, + /** + * Display as disabled + */ + isDisabled : PropTypes.bool, + /** + * Display as read-only + */ + isReadOnly : PropTypes.bool, + /** + * Tag label (string) + */ + label : PropTypes.string, + /** + * onClick callback function for delete icon + */ + onClick : PropTypes.func, +}; - let labelText = children || label; +Tag.defaultProps = +{ + children : undefined, + className : undefined, + cssMap : undefined, + id : undefined, + isDisabled : false, + isReadOnly : false, + label : undefined, + onClick : undefined, +}; - if ( typeof labelText === 'string' ) - { - labelText = ( - - { labelText } - - ); - } +Tag.displayName = componentName; - return ( -
- { labelText } - - onClick && onClick( { id } ) - } /> -
- ); - } -} +export default Tag; diff --git a/src/Tag/tests.jsx b/src/Tag/tests.jsx index d1e357a4..6da4b066 100644 --- a/src/Tag/tests.jsx +++ b/src/Tag/tests.jsx @@ -24,9 +24,9 @@ describe( 'Tag', () => wrapper = shallow( ); } ); - test( 'should have “main” as default className', () => + test( 'should have “default” as default className', () => { - expect( wrapper.prop( 'className' ) ).toEqual( 'main' ); + expect( wrapper.prop( 'className' ) ).toEqual( 'default' ); } ); test( 'should have an IconButton as a child', () =>