Giter Club home page Giter Club logo

Comments (5)

akshare avatar akshare commented on September 27, 2024 1

I created an example sandbox for how I understood the usage to be.

Basically, the key being, customizing Radio requires creating and managing your own Context (createContext, useContext).

Hopefully it helps someone.

https://codesandbox.io/s/snowy-monad-mewji5?file=/src/App.js

from informed.

joepuzzo avatar joepuzzo commented on September 27, 2024

Hi sorry just seeing this. I would suggest you look at the source code here https://github.com/teslamotors/informed/tree/master/src/components/form-fields

specifically

https://github.com/teslamotors/informed/blob/master/src/components/form-fields/Radio.js

and

https://github.com/teslamotors/informed/blob/master/src/components/form-fields/RadioGroup.js

from informed.

AmirL avatar AmirL commented on September 27, 2024

It works with useRadioGroup

const { radioGroupApi, radioGroupState } = useRadioGroup();

How can I use it? It's not exported.

from informed.

joepuzzo avatar joepuzzo commented on September 27, 2024

I wrote that myself just for my inputs. Its not doing anything fancy under hood. Just take a look at souse code and do same thing I did :)

from informed.

akshare avatar akshare commented on September 27, 2024
import React, {createContext, useContext} from "react";
import {Form, useField } from "informed";

const App = () => {
  return (
    <Form>
      <IRadioGroup name="radiogroup" label="Number Selection" initialValue="one">
        <IRadio value="one" label="One" id="one" />
        <IRadio value="two" label="Two" id="two" />
        <IRadio value="three" label="Three" id="three" />
      </IRadioGroup>
    </Form>
  );
}

export default App;

//create Context
const RadioGroupContext = createContext();

//custom Radio Group
const IRadioGroup = props => {
  const { fieldApi, fieldState, userProps } = useField(props);

  const groupContext = {
    radioGroupApi: fieldApi,
    radioGroupState: fieldState,
    ...props
  };

  const { label, id, options, children } = userProps;
  const { showError, error } = fieldState;

  return (
    <RadioGroupContext.Provider value={groupContext}>
      <fieldset aria-describedby={`${id}-error`}>
        {label ? <legend>{label}</legend> : null}
        {children}
        {showError ? (
          <small role="alert" id={`${id}-error`}>
            {error}
          </small>
        ) : null}
      </fieldset>
    </RadioGroupContext.Provider>
  );
}

//custom Radio
const IRadio = ({ label, value, ...props }) => {

  const { radioGroupApi, radioGroupState } = useContext(RadioGroupContext);

  const { setValue, setTouched } = radioGroupApi;

  const { value: groupValue, showError } = radioGroupState;

  return (
    <>
      <input
        {...props}
        aria-invalid={!!showError}
        value={value}
        checked={groupValue === value}
        onChange={e => {
          if (!e.target.checked) {
            return;
          }
          setValue(value, e);
        }}
        onBlur={e => {
          setTouched(true, e);
        }}
        type="radio"
        id={value}
      />
      <label htmlFor={value}>{label}</label>
    </>
  );
};

from informed.

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.