Skip to content

Commit 8b9ce3a

Browse files
committed
gets rid of experimental code
1 parent b448ded commit 8b9ce3a

File tree

5 files changed

+55
-37
lines changed

5 files changed

+55
-37
lines changed

src/index.tsx

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,15 @@ const useComboboxContext = () => useContext(ComboboxContext);
112112
/**
113113
* Use this hook to control the open state and input value of the combobox.
114114
* Pass the properties down to the DataListInput component.
115-
* The isExpandedRef property can be ignored. It is for advanced use cases only.
116115
*/
117116
const useComboboxControls = ({ initialValue = '', ...params }: { isExpanded: boolean; initialValue?: string }) => {
118-
const [isExpanded, setIsExpanded, isExpandedRef] = useStateRef(params.isExpanded);
117+
const [isExpanded, setIsExpanded] = useStateRef(params.isExpanded);
119118
const [value, setValue] = useState(initialValue);
120119
return {
121120
isExpanded,
122121
value,
123122
setIsExpanded,
124123
setValue,
125-
isExpandedRef,
126124
};
127125
};
128126

@@ -577,19 +575,6 @@ const useFilters = (
577575
return [filtered, filteredRef];
578576
};
579577

580-
const getTextboxTitle = (isExpanded: boolean, length: number, title?: string) => {
581-
const formattedTitle = title ? (title[title.length - 1] === '.' ? title : title + '.') : undefined;
582-
const ariaContent = `Listbox is ${isExpanded ? 'expanded' : 'closed'}. ${length} ${
583-
length === 1 ? 'option matches' : 'options match'
584-
} your input. Use ArrowDown to navigate between options.`;
585-
return formattedTitle ? `${formattedTitle} ${ariaContent}` : ariaContent;
586-
};
587-
588-
// type LabelProps<T> = {
589-
// showLabel: T;
590-
// label?: T extends true ? ReactNode : string;
591-
// };
592-
593578
type LabelProps =
594579
| {
595580
showLabel?: false;
@@ -727,7 +712,6 @@ const DatalistInput = forwardRef<HTMLDivElement, PropsWithRef<DatalistInputProps
727712
onChange={handleWith(handleChange, inputProps?.onChange)}
728713
onKeyDown={handleWith(handleKeyDownOnInput, inputProps?.onKeyDown)}
729714
aria-label={!showLabel && typeof label === 'string' ? label : undefined}
730-
title={getTextboxTitle(internalIsExpanded, filteredItems.length, inputProps?.title)}
731715
className={`react-datalist-input__textbox ${inputProps?.className}`}
732716
/>
733717
{((filteredItems.length && internalIsExpanded) || isCollapsedClassName || isCollapsedStyle) && (

tests/remix-demo/app/combobox.tsx

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,15 @@ const useComboboxContext = () => useContext(ComboboxContext);
112112
/**
113113
* Use this hook to control the open state and input value of the combobox.
114114
* Pass the properties down to the DataListInput component.
115-
* The isExpandedRef property can be ignored. It is for advanced use cases only.
116115
*/
117116
const useComboboxControls = ({ initialValue = '', ...params }: { isExpanded: boolean; initialValue?: string }) => {
118-
const [isExpanded, setIsExpanded, isExpandedRef] = useStateRef(params.isExpanded);
117+
const [isExpanded, setIsExpanded] = useStateRef(params.isExpanded);
119118
const [value, setValue] = useState(initialValue);
120119
return {
121120
isExpanded,
122121
value,
123122
setIsExpanded,
124123
setValue,
125-
isExpandedRef,
126124
};
127125
};
128126

@@ -346,6 +344,7 @@ const ComboboxInput = forwardRef<HTMLInputElement, PropsWithRef<ComboboxInputPro
346344
);
347345
},
348346
);
347+
ComboboxInput.displayName = 'ComboboxInput';
349348

350349
interface HighlightProps extends HTMLAttributes<HTMLElement> {
351350
currentInput?: string;
@@ -375,7 +374,7 @@ const Highlight: React.FC<PropsWithChildren<HighlightProps>> = ({
375374
<>
376375
{children.substring(0, index)}
377376
{as === 'mark' ? (
378-
<mark {...props}>{children.substring(index, inputLength)}</mark>
377+
<mark {...props}>{children.substring(index, index + inputLength)}</mark>
379378
) : (
380379
<span {...props}>{children.substring(index, inputLength)}</span>
381380
)}
@@ -409,6 +408,7 @@ const ListboxOption = forwardRef<HTMLLIElement, PropsWithRef<ListboxOptionProps>
409408
);
410409
},
411410
);
411+
ListboxOption.displayName = 'ListboxOption';
412412

413413
type ListboxProps = HTMLAttributes<HTMLUListElement>;
414414

@@ -443,6 +443,7 @@ const Listbox = forwardRef<HTMLUListElement, PropsWithRef<ListboxProps>>(({ chil
443443
</ul>
444444
);
445445
});
446+
Listbox.displayName = 'Listbox';
446447

447448
/*
448449
* Combobox - high-level component
@@ -537,6 +538,7 @@ const useInternalSelectedItem = (item?: Item): [Item | undefined, (item: Item) =
537538
return [selectedItem, setSelectedItem];
538539
};
539540

541+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
540542
interface Item extends Record<string, any> {
541543
id: string;
542544
value: string; // Used for filtering. Used for displaying and highlighting if node not provided.
@@ -573,19 +575,6 @@ const useFilters = (
573575
return [filtered, filteredRef];
574576
};
575577

576-
const getTextboxTitle = (isExpanded: boolean, length: number, title?: string) => {
577-
const formattedTitle = title ? (title[title.length - 1] === '.' ? title : title + '.') : undefined;
578-
const ariaContent = `Listbox is ${isExpanded ? 'expanded' : 'closed'}. ${length} ${
579-
length === 1 ? 'option matches' : 'options match'
580-
} your input. Use ArrowDown to navigate between options.`;
581-
return formattedTitle ? `${formattedTitle} ${ariaContent}` : ariaContent;
582-
};
583-
584-
// type LabelProps<T> = {
585-
// showLabel: T;
586-
// label?: T extends true ? ReactNode : string;
587-
// };
588-
589578
type LabelProps =
590579
| {
591580
showLabel?: false;
@@ -723,7 +712,6 @@ const DatalistInput = forwardRef<HTMLDivElement, PropsWithRef<DatalistInputProps
723712
onChange={handleWith(handleChange, inputProps?.onChange)}
724713
onKeyDown={handleWith(handleKeyDownOnInput, inputProps?.onKeyDown)}
725714
aria-label={!showLabel && typeof label === 'string' ? label : undefined}
726-
title={getTextboxTitle(internalIsExpanded, filteredItems.length, inputProps?.title)}
727715
className={`react-datalist-input__textbox ${inputProps?.className}`}
728716
/>
729717
{((filteredItems.length && internalIsExpanded) || isCollapsedClassName || isCollapsedStyle) && (
@@ -739,7 +727,7 @@ const DatalistInput = forwardRef<HTMLDivElement, PropsWithRef<DatalistInputProps
739727
...listboxProps?.style,
740728
}}
741729
>
742-
{filteredItems.map((item, i) => (
730+
{filteredItems.map((item) => (
743731
<ListboxOption
744732
{...listboxOptionProps}
745733
aria-label={item.label || item.value}
@@ -760,6 +748,7 @@ const DatalistInput = forwardRef<HTMLDivElement, PropsWithRef<DatalistInputProps
760748
);
761749
},
762750
);
751+
DatalistInput.displayName = 'DatalistInput';
763752

764753
export type {
765754
DatalistInputProps,

tests/remix-demo/app/routes/demo.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ export default function Index() {
3333
placeholder="Chocolate"
3434
items={items}
3535
onSelect={(i) => console.log(i)}
36+
inputProps={{
37+
onClick: (e) => console.log('CLICK', e),
38+
}}
3639
/>
3740
</div>
3841
</div>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import type { LinksFunction } from 'remix';
2+
import { DatalistInput, useComboboxControls } from '../combobox';
3+
4+
import comboboxStyles from '../combobox.css';
5+
6+
export const links: LinksFunction = () => {
7+
return [{ rel: 'stylesheet', href: comboboxStyles }];
8+
};
9+
10+
const items = new Array(1000).fill(0).map((_, index) => ({
11+
id: `${index}`,
12+
value: `value ${index}`,
13+
}));
14+
15+
export default function Index() {
16+
console.log(items);
17+
return (
18+
<div
19+
style={{
20+
fontFamily: 'system-ui, sans-serif',
21+
lineHeight: '1.4',
22+
width: '100%',
23+
marginTop: '10vh',
24+
display: 'flex',
25+
justifyContent: 'center',
26+
}}
27+
>
28+
<div style={{ width: '500px' }}>
29+
<DatalistInput
30+
label="Select your favorite flavor"
31+
placeholder="Chocolate"
32+
items={items}
33+
onSelect={(i) => console.log(i)}
34+
listboxProps={{
35+
style: { maxHeight: '300px', overflowY: 'scroll' },
36+
}}
37+
/>
38+
</div>
39+
</div>
40+
);
41+
}

tests/remix-demo/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"paths": {
1414
"~/*": ["./app/*"]
1515
},
16-
16+
// TODO once react 18 types are in @types/react, "node", "react/next", "react-dom/next" field can be removed and tslibs library removed from npm dependencies
17+
"types": ["node", "react/next", "react-dom/next"],
1718
// Remix takes care of building everything in `remix build`.
1819
"noEmit": true
1920
}

0 commit comments

Comments
 (0)