import React, { useState, useEffect } from 'react';
import { Form, InputNumber } from 'antd';
import { findValueByNamePath } from '../../utils';

export default ({ node, propKey, config, onChangePathValue }) => {
  const { name, label, extra = {}, company } = config;
  const { step = 1 } = extra;
  const monitor = node[propKey];
  const [stateValue, setStateValue] = useState(null);

  const handleChange = e => {
    setStateValue(e);
    onChangePathValue({ path: name, value: e });
  };

  useEffect(() => {
    const value = findValueByNamePath(name, monitor);
    setStateValue(value);
  }, [monitor]);

  return (
    <Form.Item label={label}>
      <InputNumber value={stateValue} onChange={handleChange} step={step} />
      {company && <span> {company}</span>}
    </Form.Item>
  );
};
