From b06d9bf0ec1add0794446f051c1af7282b7bca8a Mon Sep 17 00:00:00 2001 From: Maxime Lalo Date: Tue, 16 May 2023 17:35:28 +0200 Subject: [PATCH] wip --- src/front/Api/BaseApiService.ts | 5 +- .../DesignSystem/Form/BaseField.tsx | 31 +++++++++++ .../DesignSystem/Form/TextField/index.tsx | 19 ++----- .../DesignSystem/Form/TextareaField/index.tsx | 19 ++----- .../Components/DesignSystem/Form/index.tsx | 52 ++++++++++++++++--- .../Layouts/Folder/CreateFolder/index.tsx | 29 +++-------- 6 files changed, 93 insertions(+), 62 deletions(-) diff --git a/src/front/Api/BaseApiService.ts b/src/front/Api/BaseApiService.ts index 2992b609..166621bc 100644 --- a/src/front/Api/BaseApiService.ts +++ b/src/front/Api/BaseApiService.ts @@ -119,17 +119,18 @@ export default abstract class BaseApiService { responseJson = await response.json(); } catch (err: unknown) { responseJson = null; + return Promise.reject(err); } if (!response.ok) { - return Promise.reject(response); + return Promise.reject(responseJson); } return responseJson as T; } protected onError(error: unknown) { - console.error(error); + //console.error(error); } } diff --git a/src/front/Components/DesignSystem/Form/BaseField.tsx b/src/front/Components/DesignSystem/Form/BaseField.tsx index 109a776e..e36ed0f2 100644 --- a/src/front/Components/DesignSystem/Form/BaseField.tsx +++ b/src/front/Components/DesignSystem/Form/BaseField.tsx @@ -1,6 +1,7 @@ import React from "react"; import { FormContext, IFormContext } from "."; import { ValidationError } from "class-validator"; +import Typography, { ITypo, ITypoColor } from "../Typography"; export type IProps = { value?: string; @@ -34,6 +35,10 @@ export default abstract class BaseField

) { + this.context?.onFieldFocusChange(this.props.name, this, true); + } + + protected onBlur(event: React.FocusEvent) { + this.context?.onFieldFocusChange(this.props.name, this, false); + } + protected onChange(event: React.ChangeEvent) { this.context?.onFieldChange(this.props.name, this); this.setState({ value: event.currentTarget.value }); @@ -75,4 +88,22 @@ export default abstract class BaseField

{ + errors.push( + + {value} + , + ); + }); + return errors; + } } diff --git a/src/front/Components/DesignSystem/Form/TextField/index.tsx b/src/front/Components/DesignSystem/Form/TextField/index.tsx index 76cb1e53..366e6f61 100644 --- a/src/front/Components/DesignSystem/Form/TextField/index.tsx +++ b/src/front/Components/DesignSystem/Form/TextField/index.tsx @@ -18,34 +18,23 @@ export default class TextField extends BaseField { const value = this.state.value ?? ""; return ( -

+
{this.props.placeholder} {!this.props.required && " (Facultatif)"}
- {this.state.validationError !== null &&
{this.renderErrors()}
} + {this.hasError() &&
{this.renderErrors()}
} ); } - - private renderErrors(): JSX.Element[] | null { - if (!this.state.validationError || !this.state.validationError.constraints) return null; - let errors: JSX.Element[] = []; - Object.entries(this.state.validationError.constraints).forEach(([key, value]) => { - errors.push( - - {value} - , - ); - }); - return errors; - } } diff --git a/src/front/Components/DesignSystem/Form/TextareaField/index.tsx b/src/front/Components/DesignSystem/Form/TextareaField/index.tsx index 4115c557..43dd9c75 100644 --- a/src/front/Components/DesignSystem/Form/TextareaField/index.tsx +++ b/src/front/Components/DesignSystem/Form/TextareaField/index.tsx @@ -18,7 +18,7 @@ export default class TextAreaField extends BaseField { const value = this.state.value ?? ""; return ( -
+