import React from 'react' import {Control, Field, Label, Icon, Input} from 'bloomer' import {ICRUDChangeParams} from './CRUDActions' export type TCRUDFieldType = 'text' | 'password' | 'number' | 'email' | 'tel' export interface ICRUDFieldProps { id?: number onChange(params: ICRUDChangeParams): void Icon?: React.ComponentType error?: string label: string placeholder?: string name: keyof T & string type: TCRUDFieldType value: string } export type TCRUDErrors = Partial> export interface ICRUDField { Icon?: React.ComponentType label: string placeholder?: string name: keyof T & string type: TCRUDFieldType } export interface ICRUDFormProps { errors: TCRUDErrors id?: number item?: T error: string submitText: string fields: Array> onSubmit: (t: T) => void onChange(params: ICRUDChangeParams): void } export class CRUDField extends React.PureComponent> { handleChange = (e: React.ChangeEvent) => { const {onChange} = this.props const {value} = e.target onChange({ id: this.props.id, key: this.props.name, value, }) } render() { const {label, name, value, placeholder} = this.props return ( {!!this.props.Icon && ( )} ) } } export class CRUDForm extends React.PureComponent> { static defaultProps = { errors: {}, } handleSubmit = (e: React.FormEvent) => { e.preventDefault() const {onSubmit, item} = this.props if (item) { onSubmit(item) } } render() { const {fields, item} = this.props return (

{this.props.error}

{!!item && fields.map(field => { const error = this.props.errors[field.name] const value = item[field.name] return ( id={this.props.id} key={field.name} name={field.name} label={field.label} onChange={this.props.onChange} placeholder={field.placeholder} error={error} Icon={field.Icon} value={String(value)} type={field.type} /> ) })}
) } }