-
-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1021 from The-Commit-Company/develop
Version 1.6.8
- Loading branch information
Showing
20 changed files
with
794 additions
and
450 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
|
||
|
||
import { useCombobox } from "downshift"; | ||
import { Filter, SearchResult, useSearch } from "frappe-react-sdk"; | ||
import { useState } from "react"; | ||
import { Label } from "../Form"; | ||
import { Text, TextField } from "@radix-ui/themes"; | ||
import { useIsDesktop } from "@/hooks/useMediaQuery"; | ||
import clsx from "clsx"; | ||
|
||
export interface LinkFieldProps { | ||
doctype: string; | ||
filters?: Filter[]; | ||
label?: string, | ||
placeholder?: string, | ||
value: string, | ||
setValue: (value: string) => void, | ||
disabled?: boolean, | ||
autofocus?: boolean | ||
} | ||
|
||
|
||
const LinkField = ({ doctype, filters, label, placeholder, value, setValue, disabled, autofocus }: LinkFieldProps) => { | ||
|
||
const [searchText, setSearchText] = useState('') | ||
|
||
const isDesktop = useIsDesktop() | ||
|
||
const { data } = useSearch(doctype, searchText, filters) | ||
|
||
const items: SearchResult[] = data?.message ?? [] | ||
|
||
const { | ||
isOpen, | ||
// getToggleButtonProps, | ||
getLabelProps, | ||
getMenuProps, | ||
getInputProps, | ||
highlightedIndex, | ||
getItemProps, | ||
selectedItem, | ||
} = useCombobox({ | ||
onInputValueChange({ inputValue }) { | ||
setSearchText(inputValue ?? '') | ||
}, | ||
items: items, | ||
itemToString(item) { | ||
return item ? item.value : '' | ||
}, | ||
selectedItem: items.find(item => item.value === value), | ||
onSelectedItemChange({ selectedItem }) { | ||
setValue(selectedItem?.value ?? '') | ||
}, | ||
}) | ||
|
||
return <div className="w-full"> | ||
<div className="flex flex-col gap-1"> | ||
<Label className="w-fit" {...getLabelProps()}> | ||
{label} | ||
</Label> | ||
<TextField.Root | ||
placeholder={placeholder ?? `Search ${doctype}`} | ||
className='w-full' | ||
disabled={disabled} | ||
autoFocus={isDesktop && autofocus} | ||
{...getInputProps()} | ||
> | ||
|
||
</TextField.Root> | ||
</div> | ||
<ul | ||
className={`sm:w-[550px] w-[24rem] absolute bg-background rounded-b-md mt-1 shadow-md z-[9999] max-h-96 overflow-scroll p-0 ${!(isOpen && items.length) && 'hidden' | ||
}`} | ||
{...getMenuProps()} | ||
> | ||
{isOpen && | ||
items.map((item, index) => ( | ||
<li | ||
className={clsx( | ||
highlightedIndex === index && 'bg-accent-4', | ||
selectedItem === item && 'font-bold', | ||
'py-2 px-3 shadow-sm flex gap-2 items-center', | ||
)} | ||
key={`${item.value}`} | ||
{...getItemProps({ item, index })} | ||
> | ||
<div className='flex flex-col'> | ||
<Text as='span' weight='medium' size='2'>{item.label ?? item.value}</Text> | ||
<Text as='span' size='1' color='gray'>{item.description}</Text> | ||
</div> | ||
|
||
</li> | ||
))} | ||
</ul> | ||
</div> | ||
} | ||
|
||
export default LinkField |
30 changes: 30 additions & 0 deletions
30
frontend/src/components/common/LinkField/LinkFormField.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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Controller, ControllerProps, useFormContext } from 'react-hook-form' | ||
import LinkField, { LinkFieldProps } from './LinkField' | ||
|
||
interface LinkFormFieldProps extends Omit<LinkFieldProps, 'value' | 'setValue'> { | ||
name: string, | ||
rules: ControllerProps['rules'], | ||
disabled?: boolean | ||
} | ||
|
||
const LinkFormField = ({ name, rules, ...linkFieldProps }: LinkFormFieldProps) => { | ||
|
||
const { control } = useFormContext() | ||
return ( | ||
<Controller | ||
name={name} | ||
control={control} | ||
disabled={linkFieldProps.disabled} | ||
rules={rules} | ||
render={({ field }) => ( | ||
<LinkField | ||
value={field.value} | ||
setValue={field.onChange} | ||
{...linkFieldProps} | ||
/> | ||
)} | ||
/> | ||
) | ||
} | ||
|
||
export default LinkFormField |
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
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
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
Oops, something went wrong.