Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: UX issues in template settings form's default auto-stop field #5330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Show user friendly field name in error text
  • Loading branch information
presleyp committed Dec 6, 2022
commit 10bc4af563528fad1b87f7e44f0e9cb487172400
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ export const TemplateSettingsForm: FC<TemplateSettingsForm> = ({
</div>

<TextField
{...getFieldHelpers("default_ttl_ms", <TTLHelperText ttl={form.values.default_ttl_ms} />)}
{...getFieldHelpers("default_ttl_ms", <TTLHelperText ttl={form.values.default_ttl_ms} />, "Time until auto-stop")}
disabled={isSubmitting}
fullWidth
inputProps={{ min: 0, step: 1 }}
Expand Down
11 changes: 5 additions & 6 deletions site/src/util/formUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@ interface FormHelpers {
helperText?: ReactNode
}

// backendErrorName can be used if the backend names a field differently than the frontend does
export const getFormHelpers =
<T>(form: FormikContextType<T>, error?: Error | unknown) =>
(
name: keyof T,
HelperText: ReactNode = "",
backendErrorName?: string,
friendlyLabel?: string,
): FormHelpers => {
const apiValidationErrors =
isApiError(error) && hasApiFieldErrors(error)
Expand All @@ -54,19 +53,19 @@ export const getFormHelpers =
`name must be type of string, instead received '${typeof name}'`,
)
}
const apiErrorName = backendErrorName ?? name

// getIn is a util function from Formik that gets at any depth of nesting
// and is necessary for the types to work
const touched = getIn(form.touched, name)
const apiError = getIn(apiValidationErrors, apiErrorName)
const apiError = getIn(apiValidationErrors, name)
const frontendError = getIn(form.errors, name)
const returnError = apiError ?? frontendError
const friendlyError = friendlyLabel && returnError ? returnError.replace(name, friendlyLabel) : returnError
return {
...form.getFieldProps(name),
id: name,
error: touched && Boolean(returnError),
helperText: touched ? returnError || HelperText : HelperText,
error: touched && Boolean(friendlyError),
helperText: touched ? friendlyError || HelperText : HelperText,
}
}

Expand Down