How to use react-final-form - 10 common examples

To help you get started, we’ve selected a few react-final-form examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ewels / MegaQC / src / admin / components / resourceLink.js View on Github external
export default function ResourceLink({ reference, source, dest, children }) {
  // const form = useForm();
  // const localValue = form.getFieldState(source);
  const localValue = useField(source);
  const query = encodeURIComponent(
    JSON.stringify({
      [dest]: localValue.input.value
    })
  );
  const title = titleize(reference);
  return (
    <button label="{`Add">
      {children}</button>
github final-form / react-final-form / examples / chakra / index.js View on Github external
const CheckboxArrayControl = ({ name, value, children }) =&gt; {
  const {
    input: { checked, ...input },
    meta: { error, touched }
  } = useField(name, {
    type: 'checkbox', // important for RFF to manage the checked prop
    value // important for RFF to manage list of strings
  })
  return (
    
      {children}
    
  )
}
github urbit / bridge / src / indigo-react / components / ToggleInput.js View on Github external
// visuals
  name,
  label,
  inverseLabel,
  inverseColor = 'black',
  className,

  //
  disabled = false,

  ...rest
}) {
  const {
    input,
    meta: { submitting, submitSucceeded },
  } = useField(name, { type: 'checkbox' });

  disabled = disabled || submitting || submitSucceeded;

  return (
    
      {/* we totally hide the checkbox itself */}
github marmelab / react-admin / packages / ra-core / src / form / useInput.ts View on Github external
id,
    name,
    source,
    validate,
    onBlur: customOnBlur,
    onChange: customOnChange,
    onFocus: customOnFocus,
    ...options
}: InputProps): ComputedInputProps => {
    const finalName = name || source;

    const sanitizedValidate = Array.isArray(validate)
        ? composeValidators(validate)
        : validate;

    const { input, meta } = useFinalFormField(finalName, {
        initialValue: defaultValue,
        validate: sanitizedValidate,
        ...options,
    });

    // Extract the event handlers so that we can provide ours
    // allowing users to provide theirs without breaking the form
    const { onBlur, onChange, onFocus, ...inputProps } = input;

    const handleBlur = useCallback(
        event => {
            onBlur(event);

            if (typeof customOnBlur === 'function') {
                customOnBlur(event);
            }
github urbit / bridge / src / indigo-react / components / SelectInput.js View on Github external
export default function SelectInput({
  name,
  label,
  placeholder,
  className,
  mono = false,
  options = [],
  disabled = false,
  warning,
}) {
  const {
    input,
    meta: { active, error, submitting, submitSucceeded, touched, valid },
  } = useField(name, {
    type: 'select',
  });

  disabled = disabled || submitting || submitSucceeded;

  const [isOpen, setIsOpen] = useState(false);
  const ref = useRef();

  // close select on outside clicks
  useOnClickOutside(ref, useCallback(() => setIsOpen(false), [setIsOpen]));

  const toggleOpen = useCallback(() => {
    input.onFocus();
    setIsOpen(isOpen => !isOpen);
  }, [input]);
github final-form / react-final-form-arrays / src / useFieldArray.js View on Github external
const useFieldArray = (
  name: string,
  {
    subscription = all,
    defaultValue,
    initialValue,
    isEqual = defaultIsEqual,
    validate: validateProp
  }: UseFieldArrayConfig = {}
): FieldArrayRenderProps =&gt; {
  const form = useForm('useFieldArray')

  const formMutators: Mutators = form.mutators
  const hasMutators = !!(formMutators &amp;&amp; formMutators.push &amp;&amp; formMutators.pop)
  if (!hasMutators) {
    throw new Error(
      'Array mutators not found. You need to provide the mutators from final-form-arrays to your form'
    )
  }
  const mutators = useConstant(() =&gt;
    // curry the field name onto all mutator calls
    Object.keys(formMutators).reduce((result, key) =&gt; {
      result[key] = (...args) =&gt; formMutators[key](name, ...args)
      return result
    }, {})
  )
github marmelab / react-admin / packages / ra-core / src / form / useInitializeFormWithRecord.ts View on Github external
const useInitializeFormWithRecord = record => {
    const form = useForm();

    useEffect(() => {
        if (!record) {
            return;
        }

        const registeredFields = form.getRegisteredFields();

        // react-final-form does not provide a way to set multiple values in one call.
        // Using batch ensure we don't get rerenders until all our values are set
        form.batch(() => {
            Object.keys(record).forEach(key => {
                // We have to check the record key is actually registered as a field
                // as some record keys may not have a matching input
                if (registeredFields.some(field => field === key)) {
                    form.change(key, record[key]);
github urbit / bridge / src / form / SubmitButton.js View on Github external
as: As = ForwardButton,
  className,
  children,
  handleSubmit,
  ...rest
}) {
  const {
    valid,
    validating,
    submitting,
    hasValidationErrors,
    hasSubmitErrors,
    dirtySinceLastSubmit,
    submitErrors,
    submitSucceeded,
  } = useFormState();

  const onlyWarningInSubmitErrors = onlyHasWarning(submitErrors);

  // can submit if:
  // 1) is valid
  //      OR has submit errors
  //      AND
  //        a) is dirty
  //        b) OR only has a warning (double conf)
  // 2) AND is not validating
  // 3) AND has no validation errors
  // 4) AND is not actively submitting
  // 5) AND submit has not yet succeeded
  const canSubmit =
    (valid ||
      (hasSubmitErrors &&
github marmelab / react-admin / packages / ra-ui-materialui / src / input / RadioButtonGroupInputItem.tsx View on Github external
choice,
    optionText,
    optionValue,
    source,
    translateChoice,
}) =&gt; {
    const { getChoiceText, getChoiceValue } = useChoices({
        optionText,
        optionValue,
        translateChoice,
    });
    const label = getChoiceText(choice);
    const value = getChoiceValue(choice);
    const {
        input: { type, onChange, ...inputProps },
    } = useField(source, {
        type: 'radio',
        value,
    });

    const nodeId = `${source}_${label}`;

    return (
         onChange(value)}
                    {...inputProps}
github urbit / bridge / src / form / UploadInput.js View on Github external
export default function UploadInput({ name, label, ...rest }) {
  const {
    input,
    meta: { validating, submitting, touched, active, error },
  } = useField(name, { type: 'text' });

  const handleUpload = useCallback(
    element => {
      input.onFocus();

      const file = element.files.item(0);
      const reader = new FileReader();

      reader.onload = e => {
        input.onChange({ target: { value: e.target.result } });
      };

      const failure = _ => {
        input.onBlur();
      };

react-final-form

🏁 High performance subscription-based form state management for React

MIT
Latest version published 3 years ago

Package Health Score

67 / 100
Full package analysis

Similar packages