Skip to content

Commit

Permalink
refactor(slash): simplification du field form
Browse files Browse the repository at this point in the history
Dans l'optique de ne plus utiliser de cloneElement, et de
simplifier le code, j'ai refacto le code qui pilote les
champs *Input.
Pour le moment, seul le TextInput utilise ce nouveau code, les autres
continuent d'utiliser le LegacyField.
  • Loading branch information
JLou committed Jan 30, 2025
1 parent 96f5b30 commit 461a95c
Show file tree
Hide file tree
Showing 18 changed files with 248 additions and 151 deletions.
8 changes: 4 additions & 4 deletions slash/react/src/Form/Checkbox/CheckboxInput.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ComponentProps, forwardRef } from "react";

import { Field, useOptionsWithId } from "../core";
import { LegacyField, useOptionsWithId } from "../core";
import { Checkbox } from "./Checkbox";
import { CheckboxModes } from "./CheckboxModes";

type Props = Omit<
ComponentProps<typeof Checkbox> & ComponentProps<typeof Field>,
ComponentProps<typeof Checkbox> & ComponentProps<typeof LegacyField>,
"children" | "placeholder"
>;

Expand Down Expand Up @@ -38,7 +38,7 @@ const CheckboxInput = forwardRef<HTMLInputElement, Props>(
const newOptions = useOptionsWithId(options);

return (
<Field
<LegacyField
label={label}
id={newOptions[0].id}
message={message}
Expand All @@ -57,7 +57,7 @@ const CheckboxInput = forwardRef<HTMLInputElement, Props>(
ref={inputRef}
{...checkboxProps}
/>
</Field>
</LegacyField>
);
},
);
Expand Down
8 changes: 4 additions & 4 deletions slash/react/src/Form/Choice/ChoiceInput.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { forwardRef, type ComponentProps } from "react";
import { Field, useInputClassModifier, useOptionsWithId } from "../core";
import { LegacyField, useInputClassModifier, useOptionsWithId } from "../core";
import { Choice } from "./Choice";

type Props = ComponentProps<typeof Choice> &
Omit<ComponentProps<typeof Field>, "children">;
Omit<ComponentProps<typeof LegacyField>, "children">;

const defaultOptions = [
{ label: "Oui", value: true, id: "radioItemTrue" },
Expand Down Expand Up @@ -49,7 +49,7 @@ const ChoiceInput = forwardRef<HTMLInputElement, Props>(
value: o.value === "true",
}));
return (
<Field
<LegacyField
label={label}
id={firstId}
message={message}
Expand All @@ -70,7 +70,7 @@ const ChoiceInput = forwardRef<HTMLInputElement, Props>(
required={required}
disabled={disabled}
/>
</Field>
</LegacyField>
);
},
);
Expand Down
13 changes: 9 additions & 4 deletions slash/react/src/Form/Date/DateInput.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { ComponentPropsWithoutRef, ReactNode, useId } from "react";
import { Field, FieldInput, HelpMessage, useInputClassModifier } from "../core";
import {
FieldInput,
HelpMessage,
LegacyField,
useInputClassModifier,
} from "../core";
import { Date } from "./Date";

type Props = Omit<ComponentPropsWithoutRef<typeof Date>, "placeholderText"> &
ComponentPropsWithoutRef<typeof Field> & {
ComponentPropsWithoutRef<typeof LegacyField> & {
placeholder?: string;
helpMessage?: ReactNode;
children?: ReactNode;
Expand Down Expand Up @@ -35,7 +40,7 @@ const DateInput = ({
required,
);
return (
<Field
<LegacyField
label={label}
id={inputId}
message={message}
Expand All @@ -61,7 +66,7 @@ const DateInput = ({
{children}
</FieldInput>
<HelpMessage message={helpMessage} isVisible={!message} />
</Field>
</LegacyField>
);
};

Expand Down
13 changes: 9 additions & 4 deletions slash/react/src/Form/File/FileInput.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import "@axa-fr/design-system-slash-css/dist/Form/File/File.scss";
import { ComponentPropsWithoutRef, ReactNode, useId } from "react";
import { Field, FieldInput, HelpMessage, useInputClassModifier } from "../core";
import {
FieldInput,
HelpMessage,
LegacyField,
useInputClassModifier,
} from "../core";
import { CustomFile, File } from "./File";
import { FileTable } from "./FileTable";

type FieldProps = ComponentPropsWithoutRef<typeof Field>;
type FieldProps = ComponentPropsWithoutRef<typeof LegacyField>;
type FileProps = ComponentPropsWithoutRef<typeof File>;
type FileTableProps = ComponentPropsWithoutRef<typeof FileTable>;

Expand Down Expand Up @@ -56,7 +61,7 @@ const FileInput = ({
);
const rowModifier = `${inputFieldClassModifier} label-top`;
return (
<Field
<LegacyField
label={label}
id={inputId}
message={message}
Expand Down Expand Up @@ -91,7 +96,7 @@ const FileInput = ({
onClick={(selectedId) => onDeleteClick(selectedId, inputId)}
classModifier={classModifier}
/>
</Field>
</LegacyField>
);
};

Expand Down
13 changes: 9 additions & 4 deletions slash/react/src/Form/MultiSelect/MultiSelectInput.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import "@axa-fr/design-system-slash-css/dist/Form/MultiSelect/MultiSelect.scss";

import { useId, type ComponentProps, type ReactNode } from "react";
import { Field, FieldInput, HelpMessage, useInputClassModifier } from "../core";
import {
FieldInput,
HelpMessage,
LegacyField,
useInputClassModifier,
} from "../core";
import { MultiSelect } from "./MultiSelect";

type Props = ComponentProps<typeof Field> &
type Props = ComponentProps<typeof LegacyField> &
ComponentProps<typeof MultiSelect> & {
helpMessage?: ReactNode;
};
Expand Down Expand Up @@ -37,7 +42,7 @@ const MultiSelectInput = ({
const inputId = id || generatedId;

return (
<Field
<LegacyField
label={label}
id={inputId}
message={message}
Expand All @@ -62,7 +67,7 @@ const MultiSelectInput = ({
{children}
</FieldInput>
<HelpMessage message={helpMessage} isVisible={!message} />
</Field>
</LegacyField>
);
};

Expand Down
13 changes: 9 additions & 4 deletions slash/react/src/Form/Number/NumberInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ import {
ReactNode,
useId,
} from "react";
import { Field, FieldInput, HelpMessage, useInputClassModifier } from "../core";
import {
FieldInput,
HelpMessage,
LegacyField,
useInputClassModifier,
} from "../core";

import { Number } from "./Number";

type Props = ComponentPropsWithoutRef<typeof Field> &
type Props = ComponentPropsWithoutRef<typeof LegacyField> &
ComponentPropsWithRef<typeof Number> & {
helpMessage?: ReactNode;
children?: ReactNode;
Expand Down Expand Up @@ -43,7 +48,7 @@ export const NumberInput = ({
required,
);
return (
<Field
<LegacyField
label={label}
id={inputId}
message={message}
Expand All @@ -68,6 +73,6 @@ export const NumberInput = ({
{children}
</FieldInput>
<HelpMessage message={helpMessage} isVisible={!message} />
</Field>
</LegacyField>
);
};
13 changes: 9 additions & 4 deletions slash/react/src/Form/Pass/PassInput.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { ComponentProps, ReactNode, useId, useState } from "react";
import { Field, FieldInput, HelpMessage, useInputClassModifier } from "../core";
import {
FieldInput,
HelpMessage,
LegacyField,
useInputClassModifier,
} from "../core";
import { Pass } from "./Pass";

const strengthList: Record<number, string> = {
Expand All @@ -25,7 +30,7 @@ const calculateStrength = (score?: string | null) => {
};

type PassProps = ComponentProps<typeof Pass>;
type Props = ComponentProps<typeof Field> &
type Props = ComponentProps<typeof LegacyField> &
Omit<PassProps, "onToggleType" | "type"> & {
helpMessage?: ReactNode;
score?: string;
Expand Down Expand Up @@ -63,7 +68,7 @@ const PassInput = ({
);

return (
<Field
<LegacyField
label={label}
message={message}
messageType={messageType}
Expand Down Expand Up @@ -92,7 +97,7 @@ const PassInput = ({
{children}
<HelpMessage message={helpMessage} isVisible={!message} />
</FieldInput>
</Field>
</LegacyField>
);
};

Expand Down
8 changes: 4 additions & 4 deletions slash/react/src/Form/Radio/RadioInput.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { ComponentPropsWithoutRef, forwardRef } from "react";
import {
Field,
LegacyField,
getFirstId,
useInputClassModifier,
useOptionsWithId,
} from "../core";
import { Radio, RadioModes } from "./Radio";

type RadioInputProps = ComponentPropsWithoutRef<typeof Field> &
type RadioInputProps = ComponentPropsWithoutRef<typeof LegacyField> &
ComponentPropsWithoutRef<typeof Radio>;

const RadioInput = forwardRef<HTMLInputElement, RadioInputProps>(
Expand Down Expand Up @@ -47,7 +47,7 @@ const RadioInput = forwardRef<HTMLInputElement, RadioInputProps>(
);

return (
<Field
<LegacyField
label={label}
id={firstId}
message={message}
Expand All @@ -72,7 +72,7 @@ const RadioInput = forwardRef<HTMLInputElement, RadioInputProps>(
{...radioProps}
/>
{children}
</Field>
</LegacyField>
);
},
);
Expand Down
13 changes: 9 additions & 4 deletions slash/react/src/Form/Select/SelectInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ import {
useId,
} from "react";

import { Field, FieldInput, HelpMessage, useInputClassModifier } from "../core";
import {
FieldInput,
HelpMessage,
LegacyField,
useInputClassModifier,
} from "../core";
import { Select } from "./Select";

type Props = ComponentProps<typeof Field> &
type Props = ComponentProps<typeof LegacyField> &
ComponentProps<typeof Select> & {
helpMessage?: ReactNode;
};
Expand Down Expand Up @@ -45,7 +50,7 @@ const SelectInput = forwardRef<HTMLSelectElement, PropsWithChildren<Props>>(
required,
);
return (
<Field
<LegacyField
label={label}
id={inputId}
message={message}
Expand All @@ -72,7 +77,7 @@ const SelectInput = forwardRef<HTMLSelectElement, PropsWithChildren<Props>>(
{children}
</FieldInput>
<HelpMessage message={helpMessage} isVisible={!message} />
</Field>
</LegacyField>
);
},
);
Expand Down
8 changes: 4 additions & 4 deletions slash/react/src/Form/Slider/SliderInput.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useId, useMemo, type ComponentProps, type ReactNode } from "react";
import { Field, HelpMessage } from "../core";
import { HelpMessage, LegacyField } from "../core";
import { Slider } from "./Slider";

type Props = ComponentProps<typeof Field> &
type Props = ComponentProps<typeof LegacyField> &
ComponentProps<typeof Slider> & {
helpMessage?: ReactNode;
};
Expand All @@ -26,7 +26,7 @@ const SliderInput = ({
const newId = useMemo(() => id ?? generatedId, [generatedId, id]);

return (
<Field
<LegacyField
id={newId}
label={label}
message={message}
Expand All @@ -41,7 +41,7 @@ const SliderInput = ({
<Slider {...sliderProps} id={id} classModifier={classModifier} />
{children}
<HelpMessage message={helpMessage} isVisible={!message} />
</Field>
</LegacyField>
);
};

Expand Down
2 changes: 1 addition & 1 deletion slash/react/src/Form/Text/Text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ const Text = forwardRef<HTMLInputElement, Props>(
},
);

Text.displayName = "Text";
Text.displayName = "EnhancedInput";

export { Text };
4 changes: 3 additions & 1 deletion slash/react/src/Form/Text/TextInput.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Meta, StoryObj } from "@storybook/react";
import { fn } from "@storybook/test";
import { MessageTypes } from "../core";
import { TextInput } from "./TextInput";
import { inputTypes } from "./inputTypes";
import { TextInput } from "./TextInput";

const meta: Meta<typeof TextInput> = {
component: TextInput,
Expand All @@ -29,6 +29,8 @@ export const TextInputStory: Story = {
readOnly: false,
disabled: false,
autoFocus: false,
message: "message",

className: "",
type: "text",
label: "Your name",
Expand Down
Loading

0 comments on commit 461a95c

Please sign in to comment.