From c51250fd46cc84f3d043c90a9af6d0a7915af9d6 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 21 Sep 2023 14:34:42 +0200 Subject: [PATCH] Create SelectNext component replacing Select --- src/components/input/Select.tsx | 1 + src/components/input/SelectContext.ts | 10 + src/components/input/SelectNext.tsx | 195 ++++++++++ src/components/input/index.ts | 2 + src/components/input/test/SelectNext-test.js | 220 +++++++++++ src/index.ts | 2 + .../components/patterns/input/SelectPage.tsx | 16 +- .../patterns/prototype/SelectNextPage.tsx | 344 ++++++++++++++++++ src/pattern-library/routes.ts | 7 + 9 files changed, 793 insertions(+), 4 deletions(-) create mode 100644 src/components/input/SelectContext.ts create mode 100644 src/components/input/SelectNext.tsx create mode 100644 src/components/input/test/SelectNext-test.js create mode 100644 src/pattern-library/components/patterns/prototype/SelectNextPage.tsx diff --git a/src/components/input/Select.tsx b/src/components/input/Select.tsx index 2344ec1d6..db32bab64 100644 --- a/src/components/input/Select.tsx +++ b/src/components/input/Select.tsx @@ -23,6 +23,7 @@ const arrowImage = `url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000 /** * Style a native `'} elements. -

+ <> +

+ Select is a presentational component that styles native{' '} + {' + {value.name} + {!textOnly && ( +

+ {value.id} +
+ )} + + ) : disabled ? ( + <>This is disabled + ) : ( + <>Select one... + ) + } + disabled={disabled} + > + {theItems.map(item => ( + + {() => + textOnly ? ( + <>{item.name} + ) : ( + <> + {item.name} +
+
+ {item.id} +
+ + ) + } + + ))} + + ); +} + +function InputGroupSelect_() { + const [selected, setSelected] = useState<(typeof items)[number]>(); + const selectedIndex = useMemo( + () => (!selected ? -1 : items.findIndex(item => item === selected)), + [selected], + ); + const next = useCallback(() => { + const newIndex = selectedIndex + 1; + setSelected( + (items[newIndex]?.id === '4' ? items[newIndex + 1] : items[newIndex]) ?? + selected, + ); + }, [selected, selectedIndex]); + const previous = useCallback(() => { + const newIndex = selectedIndex - 1; + setSelected( + (items[newIndex]?.id === '4' ? items[newIndex - 1] : items[newIndex]) ?? + selected, + ); + }, [selected, selectedIndex]); + + return ( + + +
+ +
+ = items.length - 1} + /> +
+ ); +} + +export default function SelectNextPage() { + return ( + + SelectNext is a presentational component which behaves + like the native {'