refine icon indicating copy to clipboard operation
refine copied to clipboard

[FEAT] custom optionLabel and remove duplicate list in useSelect hook.

Open dipbazz opened this issue 10 months ago • 10 comments

Is your feature request related to a problem? Please describe.

I wanted to add a custom optionLabel in useSelect hook which I couldn't manipulate in useSelect options, @omeraplak have helped me with creating a custom options which resolved my issues as below:

const { selectProps, queryResult, defaultValueQueryResult } = useSelect({
        resource: "resource",
});

const allOptions = [
    ...(queryResult?.data?.data || []),
    ...(defaultValueQueryResult?.data?.data || [])
];

// in select components I have created my custom option label as below.
<Select
    {...selectProps}
    options = {allOptions.map((p: IPoint) => ({
        label: `${p.location_name} - ${p.landmark}`,
        value: p.id
    }))}
/>

This was working fine but whenever I search in my select input box my default value get pushed to the list which result in a duplicate value showing multiple times.

Describe alternatives you've considered

I have tried to remove duplicate value by creating a function that filters out duplicate value as below:

const uniqueArrayofObject = <TData extends { id: number | string }>(options: TData[]): TData[] => {
    return Array.from(new Set(options.map(a => a.id)))
      .map(id => options.find(a => a.id === id)) as TData[];
}

And using this function on my allOptions:

const allOptions = uniqueArrayofObject<IPoint>([
    ...(queryResult?.data?.data || []),
    ...(defaultValueQueryResult?.data?.data || [])
]);

Additional context

No response

Describe the thing to improve

If we could have an option to create a custom optionLabel and filter out data based on unique value from the record that could solve this issue. As suggested by @salihozdemir useSelect can have a function that returns a custom optionLabel and unique value from item to compare for duplicate as

useSelect({
    optionLabel: (item) => `${item.someField} ${item.anotherField}`,
    optionValue: (item) => item.foo.id,
})

dipbazz avatar Aug 30 '23 07:08 dipbazz

Hello @dipbazz thanks for the detailed issue! @salihozdemir actually created a task in our backlog as you guys communicated. Would you like to create a PR for this one? If not, we'll deliver it in the coming weeks.

BatuhanW avatar Aug 30 '23 08:08 BatuhanW

We've discussed this internally and agreed to continue with @dipbazz's suggestion.

useSelect({
    optionLabel: (item) => `${item.someField} ${item.anotherField}`,
    optionValue: (item) => item.foo.id,
})

BatuhanW avatar Oct 02 '23 08:10 BatuhanW

@ BatuhanW , Can you assign me this issue?

Souradeephazra123 avatar Oct 15 '23 08:10 Souradeephazra123

Hello @Souradeephazra123, do you have time to work on this issue? If not, I'll unassign it so someone else can take it! Best.

BatuhanW avatar Nov 20 '23 07:11 BatuhanW

Yes sir, I want to work, it was going exam, that's why I didn't open github for some days.

Souradeephazra123 avatar Nov 20 '23 07:11 Souradeephazra123

Hello @BatuhanW is it okay if i take up this issue if not closed yet

ruthvik129 avatar Jan 09 '24 14:01 ruthvik129

Hello @ruthvik129 assigned to you!

BatuhanW avatar Jan 10 '24 08:01 BatuhanW

Hello @BatuhanW i see 2 useSelect hooks for , i am assuming it to be this one https://github.com/refinedev/refine/blob/master/packages/core/src/hooks/useSelect/index.ts and not the one under antD

Also , is there a storybook instance or similar to have a look at how the useSelect hook is working and to preview my changes

ruthvik129 avatar Jan 11 '24 14:01 ruthvik129

Brother @ruthvik129 are you still working on it? Or should I push the changes I have made? Sir @BatuhanW if there is no one working on it as well in team then you can assign this task to me 👍

Conqxeror avatar Feb 07 '24 07:02 Conqxeror

Hey @Conqxeror assigned, looking forward to your PR! 🙌🏼

BatuhanW avatar Feb 07 '24 11:02 BatuhanW