import * as React from 'react' import type { StoryObj, Meta } from '@storybook/react-webpack5' import { GridEditorCombobox } from '.' import { Grid } from '../../grids' import { GridCellDefault } from '../../components' import { PlusCircle } from '@planview/pv-icons' import type { GridConfirmPayload } from '../../types' import type { InputType } from '@storybook/csf' const hideControl: InputType = { control: false } export default { title: 'pv-grid/Components/Editors/GridEditorCombobox', component: GridEditorCombobox, parameters: { controls: { exclude: ['label', 'mode', 'rowId', 'columnId'], }, }, argTypes: { columnId: hideControl, rowId: hideControl, tabIndex: hideControl, value: hideControl, icon: { options: ['no icon', 'with custom icon'], mapping: { 'no icon': undefined, 'with custom icon': , }, control: { type: 'radio', }, }, }, } satisfies Meta const privacyOptions: { label: string value: string }[] = [ { label: 'Private', value: 'private', }, { label: 'Public', value: 'public', }, ] type PrivacyRow = { id: string privacy: string } export const Default: StoryObj = { render: function Component(args) { const [rows, setRows] = React.useState([ { id: '1', privacy: privacyOptions[0].value }, { id: '2', privacy: privacyOptions[1].value }, { id: '3', privacy: privacyOptions[0].value }, ]) return ( props.label === item.value )?.label } withCaret /> ) }, Editor(props) { return ( option.value === props.value ) } /> ) }, }, }, ]} rows={rows} onCellChange={( confirm: GridConfirmPayload< PrivacyRow, 'privacy', { value: string } | null > ) => { setRows((r) => r.map((row) => { if (row.id === confirm.rowId) { return { ...row, privacy: confirm.nextValue?.value || '', } } return row }) ) }} /> ) }, } Default.parameters = { docs: { source: { code: ` const [rows, setRows] = React.useState([ { id: '1', privacy: privacyOptions[0].value }, { id: '2', privacy: privacyOptions[1].value }, { id: '3', privacy: privacyOptions[0].value }, ]) return ( props.label === item.value )?.label } withCaret /> ) }, Editor(props) { return ( option.value === props.value ) } /> ) }, }, }, ]} rows={rows} onCellChange={( confirm: GridConfirmPayload< PrivacyRow, 'privacy', { value: string } | null > ) => { setRows((r) => r.map((row) => { if (row.id === confirm.rowId) { return { ...row, privacy: confirm.nextValue?.value || '', } } return row }) ) }} /> ) `, }, }, } export const WithAvatars: StoryObj = { render: function Component(args) { const users = React.useMemo( () => [ { value: '1', label: 'Jane Doe', avatar: 'img/avatars/initial_avatar_DS.png', }, { value: '2', label: 'Albert Doe', avatar: 'img/avatars/initial_avatar_DS.png', }, ], [] ) const [rows, setRows] = React.useState([ { id: '2', user: users[0].value }, { id: '3', user: users[1].value }, ]) return ( users.find( (item) => label === item.value ), [label] ) return ( ) }, Editor({ value, ...props }) { const valOption = React.useMemo( () => users.find( (option) => option.value === value ), [value] ) return ( ) }, }, }, ]} rows={rows} onCellChange={( confirm: GridConfirmPayload< { id: string }, 'user', { value: string } | null > ) => { setRows((r) => r.map((row) => { if (row.id === confirm.rowId) { return { ...row, user: confirm.nextValue?.value || '', } } return row }) ) }} /> ) }, } WithAvatars.parameters = { controls: { exclude: ['label', 'mode', 'rowId', 'columnId', 'icon'], }, docs: { source: { code: ` Component(args) { const users = React.useMemo( () => [ { value: '1', label: 'Jane Doe', avatar: 'img/avatars/initial_avatar_DS.png', }, { value: '2', label: 'Albert Doe', avatar: 'img/avatars/initial_avatar_DS.png', }, ], [] ) const [rows, setRows] = React.useState([ { id: '2', user: users[0].value }, { id: '3', user: users[1].value }, ]) return ( users.find( (item) => label === item.value ), [label] ) return ( ) }, Editor({ value, ...props }) { const valOption = React.useMemo( () => users.find( (option) => option.value === value ), [value] ) return ( ) }, }, }, ]} rows={rows} onCellChange={( confirm: GridConfirmPayload< { id: string }, 'user', { value: string } | null > ) => { setRows((r) => r.map((row) => { if (row.id === confirm.rowId) { return { ...row, user: confirm.nextValue?.value || '', } } return row }) ) }} /> ) } `, }, }, }