N-FIN-42: set default category when payee is selected

This commit is contained in:
Markus Thielker 2024-03-17 12:52:12 +01:00
parent 715ce17e1f
commit 3c19d8b639
No known key found for this signature in database
2 changed files with 22 additions and 3 deletions

View file

@ -166,7 +166,17 @@ export default function PaymentForm({value, entities, categories, onSubmit, clas
placeholder="Select payee"
items={entitiesMapped}
next={categoryRef}
{...field} />
{...field}
onChange={(e) => {
field.onChange(e);
if (e && e.target.value) {
const entity = entities.find((entity) => entity.id === Number(e.target.value));
console.log(entity?.defaultCategoryId);
if (entity?.defaultCategoryId !== null) {
form.setValue('categoryId', entity?.defaultCategoryId);
}
}
}}/>
</FormControl>
<FormMessage/>
</FormItem>

View file

@ -13,12 +13,12 @@ export interface AutoCompleteInputProps
const AutoCompleteInput = React.forwardRef<HTMLInputElement, AutoCompleteInputProps>(
({className, type, ...props}, ref) => {
const [value, setValue] = useState(getInitialValue());
const [value, setValue] = useState(getNameOfPropValue());
const [open, setOpen] = useState(false);
const [lastKey, setLastKey] = useState('');
const [filteredItems, setFilteredItems] = useState(props.items);
function getInitialValue() {
function getNameOfPropValue() {
if (!props.items) {
return '';
@ -50,6 +50,15 @@ const AutoCompleteInput = React.forwardRef<HTMLInputElement, AutoCompleteInputPr
}
}, [filteredItems]);
useEffect(() => {
console.log('Prop value changed', value, props.value);
if (props.value) {
setValue(getNameOfPropValue());
} else {
setValue('');
}
}, [props.value]);
return (
<div className="relative">
<input