Fix SiteForm properties

This commit is contained in:
Jerko Steiner 2019-04-03 17:50:27 +08:00
parent 0b1cd203c3
commit 91f47653bf
4 changed files with 26 additions and 12 deletions

View File

@ -6,8 +6,14 @@ import {TCRUDCreateAction} from './TCRUDAction'
import {TCRUDEditAction} from './TCRUDAction'
import {TCRUDMethod} from './TCRUDMethod'
type TAction<T, ActionType extends string, Method extends TCRUDMethod> =
TFilter<TCRUDAction<T, ActionType>, {method: Method, status: 'pending'}>
type TAction <T, ActionType extends string, Method extends TCRUDMethod> =
TFilter<TCRUDAction<T, ActionType> , {method: Method, status: 'pending'}>
export interface ICRUDChangeParams<T> {
id?: number
key: keyof T & string
value: string
}
export class SaveActionCreator<
T extends IRoutes,
@ -166,11 +172,8 @@ export class FormActionCreator<T, ActionType extends string> {
}
}
change = (params: {
id?: number,
key: keyof T,
value: string
}): TCRUDChangeAction<T, ActionType> => {
change = (params: ICRUDChangeParams<T>)
: TCRUDChangeAction<T, ActionType> => {
return {
payload: params,
type: this.actionType,

View File

@ -1,10 +1,12 @@
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<T> {
onChange<K extends keyof T>(key: K, value: string): void
id?: number
onChange<K extends keyof T>(params: ICRUDChangeParams<T>): void
Icon?: React.ComponentType
error?: string
label: string
@ -14,8 +16,11 @@ export interface ICRUDFieldProps<T> {
value: string
}
export type TCRUDErrors<T> = Partial<Record<keyof T & string, string>>
export interface ICRUDFormProps<T> {
errors: Partial<Record<keyof T & string, string>>
errors: TCRUDErrors<T>
id?: number
item: T
error: string
submitText: string
@ -28,14 +33,18 @@ export interface ICRUDFormProps<T> {
}>
onSubmit: (t: T) => void
onChange<K extends keyof T>(key: K, value: string): void
onChange(params: ICRUDChangeParams<T>): void
}
export class CRUDField<T> extends React.PureComponent<ICRUDFieldProps<T>> {
handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const {onChange} = this.props
const {value} = e.target
onChange(this.props.name, value)
onChange({
id: this.props.id,
key: this.props.name,
value,
})
}
render() {
const {label, name, value, placeholder} = this.props
@ -76,6 +85,7 @@ export class CRUDForm<T> extends React.PureComponent<ICRUDFormProps<T>> {
const value = item[field.name]
return (
<CRUDField<T>
id={this.props.id}
key={field.name}
name={field.name}
label={field.label}

View File

@ -19,7 +19,7 @@ export interface ICRUDState<T extends ICRUDEntity> {
readonly form: ICRUDForm<T>
}
interface ICRUDForm<T extends ICRUDEntity> {
export interface ICRUDForm<T extends ICRUDEntity> {
readonly create: {
readonly item: Pick<T, Exclude<keyof T, 'id'>>,
readonly errors: Partial<Record<keyof T, string>>

View File

@ -1,4 +1,5 @@
export * from './CRUDActions'
export * from './CRUDForm'
export * from './CRUDList'
export * from './CRUDReducer'
export * from './TCRUDAction'