import * as React from 'react'; import { SupportCreateSuggestionOptions, useSupportCreateSuggestion, useCreateSuggestionContext, } from './useSupportCreateSuggestion'; import { CreateBase } from '../create/CreateBase'; import { SimpleForm, TextInput } from '../../test-ui'; import { CoreAdminContext } from '../../core/CoreAdminContext'; import { DataProvider } from '../../types'; export default { title: 'ra-core/controller/input/useSupportCreateSuggestion' }; const CreateElement = () => { const { filter, onCancel, onCreate } = useCreateSuggestionContext(); return ( ); }; export const UseSupportCreateSuggestion = ( options: Pick< SupportCreateSuggestionOptions, | 'createLabel' | 'createItemLabel' | 'createValue' | 'createHintValue' | 'optionText' > & { withCreateElement?: boolean } ) => { const [value, setValue] = React.useState(''); const [filter, setFilter] = React.useState(''); const [createParams, setCreateParams] = React.useState(null); const [onCreateParams, setOnCreateParams] = React.useState(null); const { withCreateElement, ...rest } = options; const { createId, createHintId, getCreateItem, handleChange, createElement, getOptionDisabled, } = useSupportCreateSuggestion({ filter, handleChange: eventOrValue => { setValue(eventOrValue?.target?.value ?? eventOrValue.id); }, onCreate: withCreateElement ? undefined : arg => { setOnCreateParams(arg); return { id: 'new_id_from_onCreate' }; }, create: withCreateElement ? : undefined, ...rest, }); const createItem = getCreateItem(filter); const disabled = getOptionDisabled(createItem); const dataProvider = { create: async (_, args) => { setCreateParams(args); return { data: { id: 'new_id_from_create' } }; }, } as unknown as DataProvider; return (
Return values
{JSON.stringify({ createId, createHintId }, null, 2)}
Create Item
{JSON.stringify({ createItem, disabled }, null, 2)}
Inputs  
  setFilter(e.target.value)} />
{withCreateElement ? ( <>
Create Element {createElement}
Called create with
{JSON.stringify(createParams, null, 2)}
) : (
Called onCreate with
{JSON.stringify(onCreateParams, null, 2)}
)}
); }; UseSupportCreateSuggestion.argTypes = { createLabel: { control: 'text' }, createItemLabel: { control: 'text' }, createValue: { control: 'text' }, createHintValue: { control: 'text' }, optionText: { control: 'text' }, withCreateElement: { control: 'boolean' }, };