selector.select
This commit is contained in:
1 parent
596c3363e7
commit
0fc3ea6146
4 files changed
+155
-65
No files matched your search
@@ -5,19 +5,23 @@ import TextField from "@mui/material/TextField";
|
||||
import Chip from "@mui/material/Chip";
|
||||
import { Badge } from "@mui/material";
|
||||
import { SearchItem } from "./extras";
|
||||
import { ReactNode } from "react";
|
||||
|
||||
export type InputAutoCompleteProps<T extends Option<any>> = {
|
||||
className?: string;
|
||||
validateOption?: (v: string[]) => string[] | null;
|
||||
groupBy?: (opt: T) => string;
|
||||
options: T[];
|
||||
getLabel?: (opt: T) => string;
|
||||
getID?: (opt: T) => string;
|
||||
helperText?: string;
|
||||
endAdornment?: ReactNode;
|
||||
required?: boolean;
|
||||
onlyShowLabel?: boolean;
|
||||
} & InputAutoCompletePropsBase;
|
||||
|
||||
export type InputAutoCompletePropsBase = {
|
||||
label?: string;
|
||||
className?: string;
|
||||
} & (
|
||||
| {
|
||||
value: string | null;
|
||||
@@ -155,6 +159,7 @@ export function InputAutoComplete<T extends Option>(
|
||||
id={id}
|
||||
label={label}
|
||||
searchTerm={inputValue}
|
||||
onlyShowLabel={props.onlyShowLabel}
|
||||
/>
|
||||
);
|
||||
}}
|
||||
@@ -162,6 +167,7 @@ export function InputAutoComplete<T extends Option>(
|
||||
<TextField
|
||||
{...params}
|
||||
variant="filled"
|
||||
required={props.required}
|
||||
label={props.label ?? "Entity ID"}
|
||||
helperText={helperText}
|
||||
error={error}
|
||||
@@ -181,7 +187,9 @@ export function InputAutoComplete<T extends Option>(
|
||||
label={
|
||||
<>
|
||||
{id === label ? <span>{label}</span> : <b>{label}</b>}
|
||||
{id !== label && <small>{id}</small>}
|
||||
{id !== label && !props.onlyShowLabel && (
|
||||
<small>{id}</small>
|
||||
)}
|
||||
</>
|
||||
}
|
||||
{...tagProps}
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
import TextField from "@mui/material/TextField";
|
||||
import Autocomplete from "@mui/material/Autocomplete";
|
||||
import { ReactNode } from "react";
|
||||
|
||||
export interface InputAutoTextProps {
|
||||
label: string;
|
||||
value: string;
|
||||
className?: string;
|
||||
options?: string[];
|
||||
error?: string;
|
||||
required?: boolean;
|
||||
endAdornment?: ReactNode;
|
||||
onChange: (v: string) => void;
|
||||
}
|
||||
export default function InputAutoText({
|
||||
@@ -13,28 +17,37 @@ export default function InputAutoText({
|
||||
label,
|
||||
options = [],
|
||||
onChange,
|
||||
className,
|
||||
required,
|
||||
endAdornment,
|
||||
error,
|
||||
}: InputAutoTextProps) {
|
||||
|
||||
return <Autocomplete
|
||||
freeSolo
|
||||
value={value}
|
||||
options={options}
|
||||
onChange={(_e, v) => onChange(
|
||||
v === null ? "" : v
|
||||
)}
|
||||
renderInput={params => <TextField
|
||||
{...params}
|
||||
variant="filled"
|
||||
label={label}
|
||||
error={error !== undefined}
|
||||
helperText={error}
|
||||
onChange={e => {
|
||||
e.preventDefault();
|
||||
onChange(
|
||||
e.target.value
|
||||
)
|
||||
}}
|
||||
/>}
|
||||
/>
|
||||
console.log(options);
|
||||
return (
|
||||
<Autocomplete
|
||||
freeSolo
|
||||
className={className}
|
||||
value={value}
|
||||
options={options}
|
||||
onChange={(_e, v) => onChange(v === null ? "" : v)}
|
||||
renderInput={(params) => (
|
||||
<TextField
|
||||
{...params}
|
||||
required={required}
|
||||
variant="filled"
|
||||
label={label}
|
||||
error={error !== undefined}
|
||||
helperText={error}
|
||||
fullWidth
|
||||
InputProps={{
|
||||
endAdornment,
|
||||
}}
|
||||
onChange={(e) => {
|
||||
e.preventDefault();
|
||||
onChange(e.target.value);
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -1,47 +1,80 @@
|
||||
import "./extras.css";
|
||||
import { FC } from "react";
|
||||
import parse from 'autosuggest-highlight/parse';
|
||||
import match from 'autosuggest-highlight/match';
|
||||
|
||||
import parse from "autosuggest-highlight/parse";
|
||||
import match from "autosuggest-highlight/match";
|
||||
|
||||
export const HighlightElm: FC<{
|
||||
text: string,
|
||||
searchTerm: string
|
||||
}> = ({
|
||||
text,
|
||||
searchTerm
|
||||
}) => {
|
||||
const matches = match(text, searchTerm, {
|
||||
"findAllOccurrences": true,
|
||||
"insideWords": true
|
||||
});
|
||||
const parts = parse(text, matches);
|
||||
return <>
|
||||
{parts.map((part, index) => (
|
||||
<span
|
||||
key={index}
|
||||
style={{
|
||||
textDecoration: part.highlight ? 'underline' : 'unset'
|
||||
}}
|
||||
>
|
||||
{part.text}
|
||||
</span>
|
||||
))}
|
||||
</>
|
||||
}
|
||||
text: string;
|
||||
searchTerm: string;
|
||||
}> = ({ text, searchTerm }) => {
|
||||
const matches = match(text, searchTerm, {
|
||||
findAllOccurrences: true,
|
||||
insideWords: true,
|
||||
});
|
||||
const parts = parse(text, matches);
|
||||
return (
|
||||
<>
|
||||
{parts.map((part, index) => (
|
||||
<span
|
||||
key={index}
|
||||
style={{
|
||||
textDecoration: part.highlight ? "underline" : "unset",
|
||||
}}
|
||||
>
|
||||
{part.text}
|
||||
</span>
|
||||
))}
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export const SearchItem: FC<{
|
||||
listProps: React.HTMLAttributes<HTMLLIElement>,
|
||||
label: string,
|
||||
id: string,
|
||||
searchTerm: string,
|
||||
}> = ({
|
||||
listProps,
|
||||
label,
|
||||
id,
|
||||
searchTerm
|
||||
}) => <li {...listProps} className={[listProps.className ?? "", 'input-extras--search-item--list'].join(' ')}>
|
||||
<b><HighlightElm text={label} searchTerm={searchTerm} /></b> <small>
|
||||
<HighlightElm text={id} searchTerm={searchTerm} />
|
||||
</small>
|
||||
</li>
|
||||
listProps: React.HTMLAttributes<HTMLLIElement>;
|
||||
label: string;
|
||||
id: string;
|
||||
searchTerm: string;
|
||||
onlyShowLabel?: boolean;
|
||||
}> = ({ listProps, label, id, searchTerm, onlyShowLabel = false }) => {
|
||||
let inner = (
|
||||
<>
|
||||
<b>
|
||||
<HighlightElm text={label} searchTerm={searchTerm} />
|
||||
</b>{" "}
|
||||
<small>
|
||||
<HighlightElm text={id} searchTerm={searchTerm} />
|
||||
</small>
|
||||
</>
|
||||
);
|
||||
|
||||
if (onlyShowLabel) {
|
||||
if (label) {
|
||||
inner = (
|
||||
<>
|
||||
<b>
|
||||
<HighlightElm text={label} searchTerm={searchTerm} />
|
||||
</b>{" "}
|
||||
</>
|
||||
);
|
||||
} else {
|
||||
inner = (
|
||||
<>
|
||||
<small>
|
||||
<HighlightElm text={id} searchTerm={searchTerm} />
|
||||
</small>
|
||||
</>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<li
|
||||
{...listProps}
|
||||
className={[
|
||||
listProps.className ?? "",
|
||||
"input-extras--search-item--list",
|
||||
].join(" ")}
|
||||
>
|
||||
{inner}
|
||||
</li>
|
||||
);
|
||||
};
|
||||
Reference in new issue
Block a user