Remove "selected" from select.tsx

This commit is contained in:
Bastian Meissner 2021-11-26 21:06:15 +01:00
parent 30657f04d3
commit a24579420a
2 changed files with 50 additions and 43 deletions

View file

@ -1,4 +1,4 @@
import React from "react"; import React, { useState } from "react";
import styled from "styled-components"; import styled from "styled-components";
export interface IItemProps { export interface IItemProps {
@ -19,34 +19,40 @@ const List = styled.select`
padding: 0.5rem; padding: 0.5rem;
`; `;
const update = (
items: Array<IItemProps>,
onChange: (item: any) => void,
e: React.ChangeEvent<HTMLSelectElement>,
) => {
onChange(items.find((item) => item.value.toString() === e.target.value));
};
const Select = ({ const Select = ({
items, items,
onChange, onChange,
current, current,
className, className,
testId, testId,
}: ISelectProps) => ( }: ISelectProps) => {
const [selected, setSelected] = useState<string | undefined>(current);
const update = (
items: Array<IItemProps>,
onChange: (item: any) => void,
event: React.ChangeEvent<HTMLSelectElement>,
) => {
setSelected(event.target.value);
onChange(
items.find((item) => item.value.toString() === event.target.value),
);
};
return (
<List <List
data-testid={"select" + (testId ? "-" + testId : "")} data-testid={"select" + (testId ? `-${testId}` : "")}
onChange={(e) => update(items, onChange, e)} onChange={(e) => update(items, onChange, e)}
className={className} className={className}
value={selected}
> >
{items.map(({ label, value }, index) => { {items.map(({ label, value }, index) => {
if (label === current) { if (label === current) {
return ( return (
<option <option
data-testid={"option-" + (testId ? testId + "-" : "") + index} data-testid={"option-" + (testId ? `${testId}-` : "") + index}
key={[label, index].join("")} key={[label, index].join("")}
value={value.toString()} value={value.toString()}
selected
> >
{label} {label}
</option> </option>
@ -54,7 +60,7 @@ const Select = ({
} else { } else {
return ( return (
<option <option
data-testid={"option-" + (testId ? testId + "-" : "") + index} data-testid={"option-" + (testId ? `${testId}-` : "") + index}
key={[label, index].join("")} key={[label, index].join("")}
value={value.toString()} value={value.toString()}
> >
@ -64,6 +70,7 @@ const Select = ({
} }
})} })}
</List> </List>
); );
};
export default Select; export default Select;

View file

@ -101,8 +101,8 @@ const Settings = ({ themes, search }: ISettingsProps) => {
const [newLightTheme, setNewLightTheme] = useState<IThemeProps>(); const [newLightTheme, setNewLightTheme] = useState<IThemeProps>();
const [newDarkTheme, setNewDarkTheme] = useState<IThemeProps>(); const [newDarkTheme, setNewDarkTheme] = useState<IThemeProps>();
const currentLightTheme = getTheme("light").label; const currentLightTheme = getTheme("light").value;
const currentDarkTheme = getTheme("dark").label; const currentDarkTheme = getTheme("dark").value;
if (themes || search) { if (themes || search) {
return ( return (