Skip to content

Commit

Permalink
Fixed #2345 - Add removable property to Chips
Browse files Browse the repository at this point in the history
  • Loading branch information
mertsincan committed Nov 4, 2021
1 parent 2eb20fa commit eedb022
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/components/chips/Chips.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import * as React from 'react';
import TooltipOptions from '../tooltip/tooltipoptions';

type ChipsRemovableType = boolean | ((options: ChipsRemovableOptions) => boolean);

interface ChipsRemovableOptions {
value: any;
index: number;
props: ChipsProps;
}

interface ChipsAddParams {
originalEvent: React.SyntheticEvent;
value: any;
Expand Down Expand Up @@ -30,6 +38,7 @@ export interface ChipsProps {
value?: any[];
max?: number;
disabled?: boolean;
removable?: ChipsRemovableType;
style?: object;
className?: string;
tooltip?: string;
Expand Down
24 changes: 22 additions & 2 deletions src/components/chips/Chips.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { Component, createRef } from 'react';
import PropTypes from 'prop-types';
import { classNames } from '../utils/Utils';
import { classNames, ObjectUtils } from '../utils/Utils';
import { tip } from '../tooltip/Tooltip';

export class Chips extends Component {
Expand All @@ -13,6 +13,7 @@ export class Chips extends Component {
value: null,
max: null,
disabled: null,
removable: true,
style: null,
className: null,
tooltip: null,
Expand All @@ -36,6 +37,7 @@ export class Chips extends Component {
value: PropTypes.array,
max: PropTypes.number,
disabled: PropTypes.bool,
removable: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
style: PropTypes.object,
className: PropTypes.string,
tooltip: PropTypes.string,
Expand Down Expand Up @@ -75,6 +77,10 @@ export class Chips extends Component {
let values = [...this.props.value];
const removedItem = values.splice(index, 1);

if (!this.isRemovable(removedItem, index)) {
return;
}

if (this.props.onRemove) {
this.props.onRemove({
originalEvent: event,
Expand Down Expand Up @@ -216,6 +222,10 @@ export class Chips extends Component {
return (this.props.value && this.props.value.length) || (this.inputRef && this.inputRef.current && this.inputRef.current.value && this.inputRef.current.value.length);
}

isRemovable(value, index) {
return ObjectUtils.getPropValue(this.props.removable, { value, index, props: this.props })
}

updateInputRef() {
let ref = this.props.inputRef;

Expand Down Expand Up @@ -269,9 +279,19 @@ export class Chips extends Component {
});
}

renderRemoveIcon(value, index) {
if (!this.props.disabled && this.isRemovable(value, index)) {
return (
<span className="p-chips-token-icon pi pi-times-circle" onClick={(event) => this.removeItem(event, index)}></span>
)
}

return null;
}

renderItem(value, index) {
const content = this.props.itemTemplate ? this.props.itemTemplate(value) : value;
const icon = this.props.disabled ? null : <span className="p-chips-token-icon pi pi-times-circle" onClick={(event) => this.removeItem(event, index)}></span>;
const icon = this.renderRemoveIcon(value, index);

return (
<li key={index} className="p-chips-token p-highlight">
Expand Down
6 changes: 6 additions & 0 deletions src/showcase/chips/ChipsDoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,12 @@ customChip(item) {
<td>false</td>
<td>When present, it specifies that the element should be disabled.</td>
</tr>
<tr>
<td>removable</td>
<td>boolean</td>
<td>true</td>
<td>Whether an item is removable.</td>
</tr>
<tr>
<td>style</td>
<td>string</td>
Expand Down

0 comments on commit eedb022

Please sign in to comment.