Skip to content

Commit

Permalink
fix: refactor chips delete callback handling
Browse files Browse the repository at this point in the history
  • Loading branch information
HellWolf93 committed Jun 28, 2020
1 parent f19835d commit 95fd79f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
14 changes: 8 additions & 6 deletions src/components/MultiSelect/chips.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,30 @@ import React from 'react';
import PropTypes from 'prop-types';
import { StyledChip } from './styled';

function Chips(props) {
const Chips = props => {
const { value, variant, onDelete, disabled, readOnly } = props;
const getDeleteCallback = val => {
return disabled || readOnly ? null : () => onDelete(val);
};

if (!value) {
return null;
}

if (Array.isArray(value)) {
return value.map(val => {
const onDeleteCallback = disabled || readOnly ? null : () => onDelete(val);
return (
<StyledChip
key={val.name}
label={val.label}
variant={variant}
onDelete={onDeleteCallback}
onDelete={getDeleteCallback(val)}
/>
);
});
}
const onDeleteCallback = disabled || readOnly ? null : () => onDelete(value);
return <StyledChip label={value.label} variant={variant} onDelete={onDeleteCallback} />;
}
return <StyledChip label={value.label} variant={variant} onDelete={getDeleteCallback(value)} />;
};

Chips.propTypes = {
value: PropTypes.oneOfType([
Expand Down
3 changes: 1 addition & 2 deletions src/components/MultiSelect/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import Chips from './chips';

const Content = props => {
const { variant, value, chipVariant, readOnly, disabled, onDelete } = props;
const onDeleteCallback = disabled || readOnly ? null : onDelete;

if (variant === 'chip') {
return (
Expand All @@ -15,7 +14,7 @@ const Content = props => {
variant={chipVariant}
readOnly={readOnly}
disabled={disabled}
onDelete={onDeleteCallback}
onDelete={onDelete}
/>
);
}
Expand Down

0 comments on commit 95fd79f

Please sign in to comment.