Git Product home page Git Product logo

Comments (4)

radekmie avatar radekmie commented on May 26, 2024

Hi @emmett08. It looks like you are creating a custom component. In that case, you probably want to wire it up with the form's model. Here's my first question - what is the checkboxes in your example? Or rather, how would you like to use it?

Second thing is the AutoField usage: are the field and with props specific application? These are not used in uniforms.

And lastly, remember that all fields rendered by uniforms have to be a part of the schema, and if you'd like to render some fields dynamically (e.g., make the number of fields depend on other fields), it has to be reflected in the schema too.

from uniforms.

emmett08 avatar emmett08 commented on May 26, 2024

Hi @radekmie - field was a github copilot addition which I didn't check. with is what I'd like to have to pass an array to the component but not needed since I haven't wired the component to the forms model (I thought that was the function of connectField e.g. connectField<CheckboxListProps, unknown>(CheckboxList);).

How do I pass in the choices array to the component when registering it in an AutoForm please?

const choices = [
  {key: 'aws', label: 'AWS', checked: false, onChange: (event: React.ChangeEvent<HTMLInputElement>) => {console.log(event.target.checked)}},
  {key: 'gcp', label: 'GCP', checked: false, onChange: (event: React.ChangeEvent<HTMLInputElement>) => {console.log(event.target.checked)}}
];

// custom uniforms component
// <CheckboxList checkboxes={choices} />

This is where I use the component and connectField:

export type VaultProps = HTMLFieldProps<string, HTMLDivElement> &
  CheckboxListProps;

export const sourceApplicationLocations: CheckboxListProps = {
  checkboxes: [
    {
      key: "aws",
      label: "AWS",
      checked: false,
      onChange: (event: React.ChangeEvent<HTMLInputElement>) => {
        console.log(event.target.checked);
      },
    },
    {
      key: "gcp",
      label: "GCP",
      checked: false,
      onChange: (event: React.ChangeEvent<HTMLInputElement>) => {
        console.log(event.target.checked);
      },
    },
  ],
};

const XOptions = connectField<CheckboxListProps, unknown>(CheckboxList);
export default XOptions;

from uniforms.

emmett08 avatar emmett08 commented on May 26, 2024

In case this helps anyone else...

import { Autocomplete, Box, Paper, TextField } from "@mui/material";
import { connectField, HTMLFieldProps } from "uniforms";

export type EnvironmentProps = HTMLFieldProps<string, HTMLDivElement> & {
  environments: string[];
};

function Environment({ value, label, environments }:EnvironmentProps) {

  const CustomPaper = (props: any) => {
    return <Paper elevation={8} {...props} />;
  };
  
  return (
    <Box sx={{ mt: 1, mb: 1 }}>
      <Autocomplete
        multiple
        id="selected-environments-outlined"
        // onChange={onSelectedValuesChange}
        options={environments.map((option) => option)}
        getOptionLabel={(option) => option} 
        isOptionEqualToValue={(a, b) => a === b}
        PaperComponent={CustomPaper}
        filterSelectedOptions
        renderInput={(params) => (
          <TextField
            {...params}
            variant="outlined"
            label={label} // TODO: add props. This has no context
            placeholder={value} // TODO: add props. This has no context
            // onChange={handleOnChange}
          />
        )}
      />
    </Box>
  );
}

export default connectField(Environment);


Usage:

export type EnvironmentsData = {
    globalPlatform: string[];
};

const schema: JSONSchemaType<EnvironmentsData> = {
    title: 'Environments Required',
    type: 'object',
    properties: {
        globalPlatform: {
            type: 'array',
            minItems: 0,
            items: {
              type: 'string'
            },
            uniforms: { component: EnvironmentField, value: "Desired Platforms", label: 'Global', environments: ['Dev', 'Stage', 'Prod', 'Mgmt'] }
        },
    },
    required: [],
};

from uniforms.

radekmie avatar radekmie commented on May 26, 2024

I'm glad you figured it out! Just to make it clear: what you proposed in this comment could work, but is not in-align with how uniforms are meant to be used. The onChange should not be passed to any field, as a field is "just" a part of the form, not a standalone input.

And as for this code - you could simplify it a little. The Environment components has no need to be wrapped in connectField and it'd be enough to forward all of the props from it to the TextField inside. If you'd need to read a value or other prop, you can always use useField to do that.

I'll close this one, but feel free to comment further.

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.