Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vault category auto select #97

Merged
merged 3 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions frontend/src/components/templates/vault-create/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { SelectCategory } from './select-category'
export interface IOption {
readonly label: string
readonly value: number
readonly disabled: boolean
}

interface IVaultCreateTemplate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,26 @@ interface ISelectCategory {
clearErrors?(): void
}

const createOption = (label: string, value: number): IOption => ({
const createOption = (
label: string,
value: number,
disabled?: boolean
): IOption => ({
label,
value: value,
disabled: disabled ?? false,
})

export const SelectCategory: React.FC<ISelectCategory> = ({
vaultCategories,
createVaultCategory,
setCategorySelected,
categorySelected,
clearErrors
clearErrors,
}) => {
const [isLoading, setIsLoading] = useState(false)
const [options, setOptions] = useState<IOption[]>([])
const [value, setValue] = useState<IOption>()

const { colorMode } = useColorMode()

Expand All @@ -41,14 +47,16 @@ export const SelectCategory: React.FC<ISelectCategory> = ({
setIsLoading(false)
setOptions(prev => [...prev, newOption])
setCategorySelected(newOption)
setValue(newOption)
}, 1000)
}

useEffect(() => {
const ops = vaultCategories?.map(
let ops = vaultCategories?.map(
(vaultCategory: Hooks.UseVaultsTypes.IVaultCategory) =>
createOption(vaultCategory.name, vaultCategory.id)
)
ops = [...(ops || []), createOption('Type to create...', 0, true)]
setOptions(ops || [])
}, [vaultCategories])

Expand All @@ -59,13 +67,19 @@ export const SelectCategory: React.FC<ISelectCategory> = ({
isLoading={isLoading}
onCreateOption={handleVaultCategory}
options={options}
value={value}
isOptionDisabled={(option): boolean => option.disabled}
onChange={(newValue): void => {
if (clearErrors) clearErrors()
setCategorySelected(newValue)
}}
defaultValue={
categorySelected
? { value: categorySelected.value, label: categorySelected.label }
? {
value: categorySelected.value,
label: categorySelected.label,
disabled: false,
}
: undefined
}
styles={{
Expand All @@ -80,9 +94,10 @@ export const SelectCategory: React.FC<ISelectCategory> = ({
...base,
backgroundColor: colorMode === 'dark' ? '#303448' : undefined,
}),
option: (styles, { isFocused, isSelected }) => ({
option: (styles, { isFocused, isSelected, isDisabled }) => ({
...styles,
color: colorMode === 'dark' ? 'white' : 'black',
opacity: isDisabled ? '0.5' : undefined,
background: isFocused
? colorMode === 'dark'
? '#292d3e'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const Form: React.FC<IHeader> = ({
}) => {
const [categorySelected, setCategorySelected] = useState<
IOption | null | undefined
>(category && { label: category?.name, value: category?.id })
>(category && { label: category?.name, value: category?.id, disabled: false })

const [name, setName] = useState<string | undefined>(vault.name)

Expand Down