File size: 1,647 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { ChoicesProps } from 'react-admin';
import { ReferenceInput, SelectInput } from 'react-admin';
import { connectField } from 'uniforms';
import React from 'react';

type InputProps = {
  allowEmpty?: boolean;
  className?: string;
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  filterToQuery?: (filter: string) => any;
  label?: string | boolean | null;
  perPage?: number;
  reference: string;
  name: string;
};
type Props = InputProps & Pick<ChoicesProps, 'optionText' | 'optionValue'>;

/**
 * RaSelectReferenceInputField can be used in an autoform to
 */
const RaSelectReferenceInputField: React.FC<Omit<Props, 'children'>> =
  connectField(
    ({
      allowEmpty = true,
      value = null as any,
      onChange,
      optionText,
      optionValue,
      label,
      ...props
    }: Props & { value: string; onChange: (s: string) => void }) => {
      return (
        <ReferenceInput
          label={label as string}
          reference={props.reference}
          perPage={props.perPage}
          allowEmpty={allowEmpty}
          meta={{}}
          input={
            {
              value,
              onChange: (e: any) => {
                onChange(e.target.value);
              },
            } as any
          }
          source={null as any}
        >
          <SelectInput
            optionText={optionText || 'id'}
            emptyValue={null}
            optionValue={optionValue || 'id'}
            options={{ value }}
          />
        </ReferenceInput>
      );
    }
  );

export default RaSelectReferenceInputField;