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:
@@ -1,5 +1,5 @@
|
|||||||
import React from "react";
|
import React, { useMemo } from "react";
|
||||||
import { useSetRecoilState } from "recoil";
|
import { useRecoilState } from "recoil";
|
||||||
import Select from "react-dropdown-select";
|
import Select from "react-dropdown-select";
|
||||||
import { PencilLine } from "phosphor-react";
|
import { PencilLine } from "phosphor-react";
|
||||||
|
|
||||||
@@ -45,7 +45,12 @@ const options: WeightOption[] = [
|
|||||||
type StyleInputProps = {};
|
type StyleInputProps = {};
|
||||||
|
|
||||||
const StyleInput: React.FC<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[]) =>
|
const handleStyleChange = (values: WeightOption[]) =>
|
||||||
setStyle(values[0].value as IconStyle);
|
setStyle(values[0].value as IconStyle);
|
||||||
@@ -53,7 +58,7 @@ const StyleInput: React.FC<StyleInputProps> = () => {
|
|||||||
return (
|
return (
|
||||||
<Select
|
<Select
|
||||||
options={options}
|
options={options}
|
||||||
values={[options[2]]}
|
values={currentStyle}
|
||||||
searchable={false}
|
searchable={false}
|
||||||
labelField="key"
|
labelField="key"
|
||||||
onChange={handleStyleChange}
|
onChange={handleStyleChange}
|
||||||
|
|||||||
Reference in New Issue
Block a user