import * as React from 'react'; import fakerestDataProvider from 'ra-data-fakerest'; import polyglotI18nProvider from 'ra-i18n-polyglot'; import englishMessages from 'ra-language-english'; import { Form } from '../Form'; import { useInput } from '../useInput'; import { CoreAdminContext, CreateBase, DataProvider, EditBase, FormDataConsumer, ValidationError, mergeTranslations, useUnique, } from '../..'; import { QueryClient } from '@tanstack/react-query'; import { TestMemoryRouter } from '../../routing'; export default { title: 'ra-core/form/validation/useUnique', }; const Input = props => { const { field, fieldState } = useInput(props); return ( <> {fieldState.error && fieldState.error?.message ? ( ) : null} ); }; const OrgSelect = props => { const { field, fieldState } = useInput(props); return ( <>

{fieldState.error?.message}

); }; const defaultDataProvider = fakerestDataProvider( { users: [ { id: 1, name: 'John Doe', organization_id: 1 }, { id: 2, name: 'Jane Doe', organization_id: 2 }, ], organizations: [ { id: 1, name: 'BigCorp' }, { id: 2, name: 'EvilCorp' }, ], }, process.env.NODE_ENV !== 'test' ); const i18nProvider = polyglotI18nProvider(() => mergeTranslations(englishMessages, { myapp: { validation: { unique: 'Value %{value} is already in use for %{field}', }, }, }) ); const Wrapper = ({ children, dataProvider = defaultDataProvider }) => { return ( {children} ); }; const BasicForm = () => { const unique = useUnique(); return (

The name field should be unique. Try to enter "John Doe" or "Jane Doe".

); }; export const Create = ({ dataProvider }: { dataProvider?: DataProvider }) => { return ( ); }; const EditForm = () => { const unique = useUnique(); return (

The name field should be unique. Try to enter "John Doe". Jane Doe should work as this is the current record value

); }; export const Edit = ({ dataProvider, id = 2, }: { dataProvider?: DataProvider; id?: number; }) => { return ( ); }; const DeepFieldForm = () => { const unique = useUnique(); return (

The name field should be unique. Try to enter "John Doe" or "Jane Doe".

); }; export const DeepField = ({ dataProvider = fakerestDataProvider( { users: [ { id: 1, identity: { name: 'John Doe' }, organization_id: 1 }, { id: 2, identity: { name: 'Jane Doe' }, organization_id: 2 }, ], organizations: [ { id: 1, name: 'BigCorp' }, { id: 2, name: 'EvilCorp' }, ], }, process.env.NODE_ENV !== 'test' ), }) => { return ( ); }; const WithMessageForm = () => { const unique = useUnique(); return (

The name field should be unique. Try to enter "John Doe" or "Jane Doe".

); }; export const WithMessage = ({ dataProvider, }: { dataProvider?: DataProvider; }) => { return ( ); }; const WithTranslatedMessageForm = () => { const unique = useUnique(); return (

The name field should be unique. Try to enter "John Doe" or "Jane Doe".

Show that the value and the field label are passed to the translation provider to build the validation message

); }; export const WithTranslatedMessage = () => { return ( ); }; const WithAdditionalFiltersForm = () => { const unique = useUnique(); return (

The name field should be unique. "John Doe" is already registered for BigCorp and "Jane Doe" is already registered for EvilCorp.

{({ formData }) => ( )} ); }; export const WithAdditionalFilters = ({ dataProvider, }: { dataProvider?: DataProvider; }) => { return ( ); }; export const DataProviderErrorOnValidation = () => { let fail = true; const errorDataProvider = { ...defaultDataProvider, getList: (resource, params) => { if (fail) { fail = false; return Promise.reject(new Error('API is down')); } fail = true; return defaultDataProvider.getList(resource, params); }, }; return (

The validation will fail one time over two

); };