From 3a594a0a32c43ac24380c634cd82115a7f1bfecc Mon Sep 17 00:00:00 2001 From: AlixH Date: Wed, 17 Nov 2021 09:51:24 +0100 Subject: [PATCH 1/5] Allow to clear car selection --- src/components/modal/ModalSelect.tsx | 50 +++++++++++++------ src/components/modal/ModalStyles.tsx | 4 ++ .../ChargingStationConnectorDetails.tsx | 19 ++++++- 3 files changed, 57 insertions(+), 16 deletions(-) diff --git a/src/components/modal/ModalSelect.tsx b/src/components/modal/ModalSelect.tsx index 05934c658..de57bf664 100644 --- a/src/components/modal/ModalSelect.tsx +++ b/src/components/modal/ModalSelect.tsx @@ -17,8 +17,12 @@ export interface Props extends BaseProps { disabled?: boolean; label?: string; renderItem?: (item?: T) => React.ReactElement; + // Render a generic item when the items list is empty (only if renderItemPlaceholder is null) renderNoItem?: () => React.ReactElement; + // Render a generic item when nothing is selected renderItemPlaceholder?: () => React.ReactElement; + // Whether or not to display the button to cleat the input + canClearInput?: boolean; selectionMode: ItemSelectionMode; onItemsSelected: (selectedItems: T[]) => void; defaultItemLoading?: boolean; @@ -26,6 +30,7 @@ export interface Props extends BaseProps { } interface State { isVisible: boolean; + noneSelected: boolean; selectedItems: T[]; selectedItemsCount: number; } @@ -42,12 +47,29 @@ export default class ModalSelect extends React.Component>, prevState: Readonly>, snapshot?: any) { + if(this.state.selectedItems[0]?.id !== this.props.defaultItem?.id && this.state.noneSelected === false) { + this.setState({ selectedItems: [this.props.defaultItem, ...this.state.selectedItems.slice(1)] }); + } + } + + public resetInput(noneSelected: boolean = false): void { + this.itemsListRef?.current?.clearSelectedItems(); + this.setState({noneSelected, selectedItems: [], selectedItemsCount: 0, isVisible: false}); + } + + private clearInput(): void { + this.resetInput(true); + } public render() { @@ -88,7 +110,7 @@ export default class ModalSelect extends React.Component {selectionMode === ItemSelectionMode.MULTI && ( -