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..43898868 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 });
+ this.setState({ value: event.currentTarget.value, validationError: null });
if (this.props.onChange) {
this.props.onChange(event);
}
}
+
+ protected hasError(): boolean {
+ return this.state.validationError !== null;
+ // if(!this.context) return false;
+ // if(!this.context.hasOneFocusedInput() && this.state.validationError !== null) return true;
+ // return this.state.validationError !== null && this.context.isInputFocused(this.props.name);
+ }
+
+ protected 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/SelectField/index.tsx b/src/front/Components/DesignSystem/Form/SelectField/index.tsx
index 6abd2dbe..bf08aa3b 100644
--- a/src/front/Components/DesignSystem/Form/SelectField/index.tsx
+++ b/src/front/Components/DesignSystem/Form/SelectField/index.tsx
@@ -15,7 +15,7 @@ type IProps = {
hasBorderRightCollapsed?: boolean;
placeholder?: string;
className?: string;
- name?: string;
+ name: string;
disabled: boolean;
errors?: ValidationError;
};
@@ -60,53 +60,58 @@ export default class SelectField extends React.Component {
public override render(): JSX.Element {
const selectedOption = this.state.selectedOption ?? this.props.selectedOption;
return (
-
- {selectedOption &&
}
-
+
+
+ {selectedOption &&
}
+
-
- {this.props.options.map((option, index) => (
- - this.onSelect(option, e)}>
-
{option.icon}
- {option.label}
-
- ))}
-
+
+ {this.props.options.map((option, index) => (
+ - this.onSelect(option, e)}>
+
{option.icon}
+ {option.label}
+
+ ))}
+
- {this.state.isOpen &&
}
+ {this.state.isOpen &&
}
+
{this.state.errors !== null &&
{this.renderErrors()}
}
);
@@ -172,16 +177,12 @@ export default class SelectField extends React.Component
{
this.toggle(e);
}
- private renderErrors(): JSX.Element[] | null {
- if (!this.state.errors || !this.state.errors.constraints) return null;
- let errors: JSX.Element[] = [];
- Object.entries(this.state.errors.constraints).forEach(([key, value]) => {
- errors.push(
-
- {value}
- ,
- );
- });
- return errors;
+ private renderErrors(): JSX.Element | null {
+ if (!this.state.errors) return null;
+ return (
+
+ {this.props.placeholder} est requis
+
+ );
}
}
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.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 (
-
+
);
@@ -42,17 +44,4 @@ export default class TextAreaField extends BaseField
{
value: this.props.defaultValue ?? "",
});
}
-
- 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/index.tsx b/src/front/Components/DesignSystem/Form/index.tsx
index 7ad9b593..94a9f082 100644
--- a/src/front/Components/DesignSystem/Form/index.tsx
+++ b/src/front/Components/DesignSystem/Form/index.tsx
@@ -8,25 +8,39 @@ export type IFormContext = {
setField: (name: string, field: IBaseField) => void;
unSetField: (name: string) => void;
onFieldChange: (name: string, field: IBaseField) => void;
+ onFieldFocusChange: (name: string, field: IBaseField, focused: boolean) => void;
+ isInputFocused: (name: string) => boolean;
+ hasOneFocusedInput: () => boolean;
};
type IFields = {
[key: string]: IBaseField;
};
-type IState = {};
+type IState = {
+ inputFocused: {
+ name: string;
+ field: IBaseField;
+ focused: boolean;
+ };
+};
export type IProps = {
+ onFieldFocusChange?: (name: string, field: IBaseField, focused: boolean) => void;
onFieldChange?: (name: string, field: IBaseField) => void;
- onSubmit?: (
- e: React.FormEvent | null,
- values: { [key: string]: string },
- ) => void;
+ onSubmit?: (e: React.FormEvent | null, values: { [key: string]: string }) => void;
className?: string;
children?: ReactNode;
};
-export const FormContext = React.createContext({ setField: () => {}, unSetField: () => {}, onFieldChange: () => {} });
+export const FormContext = React.createContext({
+ setField: () => {},
+ unSetField: () => {},
+ onFieldChange: () => {},
+ onFieldFocusChange: () => {},
+ isInputFocused: () => false,
+ hasOneFocusedInput: () => false,
+});
export default class Form extends React.Component {
protected fields: IFields = {};
@@ -35,12 +49,22 @@ export default class Form extends React.Component {
constructor(props: IProps) {
super(props);
- this.state = {};
+ this.state = {
+ inputFocused: {
+ name: "",
+ field: {} as IBaseField,
+ focused: false,
+ },
+ };
+
this.setField = this.setField.bind(this);
this.unSetField = this.unSetField.bind(this);
this.onFieldChange = this.onFieldChange.bind(this);
this.onSubmit = this.onSubmit.bind(this);
this.formRef = React.createRef();
+ this.onFieldFocusChange = this.onFieldFocusChange.bind(this);
+ this.isInputFocused = this.isInputFocused.bind(this);
+ this.hasOneFocusedInput = this.hasOneFocusedInput.bind(this);
}
public override render() {
@@ -50,6 +74,9 @@ export default class Form extends React.Component {
setField: this.setField,
unSetField: this.unSetField,
onFieldChange: this.onFieldChange,
+ onFieldFocusChange: this.onFieldFocusChange,
+ isInputFocused: this.isInputFocused,
+ hasOneFocusedInput: this.hasOneFocusedInput,
}}>