Async Data option is missing in Select Component #345
Replies: 7 comments 14 replies
-
You can subscribe to search query changes with |
Beta Was this translation helpful? Give feedback.
-
Is there any support for a loading indicator in the dropdown? If not, do you happen to have any recommendations for showing the user that the request sent on the searchChange event is pending? Appreciate the advice, love the UI library! |
Beta Was this translation helpful? Give feedback.
-
Implementing your Select combined with react-query and react-hook-form was child's play. Well done! Below the code as if someone needed it.
|
Beta Was this translation helpful? Give feedback.
-
Hello @rtivital, I'm trying to implement searchable Select component using
With the code above this is what is happening.
The problem I see is when I select an option Is there any way I can achieve this? Thanks, |
Beta Was this translation helpful? Give feedback.
-
Does creatable prop support async way of creating options? |
Beta Was this translation helpful? Give feedback.
-
Here's how I did this, also has error handling. import {
ActionIcon,
Loader,
MultiSelect,
MultiSelectProps
} from '@mantine/core';
import { IconRefresh } from '@tabler/icons';
import React, { useCallback, useEffect, useState } from 'react';
import { getFullName } from 'utils/users';
import { xhr } from 'utils/xhr';
type State = {
data: { value: string; label: string }[];
status: 'initial' | 'loading' | 'error' | 'success';
};
type Props = Omit<MultiSelectProps, 'data'>;
const UserSelect: React.FunctionComponent<Props> = props => {
const [{ status, data }, setState] = useState<State>({
data: [],
status: 'initial'
});
const fetchData = useCallback(async () => {
try {
setState(oldState => {
return {
...oldState,
status: 'loading'
};
});
const response = await xhr('/list-users');
const {
data
}: {
data: User[];
nextToken?: string;
} = await response.json();
setState({
data: data.map(user => {
return {
value: user.id,
label: getFullName(user)
};
}),
status: 'success'
});
} catch (error) {
console.log(error);
setState(oldState => {
return {
...oldState,
status: 'error'
};
});
}
}, []);
useEffect(() => {
if (status === 'initial') fetchData();
}, [status, fetchData]);
if (status === 'initial' || status === 'loading') {
return (
<MultiSelect
{...props}
data={[]}
disabled
rightSection={<Loader size={20} />}
/>
);
}
return (
<MultiSelect
{...props}
data={data}
searchable
nothingFound={
status === 'error'
? 'There was an error loading the data. Click refresh icon to try again.'
: 'Nothing found'
}
maxDropdownHeight={150}
limit={20}
rightSection={
status === 'error' && (
<ActionIcon onClick={fetchData}>
<IconRefresh size={20} />
</ActionIcon>
)
}
/>
);
};
export default UserSelect; |
Beta Was this translation helpful? Give feedback.
-
infinite loading support ? |
Beta Was this translation helpful? Give feedback.
-
Hi @rtivital ,
Async way of loading data / options in Select & Multiselect in missing.
I am using matine as UI component library in my project but due to this async way of loading data i have to use another package.
Beta Was this translation helpful? Give feedback.
All reactions