feat: normalize routing form identifier field with URL-safe format (#25128)
## What does this PR do? - Fixes #25116 - Fixes [CAL-6741](https://linear.app/calcom/issue/CAL-6741/make-routing-edit-question-form-less-intrusive-on-how-it-handles-the) https://github.com/user-attachments/assets/d78c4c62-2af5-44d8-b25c-e938cc8ddf6f Improves the routing form field identifier auto-fill behavior to generate URL-safe identifiers from labels. Previously, the identifier field simply duplicated the label text. Now it normalizes the input to be URL-friendly. **Changes:** - Converts labels to lowercase with hyphens (e.g., "What is your name?" → "what-is-your-name") - Limits identifiers to the first 5 words (e.g., "What do you work on and how can we help?" → "what-do-you-work-on") - Updates the Identifier label to clarify it's a URL parameter with an example - Preserves manual identifier edits when the label changes **Link to Devin run:** https://app.devin.ai/sessions/f569297990fe4436bab1fec89d8b71ac **Requested by:** @PeerRich ([email protected]) ## Key Implementation Details The `normalizeIdentifier` function: - Strips special characters and replaces them with spaces - Converts to lowercase - Splits on whitespace and takes first 5 words - Joins with hyphens The auto-fill logic only updates the identifier if: 1. The identifier field is empty, OR 2. The identifier matches the normalized version of the previous label This preserves manual edits while still providing helpful auto-fill for new fields. ## Mandatory Tasks (DO NOT REMOVE) - [x] I have self-reviewed the code (A decent size PR without self-review might be rejected). - [x] I have updated the developer docs in /docs if this PR makes changes that would require a [documentation change](https://cal.com/docs). If N/A, write N/A here and check the checkbox. **N/A** - This is a UI behavior change that doesn't require documentation updates. - [ ] I confirm automated tests are in place that prove my fix is effective or that my feature works. **Note:** No automated tests were added for this change. Manual testing is recommended. ## How should this be tested? **Test Cases:** 1. **Basic normalization:** - Create a new routing form field - Enter label: "What is your name?" - Verify identifier auto-fills to: "what-is-your-name" 2. **5-word limit:** - Enter label: "What do you work on and how can we help?" - Verify identifier becomes: "what-do-you-work-on" 3. **Special characters:** - Enter label: "Email (required)!" - Verify identifier becomes: "email-required" 4. **Manual edit preservation:** - Enter label: "Test Field" - Manually change identifier to: "custom-id" - Change label to: "New Test Field" - Verify identifier stays: "custom-id" (not auto-updated) 5. **Updated label text:** - Verify the Identifier field label shows: "Identifier (URL Parameter, i.e.: &what-is-your-name=john-doe)" **Environment:** - No special environment variables needed - Test in the routing forms builder at `/apps/routing-forms/[formId]` ## Important Review Points ⚠️ **No automated tests**: The normalization function and auto-fill behavior lack test coverage. Consider adding unit tests for edge cases. 🔍 **Auto-fill logic**: The identifier only auto-updates if it's empty or matches the previous normalized label. This preserves manual edits but may be surprising to users in some scenarios. 🌍 **Unicode handling**: Non-ASCII characters are replaced with spaces. This may not be ideal for international users with non-English labels. ## Checklist - [x] I have read the [contributing guide](https://github.com/calcom/cal.com/blob/main/CONTRIBUTING.md) - [x] My code follows the style guidelines of this project - [x] I have commented my code, particularly in hard-to-understand areas - [x] I have checked if my changes generate no new warnings
This commit is contained in:
1 parent
02dab51543
commit
3490272815
5 files changed
+49
-21
No files matched your search
@@ -8,6 +8,7 @@ import { v4 as uuidv4 } from "uuid";
|
||||
|
||||
import { FieldTypes } from "@calcom/app-store/routing-forms/lib/FieldTypes";
|
||||
import type { RoutingFormWithResponseCount } from "@calcom/app-store/routing-forms/types/types";
|
||||
import { getFieldIdentifier } from "@calcom/features/form-builder/utils/getFieldIdentifier";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import classNames from "@calcom/ui/classNames";
|
||||
import { Button } from "@calcom/ui/components/button";
|
||||
@@ -102,14 +103,25 @@ function Field({
|
||||
required
|
||||
{...hookForm.register(`${hookFieldNamespace}.label`)}
|
||||
onChange={(e) => {
|
||||
hookForm.setValue(`${hookFieldNamespace}.label`, e.target.value, { shouldDirty: true });
|
||||
const newLabel = e.target.value;
|
||||
// Use label from useWatch which is guaranteed to be the previous value
|
||||
// since useWatch updates reactively (after re-render), not synchronously
|
||||
const previousLabel = label || "";
|
||||
hookForm.setValue(`${hookFieldNamespace}.label`, newLabel, { shouldDirty: true });
|
||||
const currentIdentifier = hookForm.getValues(`${hookFieldNamespace}.identifier`);
|
||||
// Only auto-update identifier if it was auto-generated from the previous label
|
||||
// This preserves manual identifier changes
|
||||
const isIdentifierGeneratedFromPreviousLabel = currentIdentifier === getFieldIdentifier(previousLabel);
|
||||
if (!currentIdentifier || isIdentifierGeneratedFromPreviousLabel) {
|
||||
hookForm.setValue(`${hookFieldNamespace}.identifier`, getFieldIdentifier(newLabel), { shouldDirty: true });
|
||||
}
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
<div className="mb-3 w-full">
|
||||
<TextField
|
||||
disabled={!!router}
|
||||
label="Identifier"
|
||||
label={t("identifier_url_parameter")}
|
||||
name={`${hookFieldNamespace}.identifier`}
|
||||
required
|
||||
placeholder={t("identifies_name_field")}
|
||||
|
||||
@@ -2286,6 +2286,7 @@
|
||||
"responses_collection_waiting_description": "Wait for some time for responses to be collected. You can go and submit the form yourself as well.",
|
||||
"this_is_what_your_users_would_see": "This is what your users would see",
|
||||
"identifies_name_field": "Identifies field by this name.",
|
||||
"identifier_url_parameter": "Identifier (URL Parameter, i.e.: &what-is-your-name=john-doe)",
|
||||
"add_1_option_per_line": "Add 1 option per line",
|
||||
"select_a_router": "Select a router",
|
||||
"add_a_new_route": "Add a new Route",
|
||||
|
||||
Reference in new issue
Block a user