🐛 Reworking select input

This commit is contained in:
Maxime Lalo 2023-04-27 17:03:43 +02:00
parent 281126df6d
commit 5aeb14a38c

View File

@ -1,5 +1,6 @@
import React, { ReactNode } from "react"; import React, { ReactNode } from "react";
import BaseField, { IError, IProps as IBaseFieldProps } from "./Elements/BaseField";
import BaseField, { IProps as IBaseFieldProps, IError } from "./Elements/BaseField";
export type IBaseField = BaseField<IBaseFieldProps>; export type IBaseField = BaseField<IBaseFieldProps>;
@ -90,13 +91,25 @@ export default class Form extends React.Component<IProps, IState> {
if (this.props.onValidated) this.props.onValidated(); if (this.props.onValidated) this.props.onValidated();
const elementsValues = this.getAllChildrenFields(e).reduce( const allChildren = this.getAllChildrenFields(e);
const elementsValues = allChildren.reduce(
(obj, element) => ({ ...obj, [element.getAttribute("name") ?? ""]: (element as any).value }), (obj, element) => ({ ...obj, [element.getAttribute("name") ?? ""]: (element as any).value }),
{}, {},
); );
const radioInputs = allChildren.filter((e) => e.getAttribute("type") === "radio").filter((e) => (e as any).checked);
const radioInputsValues = radioInputs.reduce(
(obj, element) => ({ ...obj, [element.getAttribute("name") ?? ""]: (element as any).value }),
{},
);
const allInputs = {
...elementsValues,
...radioInputsValues,
}
if (this.props.onSubmit) { if (this.props.onSubmit) {
this.props.onSubmit(e, elementsValues, this.onSubmitErrorApi); this.props.onSubmit(e, allInputs, this.onSubmitErrorApi);
} }
return { values: elementsValues }; return { values: elementsValues };