Skip to content

Commit bfc6b20

Browse files
dbraquartayolab
andauthored
Add a prop to DirectoryItemsInput to browse a single element (#767)
Signed-off-by: David BRAQUART <[email protected]> Co-authored-by: Ayoub LABIDI <[email protected]>
1 parent d841a0a commit bfc6b20

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/components/inputs/reactHookForm/DirectoryItemsInput.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export interface DirectoryItemsInputProps {
6666
onRowChanged?: (a: boolean) => void;
6767
onChange?: (e: any) => void;
6868
disable?: boolean;
69+
allowMultiSelect?: boolean;
6970
labelRequiredFromContext?: boolean;
7071
}
7172

@@ -80,13 +81,14 @@ export function DirectoryItemsInput({
8081
onRowChanged,
8182
onChange,
8283
disable = false,
84+
allowMultiSelect = true,
8385
labelRequiredFromContext = true,
84-
}: DirectoryItemsInputProps) {
86+
}: Readonly<DirectoryItemsInputProps>) {
8587
const { snackError } = useSnackMessage();
8688
const intl = useIntl();
8789
const [selected, setSelected] = useState<UUID[]>([]);
8890
const [expanded, setExpanded] = useState<UUID[]>([]);
89-
const [multiSelect, setMultiSelect] = useState(true);
91+
const [multiSelect, setMultiSelect] = useState(allowMultiSelect);
9092
const types = useMemo(() => [elementType], [elementType]);
9193
const [directoryItemSelectorOpen, setDirectoryItemSelectorOpen] = useState(false);
9294
const {
@@ -165,6 +167,10 @@ export function DirectoryItemsInput({
165167
[getValues, name]
166168
);
167169

170+
const shouldReplaceElement = useMemo(() => {
171+
return allowMultiSelect === false && elements?.length === 1;
172+
}, [allowMultiSelect, elements]);
173+
168174
return (
169175
<>
170176
<FormControl
@@ -208,8 +214,14 @@ export function DirectoryItemsInput({
208214
size="small"
209215
disabled={disable}
210216
onClick={() => {
211-
setDirectoryItemSelectorOpen(true);
212-
setMultiSelect(true);
217+
if (shouldReplaceElement) {
218+
handleChipClick(0);
219+
} else {
220+
setDirectoryItemSelectorOpen(true);
221+
if (allowMultiSelect) {
222+
setMultiSelect(true);
223+
}
224+
}
213225
}}
214226
>
215227
<FolderIcon />

0 commit comments

Comments
 (0)