Skip to content
This repository has been archived by the owner on Jul 27, 2022. It is now read-only.

Commit

Permalink
Merge 6551715 into 747d99f
Browse files Browse the repository at this point in the history
  • Loading branch information
wellyshen committed Nov 13, 2020
2 parents 747d99f + 6551715 commit 9130c5a
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/fair-monkeys-arrive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-cool-form": patch
---

feat: expose `get()` and `set()` utility functions
4 changes: 2 additions & 2 deletions examples/src/BasicForm/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** @jsxImportSource @emotion/core */

import React, { useState, useEffect } from "react";
import { useForm } from "react-cool-form";
import { useForm, get, set } from "react-cool-form";
import * as Yup from "yup";

import Input from "./Input";
Expand Down Expand Up @@ -76,7 +76,7 @@ export default (): JSX.Element => {
// validateOnChange: false,
// validateOnBlur: false,
// ignoreFields: ["text.nest", "number"],
// validate: async (values, set) => {
// validate: async (values) => {
// let errors: any = { text: { nest: "" } };

// // fib(35);
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// eslint-disable-next-line import/prefer-default-export
export { default as useForm } from "./useForm";
export { get, set } from "./utils";
6 changes: 1 addition & 5 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,8 @@ export interface Debug<V> {
(formState: FormState<V>): void;
}

export interface Set {
(object: any, path: string, value?: unknown, immutable?: boolean): any;
}

interface FormValidator<V> {
(values: V, set: Set): Errors<V> | void | Promise<Errors<V> | void>;
(values: V): Errors<V> | void | Promise<Errors<V> | void>;
}

export interface FieldValidator<V> {
Expand Down
15 changes: 10 additions & 5 deletions src/types/react-cool-form.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,8 @@ declare module "react-cool-form" {
(formState: FormState<V>): void;
}

interface Set {
(object: any, path: string, value?: unknown, immutable?: boolean): any;
}

export interface FormValidator<V = FormValues> {
(values: V, set: Set): Errors<V> | void | Promise<Errors<V> | void>;
(values: V): Errors<V> | void | Promise<Errors<V> | void>;
}

export interface FieldValidator<V = FormValues> {
Expand Down Expand Up @@ -190,4 +186,13 @@ declare module "react-cool-form" {
export const useForm: <V extends FormValues = FormValues>(
config: Config<V>
) => Return<V>;

export const get: (object: any, path: string, defaultValue?: unknown) => any;

export const set: (
object: any,
path: string,
value: unknown,
immutable?: boolean
) => any;
}
5 changes: 1 addition & 4 deletions src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,7 @@ export default <V extends FormValues = FormValues>({
if (!formValidatorRef.current) return name ? undefined : {};

try {
const errors = await formValidatorRef.current(
stateRef.current.values,
set
);
const errors = await formValidatorRef.current(stateRef.current.values);

if (name) return get(errors, name);

Expand Down
11 changes: 8 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/ban-types, @typescript-eslint/explicit-module-boundary-types */

import { FieldElement, Set } from "./types";
import { FieldElement } from "./types";

export const warn = (...args: any[]): void => {
if (__DEV__) console.warn(...args);
Expand Down Expand Up @@ -81,7 +81,12 @@ const cloneObject = (object: unknown): any => {
throw new Error("Unable to clone object.");
};

export const set: Set = (object, path, value, immutable = false) => {
export const set = (
object: any,
path: string,
value: unknown,
immutable = false
) => {
if (!isPlainObject(object)) throw new TypeError("Expected an object.");

const segs = String(path)
Expand All @@ -99,7 +104,7 @@ export const set: Set = (object, path, value, immutable = false) => {
return newObject;
};

export const unset = (object: any, path: string, immutable = false): any => {
export const unset = (object: any, path: string, immutable = false) => {
if (!isPlainObject(object)) throw new TypeError("Expected an object.");

const refObject = immutable ? cloneObject(object) : object;
Expand Down

0 comments on commit 9130c5a

Please sign in to comment.