Skip to content

Commit 4a35e71

Browse files
committed
add props obj for label element
1 parent 7cf9227 commit 4a35e71

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-10
lines changed

README.md

+14-3
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ Full refactor of react-datalist-input.
112112
- Exposes helper hooks
113113
- Exposes underlying components for full customization
114114

115-
**Note:** Be aware that version 3.0.0 includes breaking changes. Version 3.0.0 deprecates some properties from the DatalistInput component but all but one use case still be implemented in the new version (in a more intuitive way using the `useComboboxControls` hook). The only property that has been fully depcreated is `requiredInputLength`. Please create an [Issue](https://github.com/andrelandgraf/react-datalist-input/issues) if you need it!
115+
**Note:** Be aware that version 3.0.0 includes breaking changes. Version 3.0.0 deprecates some properties from the DatalistInput component such as `requiredInputLength`. Instead of using custom properties for different use cases, you have now full control using the `useComboboxControls` hook and the `filters` property. Use plain React state and callbacks to control every aspect of the component's behavior!
116116

117117
#### Version 2.2.0
118118

@@ -180,7 +180,7 @@ const YourComponent = ({ options }) => {
180180
// optional: label, node
181181
// label: option.name, // use a custom label instead of the value
182182
// node: option.name, // use a custom ReactNode to display the option
183-
...option, // pass along any other properties, will be available in your onSelect callback
183+
...option, // pass along any other properties to access in your onSelect callback
184184
})),
185185
[yourItems],
186186
);
@@ -199,17 +199,27 @@ However, you can also customize the styling by providing your own CSS! Instead o
199199

200200
- `react-datalist-input__container`: For the container element.
201201
- `react-datalist-input__textbox`: For the input element.
202+
- `react-datalist-input__label`: For the label element.
202203
- `react-datalist-input__listbox`: For the dropdown list.
203204
- `react-datalist-input__listbox-option`: For each option in the dropdown list.
204205

205-
**Note:** To get up and running quickly, just copy-paste the default stylesheet and adapt the pieces you need.
206+
**Note:** Use the focus and hover states of `react-datalist-input__listbox-option` to show the user some visual feedback.
207+
208+
```css
209+
.react-datalist-input__listbox-option:focus {
210+
background-color: gray;
211+
}
212+
```
213+
214+
**Tip:** To get up and running quickly, just copy-paste the default stylesheet and adapt the pieces you need.
206215

207216
#### Tailwind CSS / Utility Classes
208217

209218
Alternatively, you can also pass custom classes to each element of the DatalistInput component by using the following props:
210219

211220
- `className`: For the container element.
212221
- `inputProps["className"]`: For the input element.
222+
- `labelProps["className"]`: For the label element.
213223
- `listboxProps["className"]`: For the dropdown list.
214224
- `listboxOptionProps["className"]`: For each option in the dropdown list.
215225
- `isExpandedClassName`: Applied to the dropdown list if it is expanded.
@@ -348,6 +358,7 @@ DatalistInput accepts the following properties:
348358
- `selectedItem`: The currently selected item. Important for ARIA. DatalistInput keeps track of the last selected item. You only need to provide this prop if you want to change the selected item outside of the component.
349359
- `inputProps`: An object of props to pass to the combobox input element.
350360
- `listboxProps`: An object of props to pass to the listbox element.
361+
- `labelProps`: An object of props to pass to the label element.
351362
- `listboxOptionProps`: An object of props to pass to the listbox option elements.
352363
- `isExpandedClassName`: The class name applied to the listbox element if it is expanded.
353364
- `isCollapsedClassName`: The class name applied to the listbox element if it is collapsed.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-datalist-input",
3-
"version": "3.0.5",
3+
"version": "3.0.6",
44
"description": "react-datalist-input provides a React datalist/combobox component called DatalistInput. The component contains an input field with a dropdown menu of suggestions based on the current input.",
55
"main": "dist/index.js",
66
"module": "dist/index.es.js",

src/index.tsx

+15-3
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ const useFilters = (
575575
return [filtered, filteredRef];
576576
};
577577

578-
type LabelProps =
578+
type LabelOptionProps =
579579
| {
580580
showLabel?: false;
581581
label: string;
@@ -585,7 +585,9 @@ type LabelProps =
585585
label: ReactNode;
586586
};
587587

588-
type DatalistInputProps = LabelProps &
588+
type LabelProps = HTMLAttributes<HTMLLabelElement>;
589+
590+
type DatalistInputProps = LabelOptionProps &
589591
Omit<HTMLAttributes<HTMLDivElement>, 'onSelect'> & {
590592
items: Array<Item>;
591593
selectedItem?: Item;
@@ -597,6 +599,7 @@ type DatalistInputProps = LabelProps &
597599
placeholder?: ComboboxInputProps['placeholder'];
598600
filters?: Array<Filter>;
599601
inputProps?: ComboboxInputProps;
602+
labelProps?: LabelProps;
600603
listboxProps?: ListboxProps;
601604
listboxOptionProps?: ListboxOptionProps;
602605
isExpandedClassName?: string;
@@ -627,6 +630,7 @@ const DatalistInput = forwardRef<HTMLDivElement, PropsWithRef<DatalistInputProps
627630
setIsExpanded,
628631
filters = [startsWithValueFilter],
629632
inputProps,
633+
labelProps,
630634
listboxOptionProps,
631635
listboxProps,
632636
isExpandedClassName = '',
@@ -700,7 +704,15 @@ const DatalistInput = forwardRef<HTMLDivElement, PropsWithRef<DatalistInputProps
700704
selectedItemId={internalSelectedItem?.id}
701705
isExpanded={internalIsExpanded}
702706
>
703-
{showLabel && <label htmlFor={inputProps?.id || internalTextboxId}>{label}</label>}
707+
{showLabel && (
708+
<label
709+
{...labelProps}
710+
className={`react-datalist-input__label ${labelProps?.className}`}
711+
htmlFor={inputProps?.id || internalTextboxId}
712+
>
713+
{label}
714+
</label>
715+
)}
704716
<ComboboxInput
705717
{...inputProps}
706718
ref={comboboxInputRef}

tests/remix-demo/app/combobox.tsx

+15-3
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ const useFilters = (
575575
return [filtered, filteredRef];
576576
};
577577

578-
type LabelProps =
578+
type LabelOptionProps =
579579
| {
580580
showLabel?: false;
581581
label: string;
@@ -585,7 +585,9 @@ type LabelProps =
585585
label: ReactNode;
586586
};
587587

588-
type DatalistInputProps = LabelProps &
588+
type LabelProps = HTMLAttributes<HTMLLabelElement>;
589+
590+
type DatalistInputProps = LabelOptionProps &
589591
Omit<HTMLAttributes<HTMLDivElement>, 'onSelect'> & {
590592
items: Array<Item>;
591593
selectedItem?: Item;
@@ -597,6 +599,7 @@ type DatalistInputProps = LabelProps &
597599
placeholder?: ComboboxInputProps['placeholder'];
598600
filters?: Array<Filter>;
599601
inputProps?: ComboboxInputProps;
602+
labelProps?: LabelProps;
600603
listboxProps?: ListboxProps;
601604
listboxOptionProps?: ListboxOptionProps;
602605
isExpandedClassName?: string;
@@ -627,6 +630,7 @@ const DatalistInput = forwardRef<HTMLDivElement, PropsWithRef<DatalistInputProps
627630
setIsExpanded,
628631
filters = [startsWithValueFilter],
629632
inputProps,
633+
labelProps,
630634
listboxOptionProps,
631635
listboxProps,
632636
isExpandedClassName = '',
@@ -700,7 +704,15 @@ const DatalistInput = forwardRef<HTMLDivElement, PropsWithRef<DatalistInputProps
700704
selectedItemId={internalSelectedItem?.id}
701705
isExpanded={internalIsExpanded}
702706
>
703-
{showLabel && <label htmlFor={inputProps?.id || internalTextboxId}>{label}</label>}
707+
{showLabel && (
708+
<label
709+
{...labelProps}
710+
className={`react-datalist-input__label ${labelProps?.className}`}
711+
htmlFor={inputProps?.id || internalTextboxId}
712+
>
713+
{label}
714+
</label>
715+
)}
704716
<ComboboxInput
705717
{...inputProps}
706718
ref={comboboxInputRef}

0 commit comments

Comments
 (0)