import React from 'react';
import { get } from 'lodash';

export interface TransformSelectValueProps {
  value?: any;
  onChange?: (value: any) => void;
  dictionary: any[];
}

const TransformSelectValue: React.FC<TransformSelectValueProps> = props => {
  const { value, onChange, dictionary, children } = props;
  // 取出id
  const newValue = get(value, 'id');

  const handleChange = (value: string) => {
    // 用id 从字典查询到当前对象
    const newValue = dictionary?.find(item => item?.id === value);
    onChange && onChange(newValue);
  };

  return (
    /* eslint-disable-next-line */
    <>
      {React.Children.map(children, (child: any) =>
        React.cloneElement(child, {
          value: newValue,
          onChange: handleChange,
        }),
      )}
    </>
  );
};

export default TransformSelectValue;
