Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

feat: add keyboard shortcuts to input field

Brad Barbinb4a535ec26be
diff --git a/apps/erp/app/components/Form/AddressAutocomplete.tsx b/apps/erp/app/components/Form/AddressAutocomplete.tsx
index 3e520ea064..19b8ce693e 100644
--- a/apps/erp/app/components/Form/AddressAutocomplete.tsx
+++ b/apps/erp/app/components/Form/AddressAutocomplete.tsx
@@ -1,10 +1,14 @@
-import { useControlField, useFormContext } from "@carbon/form";
+import { useControlField, useField, useFormContext } from "@carbon/form";
import {
Command,
CommandEmpty,
CommandGroup,
+ CommandInputTextField,
CommandItem,
CommandList,
+ FormControl,
+ FormErrorMessage,
+ FormLabel,
useDebounce,
VStack,
} from "@carbon/react";
@@ -52,6 +56,7 @@ const AddressAutocomplete = ({
const [value, setValue] = useControlField<string>(fieldName);
const { clearError } = useFormContext();
+ const { error } = useField(fieldName);
const [open, setOpen] = useState(false);
const [justSelected, setJustSelected] = useState(false);
const [userInteracted, setUserInteracted] = useState(false);
@@ -160,11 +165,11 @@ const AddressAutocomplete = ({
}
}, [value, justSelected]);
- const handleChange = useCallback(
- (e: React.ChangeEvent<HTMLInputElement>) => {
+ const handleValueChange = useCallback(
+ (newValue: string) => {
setUserInteracted(true);
setJustSelected(false);
- setValue(e.target.value);
+ setValue(newValue);
},
[setValue]
);
@@ -180,45 +185,46 @@ const AddressAutocomplete = ({
const fields: AddressFields = {
autocomplete: (
- <div className="relative w-full" ref={containerRef}>
- <Input
- name={fieldName}
- label={label}
- value={value || ""}
- onChange={handleChange}
- onFocus={handleInputFocus}
- onKeyDown={handleKeyDown}
- autoComplete="off"
- />
- {open && suggestions.length > 0 && (
- <div className="absolute w-full mt-1 z-[9999]">
- <div className="rounded-md border bg-popover text-popover-foreground shadow-md p-0">
- <Command shouldFilter={false}>
- <CommandList>
- <CommandEmpty>
- {loading ? "Loading..." : "No addresses found"}
- </CommandEmpty>
- <CommandGroup>
- {suggestions.map((suggestion) => (
- <CommandItem
- key={suggestion.placeId}
- value={suggestion.placeId}
- className="cursor-pointer"
- onMouseDown={(e) => {
- e.preventDefault();
- handleSelect(suggestion.placeId);
- }}
- >
- {suggestion.text}
- </CommandItem>
- ))}
- </CommandGroup>
- </CommandList>
- </Command>
- </div>
- </div>
- )}
- </div>
+ <FormControl isInvalid={!!error}>
+ <FormLabel htmlFor={fieldName}>{label}</FormLabel>
+ <div className="relative w-full" ref={containerRef}>
+ <Command shouldFilter={false}>
+ <CommandInputTextField
+ id={fieldName}
+ name={fieldName}
+ value={value || ""}
+ onValueChange={handleValueChange}
+ onFocus={handleInputFocus}
+ onKeyDown={handleKeyDown}
+ autoComplete="off"
+ />
+ {open && suggestions.length > 0 && (
+ <CommandList className="absolute w-full top-10 z-[9999] rounded-md border bg-popover text-popover-foreground shadow-md p-0">
+ <CommandEmpty>
+ {loading ? "Loading..." : "No addresses found"}
+ </CommandEmpty>
+ <CommandGroup>
+ {suggestions.map((suggestion) => (
+ <CommandItem
+ key={suggestion.placeId}
+ value={suggestion.placeId}
+ className="cursor-pointer"
+ onSelect={() => handleSelect(suggestion.placeId)}
+ onMouseDown={(e) => {
+ e.preventDefault();
+ handleSelect(suggestion.placeId);
+ }}
+ >
+ {suggestion.text}
+ </CommandItem>
+ ))}
+ </CommandGroup>
+ </CommandList>
+ )}
+ </Command>
+ </div>
+ {error && <FormErrorMessage>{error}</FormErrorMessage>}
+ </FormControl>
),
addressLine2: (
<Input
diff --git a/packages/react/src/Command.tsx b/packages/react/src/Command.tsx
index b75d5e0edd..01cab28383 100644
--- a/packages/react/src/Command.tsx
+++ b/packages/react/src/Command.tsx
@@ -85,6 +85,87 @@ const CommandInput = forwardRef<
CommandInput.displayName = CommandPrimitive.Input.displayName;
+const commandInputTextFieldVariants = cva(
+ "flex w-full px-3 py-1 bg-transparent text-foreground transition-colors placeholder:text-muted-foreground disabled:opacity-50 rounded-md border border-input ring-offset-background focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed shadow-sm",
+ {
+ variants: {
+ size: {
+ lg: "h-12 rounded-lg px-4 text-base",
+ md: "h-10 rounded-md px-4 text-sm",
+ sm: "h-8 rounded-md px-3 text-sm",
+ xs: "h-6 rounded px-2 text-sm",
+ },
+ isInvalid: {
+ true: "border-destructive ring-destructive focus-visible:ring-destructive",
+ false: "",
+ },
+ isReadOnly: {
+ true: "bg-muted text-muted-foreground",
+ false: "",
+ },
+ isDisabled: {
+ true: "bg-muted text-muted-foreground",
+ false: "",
+ },
+ borderless: {
+ true: "border-none px-0 outline-none ring-transparent focus:ring-transparent focus:ring-offset-0 focus-visible:ring-transparent focus:outline-none focus-visible:outline-none focus-visible:ring-0 focus-visible:ring-offset-0 shadow-none",
+ false: "",
+ },
+ },
+ defaultVariants: {
+ size: "md",
+ isInvalid: false,
+ borderless: false,
+ },
+ }
+);
+
+interface CommandInputTextFieldProps
+ extends Omit<ComponentPropsWithoutRef<typeof CommandPrimitive.Input>, "size">,
+ VariantProps<typeof commandInputTextFieldVariants> {
+ isInvalid?: boolean;
+ isDisabled?: boolean;
+ isReadOnly?: boolean;
+ borderless?: boolean;
+}
+
+const CommandInputTextField = forwardRef<
+ ElementRef<typeof CommandPrimitive.Input>,
+ CommandInputTextFieldProps
+>(
+ (
+ {
+ className,
+ size,
+ isInvalid = false,
+ isDisabled = false,
+ isReadOnly = false,
+ borderless = false,
+ ...props
+ },
+ ref
+ ) => (
+ <CommandPrimitive.Input
+ ref={ref}
+ className={cn(
+ commandInputTextFieldVariants({
+ size,
+ isInvalid,
+ isDisabled,
+ isReadOnly,
+ borderless,
+ }),
+ className
+ )}
+ disabled={isDisabled}
+ readOnly={isReadOnly}
+ {...props}
+ />
+ )
+);
+
+CommandInputTextField.displayName = "CommandInputTextField";
+
const CommandList = forwardRef<
ElementRef<typeof CommandPrimitive.List>,
ComponentPropsWithoutRef<typeof CommandPrimitive.List>
@@ -294,6 +375,7 @@ export {
CommandEmpty,
CommandGroup,
CommandInput,
+ CommandInputTextField,
CommandItem,
CommandList,
CommandLoading,
diff --git a/packages/react/src/index.tsx b/packages/react/src/index.tsx
index 3b1bb364b1..454af846a4 100644
--- a/packages/react/src/index.tsx
+++ b/packages/react/src/index.tsx
@@ -50,6 +50,7 @@ import {
CommandEmpty,
CommandGroup,
CommandInput,
+ CommandInputTextField,
CommandItem,
CommandList,
CommandLoading,
@@ -348,6 +349,7 @@ export {
CommandEmpty,
CommandGroup,
CommandInput,
+ CommandInputTextField,
CommandItem,
CommandList,
CommandLoading,