Compare commits

...
2 Commits
Author SHA1 Message Date
Halvor Haugan d1375a6c7b new prop fieldMaxWidth 2026-11-27 16:54:36 +01:00
Halvor Haugan a37c8f13f5 fix(Combobox2): misc. fixes and adjustments 2026-11-27 16:52:40 +01:00
4 changed files with 28 additions and 7 deletions

No files matched your search

+1 -1
View File
@@ -25,7 +25,7 @@
margin-bottom: var(--ax-space-2);
}
/* stylelint-disable-next-line plugin/no-unsupported-browser-features -- :nth-child(of S) in WIP preview component */
/* stylelint-disable-next-line plugin/no-unsupported-browser-features -- Progressive enhancement */
.aksel-listbox__group:first-child &:nth-child(1 of &) {
/* When we scroll to the first option, and that option is in a group,
scroll to the very top so that the group label is visible as well.
@@ -41,6 +41,12 @@ interface ComboboxProps<
* Field name. When provided, the field value will be included in form submissions.
*/
name?: string;
/**
* Maximum width of the field that shows current value.
*
* **NB: Use a unit that scales with the font size, such as rem or em.**
*/
fieldMaxWidth?: string;
}
const Combobox = forwardRef(
@@ -49,6 +55,7 @@ const Combobox = forwardRef(
label,
hideLabel,
name,
fieldMaxWidth,
size: sizeProp,
error,
errorId: errorIdProp,
@@ -110,7 +117,13 @@ const Combobox = forwardRef(
</BodyShort>
)}
<ComboboxTrigger readOnly={readOnly} {...inputProps}>
<ComboboxField ref={ref} hasError={hasError} />
<ComboboxField
ref={ref}
hasError={hasError}
containerStyle={
fieldMaxWidth ? { maxWidth: fieldMaxWidth } : undefined
}
/>
</ComboboxTrigger>
<div
className="aksel-form-field__error aksel-combo-box__error"
@@ -127,7 +140,13 @@ const Combobox = forwardRef(
{name &&
rest.value.map((val) => (
<input key={val} type="hidden" name={name} value={val} />
<input
key={val}
type="hidden"
name={name}
value={val}
disabled={inputProps.disabled}
/>
))}
<ComboboxOverlay
@@ -179,7 +198,7 @@ interface ComboboxSingleProps<
*
* **WARNING: This component is in active development and may receive breaking changes outside major releases!**
*
* @see [📝 Documentation](https://aksel.nav.no/komponenter/core/TODO)
* @see [📝 Documentation](https://aksel.nav.no/komponenter/core/combobox-single)
* @see 🏷️ {@link ComboboxSingleProps}
*
* @example
@@ -188,6 +207,7 @@ interface ComboboxSingleProps<
* label="Select country"
* name="country"
* options={countries}
* fieldMaxWidth="12rem"
* />
* ```
*/
@@ -10,10 +10,11 @@ import {
interface ComboboxFieldProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
children?: string;
hasError?: boolean;
containerStyle?: React.CSSProperties;
}
const ComboboxField = forwardRef<HTMLButtonElement, ComboboxFieldProps>(
({ children, hasError, ...rest }, ref) => {
({ children, hasError, containerStyle, ...rest }, ref) => {
const { options, value, size } = useComboboxRootContext();
const translate = useI18n("Combobox2");
@@ -35,7 +36,7 @@ const ComboboxField = forwardRef<HTMLButtonElement, ComboboxFieldProps>(
}, [label]);
return (
<div className="aksel-combo-box__field-container">
<div className="aksel-combo-box__field-container" style={containerStyle}>
<button
type="button"
ref={ref}
@@ -60,7 +60,7 @@ export const ComboboxPopup = ({ children }: ComboboxPopupProps) => {
}
const inputElm = ref.current?.querySelector<HTMLInputElement>("input");
if (inputElm) {
inputElm.focus();
inputElm.focus({ preventScroll: true });
return;
}
const listElm =