Skip to content

Commit

Permalink
fix bug edit algorithm form in Memory Field
Browse files Browse the repository at this point in the history
  • Loading branch information
zivglik committed May 8, 2022
1 parent 1b5bd45 commit 12b7481
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ const AddAlgorithmForm = ({ onToggle, onSubmit, algorithmValue }) => {

useEffect(() => {
// init values in fields

if (keyValueObject !== undefined) {
// Edit algorithm
const schemaObjectForm = form.getFieldsValue();
Expand All @@ -109,6 +110,7 @@ const AddAlgorithmForm = ({ onToggle, onSubmit, algorithmValue }) => {
flattenObjKeyValue(keyValueObject)
);
setBuildType(toSelectedBuildType(keyValueObject.type));

form.setFieldsValue(objValuesForm);
} else {
// add new algorithm
Expand Down Expand Up @@ -279,7 +281,7 @@ const AddAlgorithmForm = ({ onToggle, onSubmit, algorithmValue }) => {
AddAlgorithmForm.propTypes = {
// TODO: detail the props
// eslint-disable-next-line
algorithmValue: PropTypes.object,
algorithmValue: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
onToggle: PropTypes.func.isRequired,
onSubmit: PropTypes.func,
};
Expand Down
16 changes: 10 additions & 6 deletions src/Routes/SidebarRight/AddAlgorithm/MemoryField.react.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useEffect, useState } from 'react';
import PropTypes from 'prop-types';

import Icon from '@ant-design/icons';
Expand All @@ -12,18 +12,22 @@ const MemoryField = React.forwardRef(
// eslint-disable-next-line
({ onChange, children, value, tooltipTitle, min, iconType }, ref) => {
const [numberInitial, unitInitial] = parseUnit(value);

const [number, setNumber] = useState(numberInitial);
const [numberMem, setNumberMem] = useState(numberInitial);
const [unit, setUnit] = useState(unitInitial);

useEffect(() => {
setNumberMem(numberInitial);
setUnit(unitInitial);
}, [numberInitial, unitInitial]);

const onNumber = target => {
setNumber(target);
setNumberMem(target);
onChange(target === null || target === '' ? null : `${target}${unit}`);
};

const onSelect = target => {
setUnit(target);
onChange(number === null ? null : `${number}${target}`);
onChange(numberMem === null ? null : `${numberMem}${target}`);
};

const Wrapper = tooltipTitle ? Tooltip : React.Fragment;
Expand All @@ -34,7 +38,7 @@ const MemoryField = React.forwardRef(
<Wrapper {...wrapperProps}>
<Input.Group compact>
{iconType && <Icon type={iconType} />}
<InputNumber min={min} value={number} onChange={onNumber} />
<InputNumber min={min} value={numberMem} onChange={onNumber} />
<Select style={selectStyle} value={unit} onChange={onSelect}>
{children}
</Select>
Expand Down
3 changes: 2 additions & 1 deletion src/Routes/SidebarRight/AddAlgorithm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ AddAlgorithm.propTypes = {
// eslint-disable-next-line
onSubmit: PropTypes.func,
// eslint-disable-next-line react/forbid-prop-types
algorithmValue: PropTypes.object.isRequired,
algorithmValue: PropTypes.oneOfType([PropTypes.string, PropTypes.object])
.isRequired,
};

export default memo(AddAlgorithm);

0 comments on commit 12b7481

Please sign in to comment.