From a5eeeca1383a0fdf50d0c76730e1ace8f03f3759 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Sun, 20 Jun 2021 14:03:22 +0200 Subject: keybind selection and deletion :tada: --- components/keybindselector.tsx | 58 ++++++++++++++++++++++++++++++++---------- 1 file changed, 45 insertions(+), 13 deletions(-) (limited to 'components') diff --git a/components/keybindselector.tsx b/components/keybindselector.tsx index 084d748..1d11b6b 100644 --- a/components/keybindselector.tsx +++ b/components/keybindselector.tsx @@ -1,32 +1,64 @@ import FilledInput from '@material-ui/core/FilledInput'; import FormControl from '@material-ui/core/FormControl'; import InputLabel from '@material-ui/core/InputLabel'; -import { useState } from 'react'; +import { useEffect, useRef, useState } from 'react'; import { v4 as uuid } from 'uuid'; function Keybind(props: { keyName: string; + onClick?: () => any; }) { - return {props.keyName}; + return {props.keyName}; } -function KeybindSelectorInner(props: { - value: string[]; -}) { - return
- {props.value.map(s => )} -
; -} +var keyBlacklist = [ + 'Shift', + 'Control', + 'Alt', + 'Meta', +]; +var keyMap = { + ' ': 'Space', +}; export default function KeybindSelector(props: { label: string; value: string[]; onChange?: (newValue: string[]) => any; }) { - var [tempID, _setTempID] = useState(uuid()); + var [inputID, _setInputID] = useState(uuid()); + + function KeybindSelectorInner() { + return
+ {props.value.map(s => + { + props.onChange && props.onChange(props.value.filter(v => v != s)); + }} + /> + )} +
; + } + + return { + e.preventDefault(); + var key = e.key; + + if (key in keyMap) key = keyMap[key]; + if (keyBlacklist.includes(key)) return; + + var newArr = Array.from(props.value); + if (!newArr.includes(key)) newArr.push(key); - return - {props.label} - } /> + props.onChange(newArr); + }} + > + {props.label} + } /> ; } -- cgit v1.2.3