From 91f47653bf3cd9d140c12ee7b78066d4e61bcbec Mon Sep 17 00:00:00 2001 From: Jerko Steiner Date: Wed, 3 Apr 2019 17:50:27 +0800 Subject: [PATCH] Fix SiteForm properties --- packages/client/src/crud/CRUDActions.ts | 17 ++++++++++------- packages/client/src/crud/CRUDForm.tsx | 18 ++++++++++++++---- packages/client/src/crud/CRUDReducer.ts | 2 +- packages/client/src/crud/index.ts | 1 + 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/packages/client/src/crud/CRUDActions.ts b/packages/client/src/crud/CRUDActions.ts index e4b1d3a..4d804de 100644 --- a/packages/client/src/crud/CRUDActions.ts +++ b/packages/client/src/crud/CRUDActions.ts @@ -6,8 +6,14 @@ import {TCRUDCreateAction} from './TCRUDAction' import {TCRUDEditAction} from './TCRUDAction' import {TCRUDMethod} from './TCRUDMethod' -type TAction = - TFilter, {method: Method, status: 'pending'}> +type TAction = + TFilter , {method: Method, status: 'pending'}> + +export interface ICRUDChangeParams { + id?: number + key: keyof T & string + value: string +} export class SaveActionCreator< T extends IRoutes, @@ -166,11 +172,8 @@ export class FormActionCreator { } } - change = (params: { - id?: number, - key: keyof T, - value: string - }): TCRUDChangeAction => { + change = (params: ICRUDChangeParams) + : TCRUDChangeAction => { return { payload: params, type: this.actionType, diff --git a/packages/client/src/crud/CRUDForm.tsx b/packages/client/src/crud/CRUDForm.tsx index 5d93bf8..3542248 100644 --- a/packages/client/src/crud/CRUDForm.tsx +++ b/packages/client/src/crud/CRUDForm.tsx @@ -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 { - onChange(key: K, value: string): void + id?: number + onChange(params: ICRUDChangeParams): void Icon?: React.ComponentType error?: string label: string @@ -14,8 +16,11 @@ export interface ICRUDFieldProps { value: string } +export type TCRUDErrors = Partial> + export interface ICRUDFormProps { - errors: Partial> + errors: TCRUDErrors + id?: number item: T error: string submitText: string @@ -28,14 +33,18 @@ export interface ICRUDFormProps { }> onSubmit: (t: T) => void - onChange(key: K, value: string): void + onChange(params: ICRUDChangeParams): void } export class CRUDField extends React.PureComponent> { handleChange = (e: React.ChangeEvent) => { 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 extends React.PureComponent> { const value = item[field.name] return ( + id={this.props.id} key={field.name} name={field.name} label={field.label} diff --git a/packages/client/src/crud/CRUDReducer.ts b/packages/client/src/crud/CRUDReducer.ts index 205368c..a154ed5 100644 --- a/packages/client/src/crud/CRUDReducer.ts +++ b/packages/client/src/crud/CRUDReducer.ts @@ -19,7 +19,7 @@ export interface ICRUDState { readonly form: ICRUDForm } -interface ICRUDForm { +export interface ICRUDForm { readonly create: { readonly item: Pick>, readonly errors: Partial> diff --git a/packages/client/src/crud/index.ts b/packages/client/src/crud/index.ts index 49c1af7..b93f8dd 100644 --- a/packages/client/src/crud/index.ts +++ b/packages/client/src/crud/index.ts @@ -1,4 +1,5 @@ export * from './CRUDActions' +export * from './CRUDForm' export * from './CRUDList' export * from './CRUDReducer' export * from './TCRUDAction'