StyleInput: make sure component behaves as controlled on first render

We made the error of setting the value of this component statically,
rather than based on global IconWeight state. This meant that it was not
reflecting the current weight on first render, if it was anything other
than 'regular', for example when set by a URL param.
This commit is contained in:
rektdeckard
2021-01-08 15:49:53 -05:00
parent c3787fcde0
commit 14c8807234

View File

@@ -1,5 +1,5 @@
import React from "react";
import { useSetRecoilState } from "recoil";
import React, { useMemo } from "react";
import { useRecoilState } from "recoil";
import Select from "react-dropdown-select";
import { PencilLine } from "phosphor-react";
@@ -45,7 +45,12 @@ const options: WeightOption[] = [
type StyleInputProps = {};
const StyleInput: React.FC<StyleInputProps> = () => {
const setStyle = useSetRecoilState(iconWeightAtom);
const [style, setStyle] = useRecoilState(iconWeightAtom);
const currentStyle = useMemo(
() => [options.find((option) => option.value === style)!!],
[style]
);
const handleStyleChange = (values: WeightOption[]) =>
setStyle(values[0].value as IconStyle);
@@ -53,7 +58,7 @@ const StyleInput: React.FC<StyleInputProps> = () => {
return (
<Select
options={options}
values={[options[2]]}
values={currentStyle}
searchable={false}
labelField="key"
onChange={handleStyleChange}