Skip to content

Commit ccb861e

Browse files
committed
Implemented the logic to allow the user to remove items by clicking the 'Remove items...' button.
1 parent 5871aa5 commit ccb861e

1 file changed

Lines changed: 31 additions & 1 deletion

File tree

webpack_in/entry.jsx

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ class ListWidget extends React.Component {
129129
))
130130
});
131131

132+
this._mutateStateToRemoveItemsChecked = state => ({
133+
...state,
134+
items: state.items.filter(objItem => !objItem.is_checked)
135+
});
136+
132137
const objStateEmpty = {
133138
items: [],
134139
total_added: 0
@@ -146,6 +151,17 @@ class ListWidget extends React.Component {
146151
render() {
147152
const arrItemsSelected = this.state.items.filter(objItem => objItem.is_checked);
148153

154+
const formatListOfNames = (arrNames) => arrNames.reduce(
155+
(strOutput, strName, index) => (
156+
`${strOutput}${index > 0 // Will render either ' and' or ',' before 2nd item.
157+
// Will render ', and' before the 3rd+ last item.
158+
? (`${arrNames.length > 2 ? ',' : ""
159+
}${index === arrNames.length - 1 ? ' and' : ""}`
160+
)
161+
: ""} "${strName}"`
162+
),
163+
"");
164+
149165
return (
150166
<div style={ objStyleCommon }>
151167
<div style={ objStyleContent }>
@@ -195,7 +211,21 @@ class ListWidget extends React.Component {
195211
}}/>
196212
<ButtonWidget caption={ arrItemsSelected.length > 1 ? "Remove items..."
197213
: "Remove item..." }
198-
isDisabled={ arrItemsSelected.length === 0 }/>
214+
isDisabled={ arrItemsSelected.length === 0 }
215+
onClick={() => {
216+
const arrCaptions = arrItemsSelected
217+
.map(objItem => objItem.caption);
218+
const isConfirmed = confirm(
219+
`Are you sure you want to remove item${
220+
arrCaptions.length > 1 ? "s" : ""} ${
221+
formatListOfNames(arrCaptions)
222+
}?`);
223+
if (isConfirmed === false) {
224+
return;
225+
}
226+
227+
this.setState(this._mutateStateToRemoveItemsChecked(this.state));
228+
}}/>
199229
</div>
200230
</div>
201231
);

0 commit comments

Comments
 (0)