import React from 'react'
import styled from 'styled-components'
import { IJsonSchema } from '../../types/IJsonSchema'
import { InputWrapper } from '../InputWrapper/InputWrapper'

export type TextAreaProps = {
  value: string
  name: string
  label?:string
  placeholder?: string
  rest?: {[x: string]: unknown};
  /**
   * schema for validation
   */
  jsonSchema?: IJsonSchema
  /**
   * custom styling for text input
   */
  css?: string
  onChange: (change: string ) => void
}

export const TextArea = ({
  value,
  name,
  label,
  placeholder,
  onChange,
  jsonSchema,
  css,
  ...rest }: TextAreaProps):JSX.Element => {

  const handleChange = (event:React.ChangeEvent<HTMLTextAreaElement>) => {
    onChange(event.target.value)
  }

  return (    
    <InputWrapper 
      label={label}
      jsonSchema={jsonSchema}
      value={value}>
      <StyledTextArea
        value={value}
        name={name}
        rows={1}
        placeholder={placeholder}
        onChange={handleChange}
        css={css}
        {...rest} />
    </InputWrapper>
  )
}

const StyledTextArea = styled.textarea<{css?:string}>`
  outline: none;
  border: none;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji";
  appearance: none;
  box-sizing: border-box;
    font-size: 1rem;
  color: #000;
  text-align: left;   
  border-radius: 4px;
  border: none;
  background-color: #fff;
  transition: border-color 100ms ease 0s;
  padding: 0;
  height: auto;
  width: 100%;
  -webkit-tap-highlight-color: transparent;
  appearance: none;

  &:focus {
    outline:none;
    border-color: #228be6;
  } 
  
  ${(p) => p.css}
`
