import React, { useState, useEffect } from 'react';
import { Form, Input } from 'antd';
import { get } from 'lodash';

const { TextArea } = Input;
export default ({ node, propKey, config, onChangePathValue, rwdWidth }) => {
  const { name, label, extra = {} } = config;
  const { frozenInRwd } = extra;
  const [stateValue, setStateValue] = useState();
  const monitor = get(node[propKey], name);

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

  useEffect(() => {
    setStateValue(monitor);
  }, [monitor]);

  return (
    <Form.Item label={label}>
      <TextArea
        value={stateValue}
        disabled={frozenInRwd && !!rwdWidth}
        onChange={handleChange}
      />
    </Form.Item>
  );
};
