Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add clear all on ChipSelect #832

Merged
merged 5 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelogs/unreleased/87396.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"title": "ChipSelect: add showClearButton prop to clear all selected chips",
"type": "feat",
"packages": "ui"
}
18 changes: 17 additions & 1 deletion packages/ui/__tests__/components/organisms/ChipSelect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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(
<ChipSelect
{...props}
onChangeValue={onChangeValue}
showClearButton={true}
/>,
);

wrapper.find(Button).simulate('press');

expect(onChangeValue).toHaveBeenCalledWith([]);
});
});
40 changes: 37 additions & 3 deletions packages/ui/src/components/organisms/ChipSelect/ChipSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -46,6 +46,7 @@ interface ChipSelectProps {
readonly?: boolean;
chipNumberOfLines?: number;
onChangeValue?: (value: any) => void;
showClearButton?: boolean;
}

const ChipSelect = ({
Expand All @@ -58,7 +59,10 @@ const ChipSelect = ({
readonly = false,
chipNumberOfLines,
onChangeValue = () => {},
showClearButton = false,
}: ChipSelectProps) => {
const Colors = useThemeColor();

const [selectedChip, setSelectedChip] = useState<Item[]>(
selectionItems.filter(item => item.isActive === true),
);
Expand Down Expand Up @@ -127,6 +131,11 @@ const ChipSelect = ({
);
};

const clearAllChips = () => {
setSelectedChip([]);
onChangeValue([]);
};

if (
selectionItems.length < 2 ||
(mode !== MODES.multi && mode !== MODES.switch)
Expand All @@ -136,7 +145,25 @@ const ChipSelect = ({

return (
<View style={[styles.chipContainer, style]}>
<ScrollView horizontal={true} showsHorizontalScrollIndicator={false}>
<ScrollView
horizontal={true}
showsHorizontalScrollIndicator={false}
contentContainerStyle={styles.scrollContainer}>
{showClearButton && (
<Button
iconName="trash"
color={
selectedChip.length > 0
? Colors.primaryColor
: Colors.secondaryColor
}
onPress={clearAllChips}
disabled={selectedChip.length === 0}
iconSize={18}
width={40}
style={styles.clearButton}
/>
)}
{selectionItems?.map((item, index) => (
<Chip
key={index}
Expand All @@ -163,6 +190,13 @@ const styles = StyleSheet.create({
marginHorizontal: CHIP_CONTAINER_MARGIN,
marginVertical: 2,
},
scrollContainer: {
alignItems: 'center',
},
clearButton: {
marginRight: 5,
height: 40,
},
});

export default ChipSelect;
3 changes: 2 additions & 1 deletion packages/ui/stories/organisms/ChipSelect.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const ChipSelect: Story<typeof Component> = {
option2_color: 'plannedColor',
option3_title: 'Option 3',
option3_color: 'infoColor',
showClearButton: false,
},
argTypes: {
option1_color: colorPicker,
Expand Down Expand Up @@ -76,5 +77,5 @@ export const ChipSelect: Story<typeof Component> = {
onChangeValue={() => {}}
{...args}
/>
)
),
};
Loading