import { Meta, Canvas, Controls } from '@storybook/addon-docs/blocks'
import * as ComboboxMultiStories from './multi.stories.tsx'

<Meta of={ComboboxMultiStories} />

# GridEditorComboboxMulti

```tsx
import { GridEditorComboboxMulti } from '@planview/pv-grid'
```

The combobox multi-select cell editor uses the [Combobox](?path=/docs/pv-uikit-combobox--docs) component internally (With `multiSelectable` set to `true`). It lets users add or edit multiple values from a set of predefined options, by either typing a value directly or by selecting them from the list.

**Note:** When using the `GridEditorComboboxMulti` it is [good practice](https://design.planview.com/components/grid/grid-editors#combobox-editor) to use the `GridCellChips` in the `Renderer` function.

<Canvas of={ComboboxMultiStories.Default} />

### Props provided by the Grid (via `Editor`)

Additional props are provided by the grid that are not used by this editor.

<Controls include={['onConfirm', 'onCancel', 'tabIndex']} />

### Props to customize behavior

<Controls exclude={['onConfirm', 'onCancel', 'tabIndex']} />

## Usage with Column definition

This component should be used as part of a custom `Editor` method on the Column configuration. To prevent combobox from being unmounted and remounted during editing, it is important your `Editor` function does not get redefined. This can be accomplished by memoizing the column definition (`useMemo`) or by keeping the `Editor` defined outside of the component as shown in this example.

```tsx
const tags = [
    'urgent',
    'important',
    'phone',
    'office',
    'errands',
    'low priority',
    'fun',
]

type TagRow = {
    id: string
    tags: string[]
}

function TagComboboxEditor(props: CellEditorParams<TagRow>): React.JSX.Element {
    const tagOptions = tags.map((t) => ({ value: t, label: t }))
    const currentTags = props.value as string[]

    return (
        <GridEditorComboboxMulti
            {...props}
            clearable={false}
            value={currentTags.map(
                (t) => tagOptions.find((tagOpt) => t === tagOpt.value)!
            )}
            options={tagOptions}
        />
    )
}

export const Default: StoryObj<typeof GridEditorComboboxMulti> = {
    render: function Component(args) {
        const [rows, setRows] = React.useState<TagRow[]>([
            { id: '1', tags: ['urgent', 'phone'] },
            { id: '2', tags: ['office'] },
            { id: '3', tags: ['errands', 'low priority'] },
        ])

        return (
            <Grid
                selectionMode="none"
                columns={[
                    {
                        id: 'tags',
                        label: 'Combobox Multi Editor',
                        sortable: false,
                        width: 300,
                        cell: {
                            editable: true,
                            Renderer(props) {
                                return <GridCellChips {...props} />
                            },
                            Editor: TagComboboxEditor,
                        },
                    },
                ]}
                rows={rows}
                onCellChange={(
                    confirm: GridConfirmPayload<
                        TagRow,
                        'tags',
                        [{ value: string }]
                    >
                ) => {
                    setRows((r) =>
                        r.map((row) => {
                            if (row.id === confirm.rowId) {
                                return {
                                    ...row,
                                    tags: confirm.nextValue.map(
                                        (co) => co.value
                                    ),
                                }
                            }
                            return row
                        })
                    )
                }}
            />
        )
    },
}
```
