Skip to content

Commit ad7fa7c

Browse files
authored
Merge pull request #158 from lambda-curry/codegen-bot/select-creatable-default-trim-3b7a5c
Select: default creatable behavior trims input when onCreateOption is omitted
2 parents 5588387 + 4ab2d66 commit ad7fa7c

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

apps/docs/src/remix-hook-form/select.stories.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,11 +530,10 @@ const CreatableSelectExample = () => {
530530
<Select
531531
name="region"
532532
label="Custom Region"
533-
description="Creatable option enabled"
533+
description="Creatable option enabled (defaults to trimming the input and using it as label/value)"
534534
options={[...US_STATES.slice(0, 5), ...CANADA_PROVINCES.slice(0, 5)]}
535535
placeholder="Select a custom region"
536536
creatable
537-
onCreateOption={async (input) => ({ label: input, value: input })}
538537
/>
539538
<Button type="submit">Submit</Button>
540539
{fetcher.data?.selectedRegion && (

packages/components/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lambdacurry/forms",
3-
"version": "0.22.3",
3+
"version": "0.22.4",
44
"type": "module",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",

packages/components/src/ui/select.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,12 @@ export function Select<T extends React.Key = string>({
251251
value={q}
252252
role="option"
253253
onSelect={async () => {
254-
if (!onCreateOption) return;
255-
const created = await onCreateOption(q);
254+
const defaultCreate = (input: string): SelectOption<T> => {
255+
const normalized = input.trim();
256+
// Keep value as a string; schema can coerce on submit if needed
257+
return { label: normalized, value: normalized as unknown as T };
258+
};
259+
const created = await Promise.resolve((onCreateOption ?? defaultCreate)(q));
256260
onValueChange?.(created.value);
257261
popoverState.close();
258262
}}

0 commit comments

Comments
 (0)