L
L
lexstile2022-01-14 13:11:03
React
lexstile, 2022-01-14 13:11:03

How to properly cross formik, vkui and file upload?

Components: File , Formik

What is the complexity:

  • Need to make a crutch with - setFieldValue and setTouched
  • I had to describe the processing of onInput directly in render, since setters are available only in it with the current approach.


61e14e7b022e7253774079.png

View code
const ProjectNewPage = ({ id }) => {
  const history = useHistory();

  const [logoUrl, setLogoUrl] = useState(null);

  const goBack = () => history.goBack();

  const onSubmit = (values, { setSubmitting }) => {
    console.log('values', values);
    setSubmitting(false);
  };

  return (
    <View id={id} activePanel="main">
      <Panel id="main">
        <PanelHeader left={<PanelHeaderBack onClick={goBack} />}>Новый проект</PanelHeader>
        <Group>
          <Formik
            validationSchema={ProjectNewSchema}
            initialValues={{ [FIELDS.LOGO]: null }}
            onSubmit={onSubmit}
          >
            {({ dirty, isValid, isSubmitting, setFieldValue, setTouched }) => (
              <Form>
                <Field name={FIELDS.LOGO}>
                  {({ meta }) => (
                    <SimpleCell disabled before={<Avatar size={64} src={logoUrl} />}>
                      <FormItem top="Логотип проекта" status={getFieldStatus(meta)} bottom={meta.touched && meta.error}>
                        <File
                          name={FIELDS.LOGO}
                          stretched
                          before={<Icon24Camera />}
                          controlSize="l"
                          mode="secondary"
                          accept="image/jpeg,image/png"
                          onInput={(event) => {
                            const file = get(event, 'target.files.0', '');
                            const fileSize = get(file, 'size', 0);

                            !meta.touched && setTouched({ [FIELDS.LOGO]: true });

                            if (fileSize > 0 && fileSize <= MAX_SIZE_IMAGE) {
                              const reader = new FileReader();

                              reader.onload = (e) => setLogoUrl(get(e, 'target.result', ''));
                              reader.readAsDataURL(file);

                              setFieldValue(FIELDS.LOGO, file);
                            }
                          }}
                        />
                      </FormItem>
                    </SimpleCell>
                  )}
                </Field>
                <FormItem>
                  <Button
                    stretched
                    type="submit"
                    size="l"
                    before={isSubmitting && <Spinner size="small" style={{ color: 'var(--white)' }} />}
                    disabled={!dirty || !isValid || isSubmitting}
                  >
                    Добавить
                  </Button>
                </FormItem>
              </Form>
            )}
          </Formik>
        </Group>
      </Panel>
    </View>
  );
};

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question