import css from './index.module.css';
import {Row, 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,
  label,
  placeholder,
  height,
}) {
  return (
    <Row className='width-full' gutter={12}>
      <Col span={6} offset={2}>
        <label className={css.textRight} htmlFor={`operation_${name}`}>{label}</label>
      </Col>
      <Col span={12}>
        <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 ?? `请输入${label}`}
                  id={`operation_${name}`}
                  value={value}
                  onChange={onChange}
                  ref={ref}
                  className={css.areaFiled}
                  style={{height}}
                />
                <p className={classNames([css.errorTips, {[css.errorTipsHidden]: !errorTips}])}>
                  {errorTips}
                </p>
              </>
            );
          }}
        />
      </Col>
    </Row>
  );
};

export default OperationField;
