index.tsx 1.3 KB
import { Field } from '@formily/react';
import { FormItem, Cascader } from '@formily/antd-v5';
import cx from 'classnames';
import './index.less';
import { FieldProps } from '../../typings';

/**
 * An interface that defines an option in the Cascader component.
 */
interface Option {
  value: string | number;
  label: string;
  disabled?: boolean;
  children?: Option[];
}

/**
 * An interface that defines the properties of the CascaderInput component.
 */
export interface CascaderInputProps extends FieldProps {
  options: Option[];
}

/**
 * The CascaderInput component.
 *
 * This is a wrapper around the Cascader component that makes it easier to use in a Formily form.
 */
const CascaderInput: React.FC<CascaderInputProps> = (props) => {
  const { name, title, validator = [], decoratorProps, componentProps, options } = props;

  return (
    <div className={cx('global_cascader')}>
      <Field
        {...props}
        name={name}
        title={title}
        dataSource={options}
        decorator={[FormItem, { ...decoratorProps }]}
        component={[
          Cascader,
          {
            allowClear: true,
            ...componentProps,
            componenttypename: 'Cascader',
          },
        ]}
        validator={validator}
      />
    </div>
  );
};

export default CascaderInput;