Skip to content

Commit

Permalink
[MM-57725] Convert ./components/common/chip/chip.tsx from Class Com…
Browse files Browse the repository at this point in the history
…ponent to Function Component (#26729)

* [MM-57725] Convert `./components/common/chip/chip.tsx` from Class Component to Function Component

* :refactor: improve code structure
  • Loading branch information
Syed-Ali-Abbas-Zaidi committed Apr 23, 2024
1 parent 4ee5763 commit 9b227aa
Showing 1 changed file with 42 additions and 31 deletions.
73 changes: 42 additions & 31 deletions webapp/channels/src/components/common/chip/chip.tsx
@@ -1,7 +1,7 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import React from 'react';
import React, {useCallback} from 'react';
import {FormattedMessage} from 'react-intl';
import styled from 'styled-components';

Expand Down Expand Up @@ -52,34 +52,45 @@ const StyledChip = styled.button<{ otherOption?: boolean }>`
}
`;

export default class Chip extends React.PureComponent<Props> {
onClick = (e: React.MouseEvent) => {
const emojiStyles = {marginRight: '11px'};

const Chip = ({
onClick,
otherOption,
className,
leadingIcon,
id,
defaultMessage,
values,
additionalMarkup,
}: Props) => {
const handleClick = useCallback((e: React.MouseEvent) => {
e.preventDefault();
this.props.onClick?.();
};

render() {
return (
<StyledChip
onClick={this.onClick}
otherOption={this.props.otherOption}
className={this.props.className || ''}
>
{this.props.leadingIcon && (
<RenderEmoji
emojiName={this.props.leadingIcon}
emojiStyle={{marginRight: '11px'}}
/>
)}
{(this.props.id && this.props.defaultMessage && this.props.values) && (
<FormattedMessage
id={this.props.id}
defaultMessage={this.props.defaultMessage}
values={this.props.values}
/>
)}
{this.props.additionalMarkup}
</StyledChip>
);
}
}
onClick?.();
}, [onClick]);

return (
<StyledChip
onClick={handleClick}
otherOption={otherOption}
className={className || ''}
>
{leadingIcon && (
<RenderEmoji
emojiName={leadingIcon}
emojiStyle={emojiStyles}
/>
)}
{(id && defaultMessage && values) && (
<FormattedMessage
id={id}
defaultMessage={defaultMessage}
values={values}
/>
)}
{additionalMarkup}
</StyledChip>
);
};

export default Chip;

0 comments on commit 9b227aa

Please sign in to comment.