import css from './index.module.css';
import {Row, Space, Col, Input} from 'antd';
import classNames from 'classnames';
import {FC} from 'react';
import {Controller, UseControllerProps} from 'react-hook-form';

type Props = {
  label: string;
  placeholder?: string;
  width: string;
  height: string;
} & UseControllerProps<any, any>;

const OperationField: FC<Props> = function({
  control,
  name,
  width,
  label,
  placeholder,
  height,
}) {
  return (
    <Row style={{width}}>
      <Space direction='vertical' style={{width: '100%'}}>
        <Col>
          <label htmlFor={`operation_${name}`}>{label}</label>
        </Col>
        <Col>
          <Controller
            name={name}
            control={control}
            render={function({field: {value, onChange, ref}, formState: {errors}}) {
              const errorTips = errors[name]?.message?.toString() ?? '';
              return (
                <>
                  <Input.TextArea
                    name={name}
                    placeholder={placeholder ?? '请输入'}
                    id={`operation_${name}`}
                    value={value}
                    onChange={onChange}
                    ref={ref}
                    style={{resize: 'none', height}}
                  />
                  <p className={classNames([css.errorTips, {[css.errorTipsHidden]: !errorTips}])}>
                    {errorTips}
                  </p>
                </>
              );
            }}
          />

        </Col>
      </Space>
    </Row>
  );
};

export default OperationField;
