From 1f736aec40002273684939df29b1462eb95e35b8 Mon Sep 17 00:00:00 2001
From: gca-axelor <117281520+gca-axelor@users.noreply.github.com>
Date: Mon, 9 Dec 2024 17:29:03 +0100
Subject: [PATCH] feat: add clear all on ChipSelect (#832)
* RM#87395
---
changelogs/unreleased/87396.json | 5 +++
.../components/organisms/ChipSelect.test.js | 18 ++++++++-
.../organisms/ChipSelect/ChipSelect.tsx | 40 +++++++++++++++++--
.../stories/organisms/ChipSelect.stories.tsx | 3 +-
4 files changed, 61 insertions(+), 5 deletions(-)
create mode 100644 changelogs/unreleased/87396.json
diff --git a/changelogs/unreleased/87396.json b/changelogs/unreleased/87396.json
new file mode 100644
index 0000000000..10f8d4399a
--- /dev/null
+++ b/changelogs/unreleased/87396.json
@@ -0,0 +1,5 @@
+{
+ "title": "ChipSelect: add showClearButton prop to clear all selected chips",
+ "type": "feat",
+ "packages": "ui"
+}
diff --git a/packages/ui/__tests__/components/organisms/ChipSelect.test.js b/packages/ui/__tests__/components/organisms/ChipSelect.test.js
index db80e6229a..af3ae4c4ec 100644
--- a/packages/ui/__tests__/components/organisms/ChipSelect.test.js
+++ b/packages/ui/__tests__/components/organisms/ChipSelect.test.js
@@ -19,7 +19,7 @@
import React from 'react';
import {View} from 'react-native';
import {shallow} from 'enzyme';
-import {Chip, ChipSelect} from '@axelor/aos-mobile-ui';
+import {Button, Chip, ChipSelect} from '@axelor/aos-mobile-ui';
import {getGlobalStyles, getDefaultThemeColors} from '../../tools';
describe('ChipSelect Component', () => {
@@ -218,4 +218,20 @@ describe('ChipSelect Component', () => {
);
}
});
+
+ it('should clear all selected chips when Clear All button is pressed', () => {
+ const onChangeValue = jest.fn();
+
+ const wrapper = shallow(
+ ,
+ );
+
+ wrapper.find(Button).simulate('press');
+
+ expect(onChangeValue).toHaveBeenCalledWith([]);
+ });
});
diff --git a/packages/ui/src/components/organisms/ChipSelect/ChipSelect.tsx b/packages/ui/src/components/organisms/ChipSelect/ChipSelect.tsx
index 6e4ae29bfb..b7cb94d52d 100644
--- a/packages/ui/src/components/organisms/ChipSelect/ChipSelect.tsx
+++ b/packages/ui/src/components/organisms/ChipSelect/ChipSelect.tsx
@@ -18,8 +18,8 @@
import React, {useEffect, useMemo, useState} from 'react';
import {Dimensions, StyleSheet, View, ScrollView} from 'react-native';
-import {Color} from '../../../theme/themes';
-import {Chip} from '../../molecules';
+import {useThemeColor, Color} from '../../../theme';
+import {Button, Chip} from '../../molecules';
const CHIP_CONTAINER_MARGIN = 16;
const CHIP_MARGIN = 2;
@@ -46,6 +46,7 @@ interface ChipSelectProps {
readonly?: boolean;
chipNumberOfLines?: number;
onChangeValue?: (value: any) => void;
+ showClearButton?: boolean;
}
const ChipSelect = ({
@@ -58,7 +59,10 @@ const ChipSelect = ({
readonly = false,
chipNumberOfLines,
onChangeValue = () => {},
+ showClearButton = false,
}: ChipSelectProps) => {
+ const Colors = useThemeColor();
+
const [selectedChip, setSelectedChip] = useState- (
selectionItems.filter(item => item.isActive === true),
);
@@ -127,6 +131,11 @@ const ChipSelect = ({
);
};
+ const clearAllChips = () => {
+ setSelectedChip([]);
+ onChangeValue([]);
+ };
+
if (
selectionItems.length < 2 ||
(mode !== MODES.multi && mode !== MODES.switch)
@@ -136,7 +145,25 @@ const ChipSelect = ({
return (
-
+
+ {showClearButton && (
+