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

export type TextInputProps = {
  value: string
  name: string
  label?:string
  placeholder?: string
  /**
   * schema for validation
   */
  jsonSchema?:IJsonSchema
  /**
   * custom styling for text input
   */
  css?: string
  onChange: (change: string ) => void
}

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

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

  return (
    <InputWrapper 
      label={label} 
      jsonSchema={jsonSchema} 
      value={value}>
      <StyledTextInput
        type="text"
        value={value||''}
        name={name}
        placeholder={placeholder}
        onChange={handleChange}
        css={css}
      />
    </InputWrapper>
  )
}

const StyledTextInput = styled.input<{css?: string}>`
  width:100%;
  font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji;
  height: 36px;
  -webkit-tap-highlight-color: transparent;
  line-height: 34px;
  resize: none;
  box-sizing: border-box;
    font-size: 1rem;
  color: #000;
  text-align: left;
  min-height: 36px;
  padding-left: 5px;
  padding-right: 12px;
  border-radius: 4px;
  border: none;
  background-color: #fff;
  -webkit-transition: border-color 100ms ease;
  transition: border-color 100ms ease;

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