Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SimpleFormIterator: Clear items call to action #8287

Closed
soullivaneuh opened this issue Oct 21, 2022 · 3 comments
Closed

SimpleFormIterator: Clear items call to action #8287

soullivaneuh opened this issue Oct 21, 2022 · 3 comments

Comments

@soullivaneuh
Copy link
Contributor

soullivaneuh commented Oct 21, 2022

Is your feature request related to a problem? Please describe.

Sometimes, we need to get an array field to be emptied.

Currently, the only way to achieve that is to remove each row of the array input, one by one because the inner form iterator does not have any clear action available.

Describe the solution you'd like
A "Clear items" call to action that will remove all the items at once.

Describe alternatives you've considered
We did a custom ArrayInput that implement this functionalities:

import React, { FC } from 'react';
import {
  ArrayInput as BaseArrayInput,
  ArrayInputProps as BaseArrayInputProps,
  useTranslate,
  Button,
} from 'react-admin';
import {
  useFormContext,
} from 'react-hook-form';
import {
  CachedOutlined,
} from '@mui/icons-material';

interface ArrayInputProps extends BaseArrayInputProps {
  enableClearItem?: boolean;
}

export const ArrayInput:FC<ArrayInputProps> = ({
  enableClearItem,
  children,
  source,
  ...props
}) => {
  const { setValue, getValues } = useFormContext();
  const translate = useTranslate();
  const inputValues = getValues(source);
  return (
    <>
      <BaseArrayInput
        source={source}
        {...props}
      >
        {children}
      </BaseArrayInput>
      {enableClearItem && inputValues?.length > 0 && (
        <Button
          fullWidth
          label={translate('actions.arrayClear')}
          onClick={() => setValue(source, [], { shouldDirty: true })}
        >
          <CachedOutlined />
        </Button>
      )}
    </>
  );
};

It does the trick, however the rendering is not really clean when used with a helperText prop:

image

We should override directly the SimpleFormIterator instead. However, we have to copy/paste all the original logic of the vendor one, quite cumbersome.

Additional context

Note: As this action may be more dangereous as the other, we may make it not available by default with a enableClear prop.

@soullivaneuh soullivaneuh changed the title ArrayInput: Clear call to action SimpleFormIterator: Clear call to action Oct 21, 2022
@slax57
Copy link
Contributor

slax57 commented Oct 21, 2022

Seems like a great idea to me.
Any PR submission, or upvotes, are welcome!

@soullivaneuh soullivaneuh changed the title SimpleFormIterator: Clear call to action SimpleFormIterator: Clear items call to action Oct 22, 2022
@JinParc
Copy link
Contributor

JinParc commented Oct 25, 2022

Related PR is created :-)

@fzaninotto
Copy link
Member

Fixed by #8302

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants