import React, { useState, useEffect } from 'react';
import { Form } from 'antd';
import Group from '../../components/group';
import Colors from '../colors';
import { findValueByNamePath } from '../../utils';

export default ({ node, propKey, config, onChangePathValue }) => {
  const defaultGroup = {
    label: config.label,
  };
  const { name, group = defaultGroup } = config;
  const monitor = findValueByNamePath(name, node[propKey]);
  const [state, setState] = useState();

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

  return (
    <Group group={group}>
      <Form.Item>
        <Colors
          name={name}
          value={state}
          onChangePathValue={onChangePathValue}
        />
      </Form.Item>
    </Group>
  );
};
