-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: unify name form fields for Renku 2.0
- Loading branch information
Showing
11 changed files
with
258 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/*! | ||
* Copyright 2024 - Swiss Data Science Center (SDSC) | ||
* A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and | ||
* Eidgenössische Technische Hochschule Zürich (ETHZ). | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
export type RenkuEntityType = "group" | "user" | "project" | "data-connector"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/*! | ||
* Copyright 2024 - Swiss Data Science Center (SDSC) | ||
* A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and | ||
* Eidgenössische Technische Hochschule Zürich (ETHZ). | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import type { RenkuEntityType } from "./entities.types"; | ||
|
||
export function displayEntityType(t: RenkuEntityType): string { | ||
if (t === "data-connector") { | ||
return "data connector"; | ||
} | ||
return t; | ||
} |
73 changes: 73 additions & 0 deletions
73
client/src/features/entitiesV2/fields/GenericInputText.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/*! | ||
* Copyright 2024 - Swiss Data Science Center (SDSC) | ||
* A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and | ||
* Eidgenössische Technische Hochschule Zürich (ETHZ). | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import cx from "classnames"; | ||
import type { FieldValues } from "react-hook-form"; | ||
import { Controller } from "react-hook-form"; | ||
|
||
import { FormText, Input, Label } from "reactstrap"; | ||
import type { GenericFormFieldProps } from "./forms.types"; | ||
|
||
export default function GenericInputText<T extends FieldValues>({ | ||
className, | ||
control, | ||
dataCy, | ||
defaultErrorMessage, | ||
defaultValue, | ||
disabled, | ||
name, | ||
helpText, | ||
inputDataCy, | ||
inputId, | ||
label, | ||
rules, | ||
shouldUnregister, // eslint-disable-line spellcheck/spell-checker | ||
}: GenericFormFieldProps<T>) { | ||
const inputHelpId = `${inputId}-help`; | ||
|
||
return ( | ||
<div className={className} data-cy={dataCy}> | ||
{label && <Label for={inputId}>{label}</Label>} | ||
<Controller | ||
control={control} | ||
defaultValue={defaultValue} | ||
disabled={disabled} | ||
name={name} | ||
render={({ field: { ref, ...rest }, fieldState: { error } }) => ( | ||
<> | ||
<Input | ||
aria-describedby={helpText ? inputHelpId : undefined} | ||
className={cx(error && "is-invalid")} | ||
data-cy={inputDataCy} | ||
id={inputId} | ||
type="text" | ||
innerRef={ref} | ||
{...rest} | ||
/> | ||
<div className="invalid-feedback"> | ||
{error?.message ?? defaultErrorMessage} | ||
</div> | ||
</> | ||
)} | ||
rules={rules} | ||
shouldUnregister={shouldUnregister} // eslint-disable-line spellcheck/spell-checker | ||
/> | ||
{helpText && <FormText id={inputHelpId}>{helpText}</FormText>} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/*! | ||
* Copyright 2024 - Swiss Data Science Center (SDSC) | ||
* A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and | ||
* Eidgenössische Technische Hochschule Zürich (ETHZ). | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { useMemo } from "react"; | ||
import type { FieldValues } from "react-hook-form"; | ||
|
||
import { displayEntityType } from "../entities.utils"; | ||
import type { EntityFormFieldProps } from "./forms.types"; | ||
import GenericInputText from "./GenericInputText"; | ||
|
||
export default function NameFormField<T extends FieldValues>({ | ||
entityType, | ||
defaultErrorMessage: defaultErrorMessage_, | ||
helpText: helpText_, | ||
inputId: inputId_, | ||
inputIdPrefix, | ||
label: label_, | ||
...props | ||
}: EntityFormFieldProps<T>) { | ||
const inputId = useMemo( | ||
() => | ||
inputId_ || | ||
(inputIdPrefix | ||
? `${inputIdPrefix}-${entityType}-${props.name}` | ||
: `$${entityType}-${props.name}`), | ||
[entityType, inputIdPrefix, inputId_, props.name] | ||
); | ||
const defaultErrorMessage = useMemo( | ||
() => defaultErrorMessage_ ?? "Please provide a valid name.", | ||
[defaultErrorMessage_] | ||
); | ||
const helpText = useMemo( | ||
() => | ||
helpText_ ?? | ||
`The name you will use to refer to the ${displayEntityType(entityType)}.`, | ||
[entityType, helpText_] | ||
); | ||
const label = useMemo(() => label_ ?? "Name", [label_]); | ||
|
||
return ( | ||
<GenericInputText | ||
defaultErrorMessage={defaultErrorMessage} | ||
helpText={helpText} | ||
inputId={inputId} | ||
label={label} | ||
{...props} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/*! | ||
* Copyright 2024 - Swiss Data Science Center (SDSC) | ||
* A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and | ||
* Eidgenössische Technische Hochschule Zürich (ETHZ). | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { ReactNode } from "react"; | ||
import type { FieldValues, UseControllerProps } from "react-hook-form"; | ||
import { RenkuEntityType } from "../entities.types"; | ||
|
||
/** Most generic type for a form field in the Renku 2.0 UI. */ | ||
export interface GenericFormFieldProps<T extends FieldValues> | ||
extends UseControllerProps<T> { | ||
className?: string; | ||
dataCy?: string; | ||
defaultErrorMessage?: ReactNode; | ||
helpText?: ReactNode; | ||
inputDataCy?: string; | ||
inputId: string; | ||
label?: ReactNode; | ||
} | ||
|
||
type GenericFormFieldPropsWithOptionalInputId<T extends FieldValues> = Omit< | ||
GenericFormFieldProps<T>, | ||
"inputId" | ||
> & | ||
Partial<Pick<GenericFormFieldProps<T>, "inputId">>; | ||
|
||
/** Type for an entity's form field in the Renku 2.0 UI. */ | ||
export interface EntityFormFieldProps<T extends FieldValues> | ||
extends GenericFormFieldPropsWithOptionalInputId<T> { | ||
entityType: RenkuEntityType; | ||
inputIdPrefix?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters