multiselect numbered

This commit is contained in:
ari committed 2022-04-18 20:00:22 -04:00
1 parent 9e9cea9269
commit acb12e73e8
3 files changed
+186

No files matched your search

@@ -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<typeof InputMultiSelect>;
const Template: ComponentStory<typeof InputMultiSelect> = args => {
const [value, setValue] = useState<number[]>(args.selected)
useEffect(() => {
setValue(args.selected);
}, [args.selected])
return <div className="page">
<InputMultiSelect {...args} onChange={a => {
setValue(a);
args.onChange(a);
}} options={args.options} selected={value} />
</div>
}
export const Simple = Template.bind({})
Simple.args = {
label: "Room Tags",
options: [
"Bears",
"Cats",
"Kitchen",
"Bathroom",
],
selected: [1],
}
@@ -0,0 +1,66 @@
import { ArrowIcon } from "components/Icons";
import InputWrapper from "./InputWrapper";
import { useInputMultiSelectStyles } from "./styles";
import { useState } from 'react';
export interface Props<T extends string> {
label: string;
selected: Array<number>;
options: readonly T[];
onChange: (i: Array<number>) => void;
max?: number;
id?: string;
className?: string;
}
export default function InputMultiSelect<T extends string>({
selected = [], options, onChange, label, max,
...selattr
}: Props<T>) {
// 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 <div>
<div className={classes.input} onClick={toggle}>
<label>{label}</label>
<span className={classes.numberSelected}>{selected.length}</span>
<ArrowIcon color="white" className={classes.arrow} />
</div>
<div className={classes.optionsWrapper}>
{options.map((v, i) => {
const selectedCount = selected.indexOf(i);
const isSelected = selectedCount > -1;
return <div
key={i}
className={[classes.option, isSelected ? 'selected' : 'unselected'].join(' ')}
onClick={() => {
if (!isSelected) {
if (!maxedOut) {
onChange([...selected, i])
}
} else {
onChange(
selected.slice(0, selectedCount).concat(selected.slice(selectedCount + 1))
)
}
}}
>
<span>{v}</span>
<span>{selectedCount}</span>
</div>
})}
</div>
</div>
}
+81
View File
@@ -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 {