generated from nl-design-system/example
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FormFieldTextbox with Lux components
- Loading branch information
Jaap-Hein Wester
committed
Nov 6, 2024
1 parent
032f945
commit 0742118
Showing
4 changed files
with
142 additions
and
30 deletions.
There are no files selected for viewing
123 changes: 117 additions & 6 deletions
123
packages/components-react/src/form-field-textbox/FormFieldTextbox.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,120 @@ | ||
import { TextboxTypes } from '@utrecht/component-library-react/dist/Textbox'; | ||
import { FormFieldTextboxProps as UtrechtFormFieldTextboxProps } from '@utrecht/component-library-react/dist/css-module'; | ||
import clsx from 'clsx'; | ||
import { useId } from 'react'; | ||
import { LuxFormField } from '../form-field/FormField'; | ||
import { | ||
FormFieldTextbox as UtrechtFormFieldTextbox, | ||
FormFieldTextboxProps as UtrechtFormFieldTextboxProps, | ||
} from '@utrecht/component-library-react/dist/css-module'; | ||
LuxFormFieldDescription, | ||
type LuxFormFieldDescriptionAppearance, | ||
} from '../form-field-description/FormFieldDescription'; | ||
import { LuxFormFieldErrorMessage } from '../form-field-error-message/FormFieldErrorMessage'; | ||
import { LuxFormFieldLabel } from '../form-field-label/FormFieldLabel'; | ||
import { type Direction, LuxTextbox } from '../textbox/Textbox'; | ||
|
||
UtrechtFormFieldTextbox.displayName = 'LuxFormFieldTextbox'; | ||
export type LuxFormFieldTextboxProps = UtrechtFormFieldTextboxProps & { | ||
appearance?: LuxFormFieldDescriptionAppearance; | ||
distanced?: boolean; | ||
inputDir?: Direction; | ||
}; | ||
|
||
export const LuxFormFieldTextbox = UtrechtFormFieldTextbox; | ||
export type LuxFormFieldTextboxProps = UtrechtFormFieldTextboxProps; | ||
export const LuxFormFieldTextbox = ({ | ||
label, | ||
type, | ||
description, | ||
errorMessage, | ||
disabled, | ||
invalid, | ||
appearance, | ||
distanced, | ||
className, | ||
inputRef, | ||
inputDir, | ||
...restProps | ||
}: LuxFormFieldTextboxProps) => { | ||
const inputId = useId(); | ||
const descriptionId = useId(); | ||
const errorMessageId = useId(); | ||
|
||
const labelNode = | ||
typeof label === 'string' ? ( | ||
<LuxFormFieldLabel disabled={disabled} htmlFor={inputId}> | ||
{label} | ||
</LuxFormFieldLabel> | ||
) : ( | ||
label | ||
); | ||
const descriptionNode = | ||
typeof description === 'string' ? ( | ||
<LuxFormFieldDescription appearance={invalid ? 'invalid' : appearance} id={descriptionId}> | ||
{description} | ||
</LuxFormFieldDescription> | ||
) : ( | ||
description | ||
); | ||
const errorMessageNode = | ||
typeof errorMessage === 'string' ? ( | ||
<LuxFormFieldErrorMessage distanced={distanced} id={errorMessageId}> | ||
{errorMessage} | ||
</LuxFormFieldErrorMessage> | ||
) : ( | ||
errorMessage | ||
); | ||
|
||
// TODO: naar utils | ||
function pick<T extends object, U extends keyof T>(obj: T, paths: Array<U>): Pick<T, U> { | ||
const ret = Object.create(null); | ||
for (const k of paths) { | ||
ret[k] = obj[k]; | ||
} | ||
return ret; | ||
} | ||
|
||
const textBoxAttrs = pick(restProps, [ | ||
'autoComplete', | ||
'min', | ||
'max', | ||
'minLength', | ||
'maxLength', | ||
'pattern', | ||
'placeholder', | ||
'readOnly', | ||
'required', | ||
'inputRequired', | ||
'value', | ||
'defaultValue', | ||
'list', | ||
'size', | ||
'step', | ||
'onFocus', | ||
'onBlur', | ||
'onInput', | ||
'onChange', | ||
]); | ||
|
||
return ( | ||
<LuxFormField | ||
label={labelNode} | ||
description={descriptionNode} | ||
errorMessage={errorMessageNode} | ||
input={ | ||
<LuxTextbox | ||
ref={inputRef} | ||
id={inputId} | ||
type={(type as TextboxTypes) || 'text'} | ||
invalid={invalid} | ||
disabled={disabled} | ||
dir={inputDir || 'auto'} | ||
aria-describedby={ | ||
clsx({ | ||
[descriptionId]: description, | ||
[errorMessageId]: invalid, | ||
}) || undefined | ||
} | ||
{...textBoxAttrs} | ||
/> | ||
} | ||
className={className} | ||
{...restProps} | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters