Skip to content

Commit

Permalink
Revert "fix(ArgControl): unbox the value to simplify the code"
Browse files Browse the repository at this point in the history
This reverts commit e42fcf0.
  • Loading branch information
gaetanmaisse committed Aug 11, 2021
1 parent 1219016 commit 5f589a2
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/components/src/blocks/ArgsTable/ArgControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@ export const ArgControl: FC<ArgControlProps> = ({ row, arg, updateArgs }) => {
const { key, control } = row;

const [isFocused, setFocused] = useState(false);
const [value, setValue] = useState(() => arg);
// box because arg can be a fn (e.g. actions) and useState calls fn's
const [boxedValue, setBoxedValue] = useState({ value: arg });

useEffect(() => {
if (!isFocused) setValue(arg);
if (!isFocused) setBoxedValue({ value: arg });
}, [isFocused, arg]);

const onChange = useCallback(
(argVal: any) => {
setValue(argVal);
setBoxedValue({ value: argVal });
updateArgs({ [key]: argVal });
return argVal;
},
Expand All @@ -64,7 +65,7 @@ export const ArgControl: FC<ArgControlProps> = ({ row, arg, updateArgs }) => {

// row.name is a display name and not a suitable DOM input id or name - i might contain whitespace etc.
// row.key is a hash key and therefore a much safer choice
const props = { name: key, argType: row, value, onChange, onBlur, onFocus };
const props = { name: key, argType: row, value: boxedValue.value, onChange, onBlur, onFocus };
const Control = Controls[control.type] || NoControl;
return <Control {...props} {...control} controlType={control.type} />;
};

0 comments on commit 5f589a2

Please sign in to comment.