From 388a23fa945629abf11b5d6da8522fb97b51fddb Mon Sep 17 00:00:00 2001 From: kaulfield23 Date: Tue, 19 Sep 2023 14:59:44 +0200 Subject: [PATCH] add useEffect for fetching value, remove inputRef --- .../components/EventTypeAutocomplete.tsx | 45 ++++++++++--------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/src/features/events/components/EventTypeAutocomplete.tsx b/src/features/events/components/EventTypeAutocomplete.tsx index 0b67f8245e..2125b82305 100644 --- a/src/features/events/components/EventTypeAutocomplete.tsx +++ b/src/features/events/components/EventTypeAutocomplete.tsx @@ -26,8 +26,6 @@ const useStyles = makeStyles((theme) => ({ borderColor: ({ showBorder }) => showBorder ? lighten(theme.palette.primary.main, 0.65) : '', borderRadius: 10, - //border width - // maxWidth: '200px', paddingLeft: ({ showBorder }) => (showBorder ? 10 : 0), paddingRight: ({ showBorder }) => (showBorder ? 0 : 10), transition: 'all 0.2s ease', @@ -41,11 +39,11 @@ const useStyles = makeStyles((theme) => ({ }, border: '2px dotted transparent', borderRadius: 10, - paddingRight: 10, fontSize: '1rem', + paddingRight: 10, // But invisible and positioned absolutely to not affect flow position: 'absolute', - // visibility: 'hidden', + visibility: 'hidden', }, })); @@ -81,9 +79,8 @@ const EventTypeAutocomplete: FC = ({ const [text, setText] = useState(value?.title ?? uncategorizedMsg); const [dropdownListWidth, setDropdownListWidth] = useState(0); - const classes = useStyles({ showBorder }); const spanRef = useRef(null); - const inputRef = useRef(null); + const classes = useStyles({ showBorder }); useEffect(() => { //When a user creates a new type, it is missing an event ID. @@ -97,13 +94,15 @@ const EventTypeAutocomplete: FC = ({ }, [types.length]); useEffect(() => { - if (spanRef.current && inputRef.current) { - // Add some margin to the right while in edit mode + if (spanRef.current) { const width = spanRef.current.offsetWidth; setDropdownListWidth(width); - inputRef.current.style.width = width + 'px'; } - }, [spanRef.current, inputRef.current, text]); + }, [spanRef.current, text]); + + useEffect(() => { + setText(value ? value.title : uncategorizedMsg); + }, [value]); const allTypes: EventTypeOption[] = [ ...types, @@ -122,14 +121,14 @@ const EventTypeAutocomplete: FC = ({ { const searchedResults = fuse.search(inputValue); @@ -168,10 +167,14 @@ const EventTypeAutocomplete: FC = ({ getOptionLabel={(option) => option.title!} isOptionEqualToValue={(option, value) => option.title === value.title} onBlur={() => { - // show 'uncategorized' in textField when blur + // show 'uncategorized' in textField when blurring if (!value && text !== uncategorizedMsg) { setText(uncategorizedMsg); } + //set text to previous input value when clicking away + if (value && text !== value.title) { + setText(value.title); + } onBlur(); }} onChange={(_, value) => { @@ -190,9 +193,6 @@ const EventTypeAutocomplete: FC = ({ } ); }} - onInputChange={(e, inputValue) => { - setText(inputValue); - }} onFocus={() => onFocus()} options={allTypes} renderInput={(params) => ( @@ -202,16 +202,19 @@ const EventTypeAutocomplete: FC = ({ setText(e.target.value)} + size="small" /> )}