Skip to content

Commit

Permalink
refactor: refactoring after review
Browse files Browse the repository at this point in the history
  • Loading branch information
PKulkoRaccoonGang committed Oct 18, 2023
1 parent 2142773 commit 68e4598
Show file tree
Hide file tree
Showing 23 changed files with 111 additions and 135 deletions.
18 changes: 18 additions & 0 deletions src/Chip/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ notes: |
</Stack>
```

## With isSelected state

```jsx live
<Stack
gap={2}
direction="horizontal"
>
<Chip isSelected>New</Chip>
<Chip
isSelected
iconAfter={Close}
onIconAfterClick={() => console.log('onIconAfterClick')}
>
New
</Chip>
</Stack>
```

## With Icon Before and After
### Basic Usage

Expand Down
135 changes: 71 additions & 64 deletions src/Chip/index.tsx
Original file line number Diff line number Diff line change
@@ -1,105 +1,105 @@
import React, { ForwardedRef, KeyboardEventHandler, MouseEventHandler } from 'react';
import { useIntl } from 'react-intl';
import PropTypes from 'prop-types';
import classNames from 'classnames';
// @ts-ignore
import Icon from '../Icon';
// @ts-ignore
import IconButton from '../IconButton';
// @ts-ignore
import messages from './messages';

const STYLE_VARIANTS = [
export const ICON_AFTER_ALT_TEXT = 'Chip icon after';
export const ICON_BEFORE_ALT_TEXT = 'Chip icon before';

export const STYLE_VARIANTS = [
'light',
'dark',
];

export const CHIP_PGN_CLASS = 'pgn__chip';

export interface IChip {
children: React.ReactNode,
className?: string,
variant?: string,
iconBefore?: React.ReactElement | Function,
iconBeforeAlt?: string,
iconAfter?: React.ReactElement | Function,
iconAfterAlt?: string,
onIconBeforeClick?: KeyboardEventHandler & MouseEventHandler,
onIconAfterClick?: KeyboardEventHandler & MouseEventHandler,
disabled?: boolean,
isSelected?: boolean,
}

export const CHIP_PGN_CLASS = 'pgn__chip';

const Chip = React.forwardRef(({
children,
className,
variant,
iconBefore,
iconBeforeAlt,
iconAfter,
iconAfterAlt,
onIconBeforeClick,
onIconAfterClick,
disabled,
isSelected,
...props
}: IChip, ref: ForwardedRef<HTMLDivElement>) => {
const intl = useIntl();

return (
}: IChip, ref: ForwardedRef<HTMLDivElement>) => (
<div
tabIndex={0}
role="button"
className={classNames(
CHIP_PGN_CLASS,
`pgn__chip-${variant}`,
className,
{ disabled, selected: isSelected },
)}
ref={ref}
{...props}
>
{iconBefore && (
<div className={classNames('pgn__chip__icon-before', { active: onIconBeforeClick })}>
{onIconBeforeClick ? (
<IconButton
src={iconBefore}
onClick={onIconBeforeClick}
onKeyPress={onIconBeforeClick}
iconAs={Icon}
alt={iconBeforeAlt}
invertColors
data-testid="icon-before"
/>
) : (
<Icon src={iconBefore} />
)}
</div>
)}
<div
tabIndex={0}
role="button"
className={classNames(
CHIP_PGN_CLASS,
`pgn__chip-${variant}`,
className,
{ disabled, selected: isSelected },
)}
ref={ref}
{...props}
className={classNames('pgn__chip__label', {
'p-before': iconBefore,
'p-after': iconAfter,
})}
>
{iconBefore && (
<div className={classNames('pgn__chip__icon-before', { active: onIconBeforeClick })}>
{onIconBeforeClick ? (
<IconButton
src={iconBefore}
onClick={onIconBeforeClick}
onKeyPress={onIconBeforeClick}
iconAs={Icon}
alt={intl.formatMessage(messages.iconBeforeAltText)}
invertColors
data-testid="icon-before"
/>
) : (
<Icon src={iconBefore} />
)}
</div>
)}
<div
className={classNames('pgn__chip__label', {
'p-before': iconBefore,
'p-after': iconAfter,
})}
>
{children}
</div>
{iconAfter && (
<div className={classNames('pgn__chip__icon-after', { active: onIconAfterClick })}>
{onIconAfterClick ? (
<IconButton
onClick={onIconAfterClick}
onKeyPress={onIconAfterClick}
src={iconAfter}
iconAs={Icon}
alt={intl.formatMessage(messages.iconAfterAltText)}
invertColors
data-testid="icon-after"
/>
) : (
<Icon src={iconAfter} />
)}
</div>
)}
{children}
</div>
);
});
{iconAfter && (
<div className={classNames('pgn__chip__icon-after', { active: onIconAfterClick })}>
{onIconAfterClick ? (
<IconButton
onClick={onIconAfterClick}
onKeyPress={onIconAfterClick}
src={iconAfter}
iconAs={Icon}
alt={iconAfterAlt}
invertColors
data-testid="icon-after"
/>
) : (
<Icon src={iconAfter} />
)}
</div>
)}
</div>
));

Chip.propTypes = {
/** Specifies the content of the `Chip`. */
Expand All @@ -117,6 +117,8 @@ Chip.propTypes = {
* `import { Check } from '@edx/paragon/icons';`
*/
iconBefore: PropTypes.oneOfType([PropTypes.element, PropTypes.func]),
/** Specifies icon alt text. */
iconBeforeAlt: PropTypes.string,
/** A click handler for the `Chip` icon before. */
onIconBeforeClick: PropTypes.func,
/**
Expand All @@ -126,8 +128,11 @@ Chip.propTypes = {
* `import { Check } from '@edx/paragon/icons';`
*/
iconAfter: PropTypes.oneOfType([PropTypes.element, PropTypes.func]),
/** Specifies icon alt text. */
iconAfterAlt: PropTypes.string,
/** A click handler for the `Chip` icon after. */
onIconAfterClick: PropTypes.func,
/** Indicates if `Chip` has been selected. */
isSelected: PropTypes.bool,
};

Expand All @@ -140,6 +145,8 @@ Chip.defaultProps = {
onIconBeforeClick: undefined,
onIconAfterClick: undefined,
isSelected: false,
iconAfterAlt: ICON_AFTER_ALT_TEXT,
iconBeforeAlt: ICON_BEFORE_ALT_TEXT,
};

export default Chip;
16 changes: 0 additions & 16 deletions src/Chip/messages.js

This file was deleted.

4 changes: 3 additions & 1 deletion src/ChipCarousel/_variables.scss
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
$chip-carousel-controls-top-offset: -3px !default;
$chip-carousel-controls-top-offset: .375rem !default;
$chip-carousel-container-padding-x: .625rem !default;
$chip-carousel-container-padding-y: .313rem !default;
1 change: 1 addition & 0 deletions src/ChipCarousel/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
&.pgn__chip-carousel-gap__#{$level} {
.pgn__overflow-scroll-overflow-container {
column-gap: $space;
padding: $chip-carousel-container-padding-x $chip-carousel-container-padding-y;
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/i18n/messages/ar.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,5 @@
"pgn.Dropzone.UploadProgress.uploadLabel": "رفع {filename} جارٍ.",
"pgn.FormAutosuggest.iconButtonClosed": "إغلاق قائمة الخيارات",
"pgn.FormAutosuggest.iconButtonOpened": "فتح قائمة الخيارات",
"pgn.Toast.closeLabel": "إغلاق ",
"pgn.Chip.iconBeforeAltText": "Chip icon before",
"pgn.Chip.iconAfterAltText": "Chip icon after"
"pgn.Toast.closeLabel": "إغلاق "
}
4 changes: 1 addition & 3 deletions src/i18n/messages/ca.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,5 @@
"pgn.Dropzone.UploadProgress.uploadLabel": "Uploading {filename}.",
"pgn.FormAutosuggest.iconButtonClosed": "Close the options menu",
"pgn.FormAutosuggest.iconButtonOpened": "Open the options menu",
"pgn.Toast.closeLabel": "Close",
"pgn.Chip.iconBeforeAltText": "Chip icon before",
"pgn.Chip.iconAfterAltText": "Chip icon after"
"pgn.Toast.closeLabel": "Close"
}
4 changes: 1 addition & 3 deletions src/i18n/messages/es_419.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,5 @@
"pgn.Dropzone.UploadProgress.uploadLabel": "Subiendo {filename}.",
"pgn.FormAutosuggest.iconButtonClosed": "Cerrar el menú de opciones",
"pgn.FormAutosuggest.iconButtonOpened": "Abre el menú de opciones",
"pgn.Toast.closeLabel": "Cerrar",
"pgn.Chip.iconBeforeAltText": "Chip icon before",
"pgn.Chip.iconAfterAltText": "Chip icon after"
"pgn.Toast.closeLabel": "Cerrar"
}
4 changes: 1 addition & 3 deletions src/i18n/messages/es_AR.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,5 @@
"pgn.Dropzone.UploadProgress.uploadLabel": "Subiendo {filename}.",
"pgn.FormAutosuggest.iconButtonClosed": "Cerrar el menú de opciones",
"pgn.FormAutosuggest.iconButtonOpened": "Abre el menú de opciones",
"pgn.Toast.closeLabel": "Cerrar",
"pgn.Chip.iconBeforeAltText": "Chip icon before",
"pgn.Chip.iconAfterAltText": "Chip icon after"
"pgn.Toast.closeLabel": "Cerrar"
}
4 changes: 1 addition & 3 deletions src/i18n/messages/es_ES.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,5 @@
"pgn.Dropzone.UploadProgress.uploadLabel": "Subiendo {filename}.",
"pgn.FormAutosuggest.iconButtonClosed": "Cerrar el menú de opciones",
"pgn.FormAutosuggest.iconButtonOpened": "Abre el menú de opciones",
"pgn.Toast.closeLabel": "Cerrar",
"pgn.Chip.iconBeforeAltText": "Chip icon before",
"pgn.Chip.iconAfterAltText": "Chip icon after"
"pgn.Toast.closeLabel": "Cerrar"
}
4 changes: 1 addition & 3 deletions src/i18n/messages/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,5 @@
"pgn.Dropzone.UploadProgress.uploadLabel": "Uploading {filename}.",
"pgn.FormAutosuggest.iconButtonClosed": "Close the options menu",
"pgn.FormAutosuggest.iconButtonOpened": "Open the options menu",
"pgn.Toast.closeLabel": "Close",
"pgn.Chip.iconBeforeAltText": "Chip icon before",
"pgn.Chip.iconAfterAltText": "Chip icon after"
"pgn.Toast.closeLabel": "Close"
}
4 changes: 1 addition & 3 deletions src/i18n/messages/he.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,5 @@
"pgn.Dropzone.UploadProgress.uploadLabel": "Uploading {filename}.",
"pgn.FormAutosuggest.iconButtonClosed": "Close the options menu",
"pgn.FormAutosuggest.iconButtonOpened": "Open the options menu",
"pgn.Toast.closeLabel": "Close",
"pgn.Chip.iconBeforeAltText": "Chip icon before",
"pgn.Chip.iconAfterAltText": "Chip icon after"
"pgn.Toast.closeLabel": "Close"
}
4 changes: 1 addition & 3 deletions src/i18n/messages/id.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,5 @@
"pgn.Dropzone.UploadProgress.uploadLabel": "Uploading {filename}.",
"pgn.FormAutosuggest.iconButtonClosed": "Close the options menu",
"pgn.FormAutosuggest.iconButtonOpened": "Open the options menu",
"pgn.Toast.closeLabel": "Close",
"pgn.Chip.iconBeforeAltText": "Chip icon before",
"pgn.Chip.iconAfterAltText": "Chip icon after"
"pgn.Toast.closeLabel": "Close"
}
4 changes: 1 addition & 3 deletions src/i18n/messages/it_IT.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,5 @@
"pgn.Dropzone.UploadProgress.uploadLabel": "Caricamento {filename}.",
"pgn.FormAutosuggest.iconButtonClosed": "Close the options menu",
"pgn.FormAutosuggest.iconButtonOpened": "Open the options menu",
"pgn.Toast.closeLabel": "Chiudi",
"pgn.Chip.iconBeforeAltText": "Chip icon before",
"pgn.Chip.iconAfterAltText": "Chip icon after"
"pgn.Toast.closeLabel": "Chiudi"
}
4 changes: 1 addition & 3 deletions src/i18n/messages/ko_KR.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,5 @@
"pgn.Dropzone.UploadProgress.uploadLabel": "Uploading {filename}.",
"pgn.FormAutosuggest.iconButtonClosed": "Close the options menu",
"pgn.FormAutosuggest.iconButtonOpened": "Open the options menu",
"pgn.Toast.closeLabel": "Close",
"pgn.Chip.iconBeforeAltText": "Chip icon before",
"pgn.Chip.iconAfterAltText": "Chip icon after"
"pgn.Toast.closeLabel": "Close"
}
4 changes: 1 addition & 3 deletions src/i18n/messages/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,5 @@
"pgn.Dropzone.UploadProgress.uploadLabel": "Uploading {filename}.",
"pgn.FormAutosuggest.iconButtonClosed": "Close the options menu",
"pgn.FormAutosuggest.iconButtonOpened": "Open the options menu",
"pgn.Toast.closeLabel": "Zamknij",
"pgn.Chip.iconBeforeAltText": "Chip icon before",
"pgn.Chip.iconAfterAltText": "Chip icon after"
"pgn.Toast.closeLabel": "Zamknij"
}
4 changes: 1 addition & 3 deletions src/i18n/messages/pt_BR.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,5 @@
"pgn.Dropzone.UploadProgress.uploadLabel": "Uploading {filename}.",
"pgn.FormAutosuggest.iconButtonClosed": "Close the options menu",
"pgn.FormAutosuggest.iconButtonOpened": "Open the options menu",
"pgn.Toast.closeLabel": "Close",
"pgn.Chip.iconBeforeAltText": "Chip icon before",
"pgn.Chip.iconAfterAltText": "Chip icon after"
"pgn.Toast.closeLabel": "Close"
}
4 changes: 1 addition & 3 deletions src/i18n/messages/pt_PT.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,5 @@
"pgn.Dropzone.UploadProgress.uploadLabel": "Carregando {filename}.",
"pgn.FormAutosuggest.iconButtonClosed": "Fechar o menu de opções",
"pgn.FormAutosuggest.iconButtonOpened": "Abrir o menu de opções",
"pgn.Toast.closeLabel": "Fechar",
"pgn.Chip.iconBeforeAltText": "Chip icon before",
"pgn.Chip.iconAfterAltText": "Chip icon after"
"pgn.Toast.closeLabel": "Fechar"
}
4 changes: 1 addition & 3 deletions src/i18n/messages/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,5 @@
"pgn.Dropzone.UploadProgress.uploadLabel": "Uploading {filename}.",
"pgn.FormAutosuggest.iconButtonClosed": "Close the options menu",
"pgn.FormAutosuggest.iconButtonOpened": "Open the options menu",
"pgn.Toast.closeLabel": "Close",
"pgn.Chip.iconBeforeAltText": "Chip icon before",
"pgn.Chip.iconAfterAltText": "Chip icon after"
"pgn.Toast.closeLabel": "Close"
}
4 changes: 1 addition & 3 deletions src/i18n/messages/th.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,5 @@
"pgn.Dropzone.UploadProgress.uploadLabel": "Uploading {filename}.",
"pgn.FormAutosuggest.iconButtonClosed": "Close the options menu",
"pgn.FormAutosuggest.iconButtonOpened": "Open the options menu",
"pgn.Toast.closeLabel": "Close",
"pgn.Chip.iconBeforeAltText": "Chip icon before",
"pgn.Chip.iconAfterAltText": "Chip icon after"
"pgn.Toast.closeLabel": "Close"
}
4 changes: 1 addition & 3 deletions src/i18n/messages/tr_TR.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,5 @@
"pgn.Dropzone.UploadProgress.uploadLabel": "{filename} yükleniyor.",
"pgn.FormAutosuggest.iconButtonClosed": "Seçenekler menüsünü kapat",
"pgn.FormAutosuggest.iconButtonOpened": "Seçenekler menüsünü aç",
"pgn.Toast.closeLabel": "Kapat",
"pgn.Chip.iconBeforeAltText": "Chip icon before",
"pgn.Chip.iconAfterAltText": "Chip icon after"
"pgn.Toast.closeLabel": "Kapat"
}
Loading

0 comments on commit 68e4598

Please sign in to comment.