Code Monkey home page Code Monkey logo

Comments (6)

mariusrak avatar mariusrak commented on June 18, 2024 1

Yeah, right, I totally forgot about this option :D
Thanks :)

from uniforms.

kestarumper avatar kestarumper commented on June 18, 2024

Hi @mariusrak,
We're currently working on uniforms v4.0, and it's not likely that we will extend the onChange functionality in v3. I'll bring up that topic in our weekly meeting, and we'll consider this in v4.0 or v4.x.

If I understand correctly, you must run the callback at the Field level. It's true that we can't pass a callback to the onChange directly. However, you can access the whole model by using useForm that returns the uniforms context. Then, you can use useEffect to run the callback with the whole model. Use the dependency array to trigger the effect when only certain fields on the model change.

function WholeModelField({ callback }) {
  const form = useForm();
  useEffect(() => {
    callback(form.model);
  // assuming there is a field named `files` at the root
  }, [form.model.files]);
  return null;
}

To make it even simpler, you can use the second argument of useField, which also provides the uniforms form context.

function FilesField(props) {
  const [field, form] = useField("files", props);
  useEffect(() => {
    props.callback(form.model);
  }, [form.model.files]);
  return <input type="file" />;
}

Another workaround at the form level if that also suits you, is by using the onChange prop on the form directly or the onChangeModel.

<AutoForm
      schema={schema}
      onChange={(key, value) => console.log("onChange", key, value)}
      onChangeModel={(model) => console.log("onChangeModel", model)}
  />

Edit restless-tree-6zwdr5

from uniforms.

mariusrak avatar mariusrak commented on June 18, 2024

Unfortunately my files field is class component, not function component and therefore I cannot use hooks. I'd have to implement it in whole form and that would mean lower reusability of files field component.

from uniforms.

kestarumper avatar kestarumper commented on June 18, 2024

You can always use the HOC approach.

import React, { useEffect } from "react";
import { AutoFields, AutoForm } from "uniforms-mui";
import {
  connectField,
  FieldProps,
  filterDOMProps,
  GuaranteedProps,
  HTMLFieldProps,
  UnknownObject,
  useForm,
} from "uniforms";
import { bridge as schema } from "./schema/all-fields-schema";

class _FileField extends React.Component<
  HTMLFieldProps<string, HTMLInputElement> & { model: UnknownObject }
> {
  componentDidUpdate(): void {
    console.log("componentDidUpdate", this.props.model);
  }
  render() {
    return (
      <input
        onChange={(event) => this.props.onChange(event.target.files?.[0].name)}
        multiple={false}
        type="file"
      />
    );
  }
}

function withModelHoc<P extends UnknownObject>(
  Component: React.ComponentType<P>
) {
  return (props: Omit<P, "model">) => {
    const form = useForm();
    return <Component {...(props as P)} model={form.model} />;
  };
}

const FileField = withModelHoc(connectField(_FileField, { kind: "leaf" }));

export function App() {
  return (
    <AutoForm placeholder schema={schema}>
      <FileField name="text" />
    </AutoForm>
  );
}

from uniforms.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.