From acb12e73e82b56ee7c396001ea98951bb623bb04 Mon Sep 17 00:00:00 2001 From: Ari Date: Mon, 18 Apr 2022 20:00:22 -0400 Subject: [PATCH] multiselect numbered --- .../Inputs/InputMultiSelect.stories.tsx | 39 +++++++++ .../components/Inputs/InputMultiSelect.tsx | 66 +++++++++++++++ webapp/src/components/Inputs/styles.ts | 81 +++++++++++++++++++ 3 files changed, 186 insertions(+) create mode 100644 webapp/src/components/Inputs/InputMultiSelect.stories.tsx create mode 100644 webapp/src/components/Inputs/InputMultiSelect.tsx diff --git a/webapp/src/components/Inputs/InputMultiSelect.stories.tsx b/webapp/src/components/Inputs/InputMultiSelect.stories.tsx new file mode 100644 index 0000000..2e370c4 --- /dev/null +++ b/webapp/src/components/Inputs/InputMultiSelect.stories.tsx @@ -0,0 +1,39 @@ +import { ComponentMeta, ComponentStory } from "@storybook/react"; +import { useEffect, useState } from "react"; + +import InputMultiSelect from "./InputMultiSelect"; + +export default { + title: 'Inputs/InputMultiSelect', + component: InputMultiSelect, + parameters: { actions: { argTypesRegex: '^on.*' } }, +} as ComponentMeta; + + +const Template: ComponentStory = args => { + + const [value, setValue] = useState(args.selected) + + useEffect(() => { + setValue(args.selected); + }, [args.selected]) + + return
+ { + setValue(a); + args.onChange(a); + }} options={args.options} selected={value} /> +
+} + +export const Simple = Template.bind({}) +Simple.args = { + label: "Room Tags", + options: [ + "Bears", + "Cats", + "Kitchen", + "Bathroom", + ], + selected: [1], +} diff --git a/webapp/src/components/Inputs/InputMultiSelect.tsx b/webapp/src/components/Inputs/InputMultiSelect.tsx new file mode 100644 index 0000000..37bb847 --- /dev/null +++ b/webapp/src/components/Inputs/InputMultiSelect.tsx @@ -0,0 +1,66 @@ +import { ArrowIcon } from "components/Icons"; +import InputWrapper from "./InputWrapper"; +import { useInputMultiSelectStyles } from "./styles"; +import { useState } from 'react'; + + +export interface Props { + label: string; + selected: Array; + options: readonly T[]; + onChange: (i: Array) => void; + max?: number; + id?: string; + className?: string; +} +export default function InputMultiSelect({ + selected = [], options, onChange, label, max, + ...selattr +}: Props) { + // state + const [open, setOpen] = useState(false); + + // alias + const maxedOut = max ? selected.length >= max : false; + const toggle = () => setOpen(!open); + + // style + const { classes } = useInputMultiSelectStyles({ + open, + selected, + maxedOut, + }); + + + return
+
+ + {selected.length} + +
+
+ {options.map((v, i) => { + const selectedCount = selected.indexOf(i); + const isSelected = selectedCount > -1; + return
{ + if (!isSelected) { + if (!maxedOut) { + onChange([...selected, i]) + } + } else { + onChange( + selected.slice(0, selectedCount).concat(selected.slice(selectedCount + 1)) + ) + } + }} + > + {v} + {selectedCount} +
+ })} +
+
+} diff --git a/webapp/src/components/Inputs/styles.ts b/webapp/src/components/Inputs/styles.ts index bc746cf..869fb14 100644 --- a/webapp/src/components/Inputs/styles.ts +++ b/webapp/src/components/Inputs/styles.ts @@ -124,6 +124,87 @@ export const useInputListStyles = createAppUseStyles<{}>(theme => { } }); +export const useInputMultiSelectStyles = createAppUseStyles<{ + open: boolean; + selected: number[]; + maxedOut: boolean; +}>(theme => { + const styles = commonInputStyle(theme); + return { + input: { + ...styles.input, + cursor: 'pointer', + position: 'relative', + display: 'flex', + gap: '1em', + + "&> label": { + cursor: 'pointer', + userSelect: 'none', + }, + "&:hover": { + color: theme.primaryAccent, + } + }, + numberSelected: (({ selected }) => ({ + display: selected.length > 0 ? 'block' : 'none', + background: theme.primary, + borderColor: theme.primaryAccent, + borderStyle: 'solid', + borderWidth: '1px', + borderRadius: '60%', + padding: '0em 0.4em' + })), + optionsWrapper: (({ open }) => ({ + position: 'absolute', + display: open ? 'flex' : 'none', + flexDirection: 'column', + gap: '0.25em', + left: '1em', + top: '4em', + height: '100%', + userSelect: 'none', + })), + arrow: (({ open }) => ({ + position: 'absolute', + right: '1em', + top: '1em', + transform: open ? 'rotate(90deg)' : 'rotate(270deg)', + })), + option: (({ maxedOut }) => ({ + display: 'flex', + flexDirection: 'row', + gap: '0.25em', + width: '100%', + padding: '0.25em 0.5em', + + '&.unselected span:nth-child(2)': { + display: 'none' + }, + '&.selected span:nth-child(2)': { + background: theme.primary, + borderColor: theme.primaryAccent, + borderStyle: 'solid', + borderWidth: '1px', + borderRadius: '40%', + padding: '0em 0.4em' + }, + + '&.unselected': { + cursor: maxedOut ? 'not-allowed' : 'pointer', + }, + '&.selected': { + cursor: 'pointer', + }, + + "&:hover": { + color: theme.secondary, + backgroundColor: theme.secondaryAccent, + } + })), + } +}); + export const useInputTextAreaStyles = createAppUseStyles<{ resizable: boolean }>(theme => { const styles = commonInputStyle(theme); return {