Skip to content

Commit

Permalink
馃improve on getting value (#246)
Browse files Browse the repository at this point in the history
* improve on getting value

* remove extra function

* remove default value as empty string

* 馃 upgrade dev packages

* fix automation

* fix automation

* fix build issue

* fix build issue

* fix bundlesize

* more imporvement

* more imporvement

* more code clean up

* fix the issue introduced
  • Loading branch information
bluebill1049 committed Aug 23, 2019
1 parent 4e6c91d commit 2384e4e
Show file tree
Hide file tree
Showing 6 changed files with 643 additions and 681 deletions.
7 changes: 4 additions & 3 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "react-hook-form",
"version": "3.22.1",
"version": "3.22.2-beta.4",
"main": "dist/react-hook-form.js",
"module": "dist/react-hook-form.es.js",
"types": "dist/index.d.ts",
Expand All @@ -9,6 +9,7 @@
"prebuild": "yarn clean",
"build": "yarn build:modern && yarn build:ie11",
"build:modern": "rollup -c",
"build:min": "rollup -c rollup.min.config.js",
"build:ie11": "rollup -c rollup.ie11.config.js",
"watch": "tsc --watch",
"lint": "eslint ./src --ext .jsx,.ts --ignore-pattern *.test.ts --report-unused-disable-directives",
Expand All @@ -19,7 +20,7 @@
"testw": "yarn test:coverage -- --watchAll",
"cypress": "cypress run",
"cypress:open": "cypress open",
"bundlesize": "bundlesize",
"bundlesize": "yarn build:min && bundlesize",
"prepublishOnly": "yarn lint && yarn test && yarn run clean && yarn build",
"start:app": "yarn build && yarn link && yarn --cwd node_modules/react link && yarn --cwd ./app link react react-hook-form && yarn --cwd ./app && yarn --cwd ./app run start"
},
Expand Down Expand Up @@ -69,7 +70,7 @@
},
"bundlesize": [
{
"path": "./dist/react-hook-form.es.js",
"path": "./dist/react-hook-form.min.es.js",
"maxSize": "5 kB"
}
],
Expand Down
9 changes: 0 additions & 9 deletions rollup.config.js
@@ -1,5 +1,4 @@
import typescript from 'rollup-plugin-typescript2';
import { terser } from 'rollup-plugin-terser';
import pkg from './package.json';

export function getConfig({
Expand All @@ -23,14 +22,6 @@ export function getConfig({
tsconfig,
clean: true,
}),
terser({
warnings: true,
mangle: {
properties: {
regex: /^__/,
},
},
}),
],
output,
};
Expand Down
14 changes: 14 additions & 0 deletions rollup.min.config.js
@@ -0,0 +1,14 @@
import typescript from 'rollup-plugin-typescript2';
import { terser } from 'rollup-plugin-terser';

export default {
input: 'src/index.ts',
plugins: [typescript(), terser()],
external: ['react', 'react-dom'],
output: [
{
file: 'dist/react-hook-form.min.es.js',
format: 'es',
},
],
};
19 changes: 13 additions & 6 deletions src/logic/getDefaultValue.ts
@@ -1,8 +1,15 @@
import get from '../utils/get';
import { DataType } from '../types';
import { FieldValue } from '../types';

export default (
defaultValues: DataType | undefined,
name: string,
): DataType | string | number | boolean =>
defaultValues && (defaultValues[name] || get(defaultValues, name));
export default function getDefaultValue<
Data extends FieldValue,
Name extends keyof Data = keyof Data
>(
defaultValues: Partial<Data>,
name: Name,
defaultValue?: any,
): Data[Name] | undefined {
return (
defaultValues[name] || get(defaultValues, name as string, defaultValue)
);
}
21 changes: 9 additions & 12 deletions src/useForm.ts
Expand Up @@ -11,7 +11,6 @@ import attachNativeValidation from './logic/attachNativeValidation';
import getDefaultValue from './logic/getDefaultValue';
import assignWatchFields from './logic/assignWatchFields';
import omitValidFields from './logic/omitValidFields';
import get from './utils/get';
import isCheckBoxInput from './utils/isCheckBoxInput';
import isEmptyObject from './utils/isEmptyObject';
import isRadioInput from './utils/isRadioInput';
Expand Down Expand Up @@ -403,11 +402,9 @@ export default function useForm<
[name]: isEmptyObject(fieldsRef.current)
? !isUndefined(defaultValue) && !isString(defaultValue)
? defaultValue[name]
: defaultValues
? defaultValues[name] || get(defaultValues, name as string)
: undefined
: getDefaultValue(defaultValues, name)
: assignWatchFields(fieldValues, name as string, watchFields) ||
getDefaultValue(defaultValues, name as string),
getDefaultValue(defaultValues, name),
}),
{},
);
Expand Down Expand Up @@ -464,7 +461,7 @@ export default function useForm<
};
if (validate) fields[name]!.validate = validate;

(fields[name]!.options || []).push({
(fields[name].options || []).push({
...inputData,
mutationWatcher: onDomRemove(elementRef, () =>
removeEventListenerAndRef(inputData),
Expand All @@ -480,9 +477,9 @@ export default function useForm<
}
}

if (defaultValues) {
const defaultValue = defaultValues[name] || get(defaultValues, name);
if (defaultValue !== undefined) setFieldValue(name as Name, defaultValue);
if (!isEmptyObject(defaultValues)) {
const defaultValue = getDefaultValue(defaultValues, name);
if (!isUndefined(defaultValue)) setFieldValue(name as Name, defaultValue);
}

if (!fieldDefaultValues[name as Name])
Expand Down Expand Up @@ -610,7 +607,7 @@ export default function useForm<
return Promise.resolve(resolvedPrevious);
},
Promise.resolve<SubmitPromiseResult<Data>>({
errors: {} as ErrorMessages<Data>,
errors: {},
values: {} as Data,
}),
);
Expand Down Expand Up @@ -666,7 +663,7 @@ export default function useForm<

if (values) {
fieldsKeyValue.forEach(([key]) =>
setFieldValue(key as Name, get(values, key, '')),
setFieldValue(key as Name, getDefaultValue(values, key, '')),
);
}
reRenderForm({});
Expand All @@ -687,7 +684,7 @@ export default function useForm<
removeEventListenerAndRef(field, true),
);
},
[],
[removeEventListenerAndRef],
);

return {
Expand Down

0 comments on commit 2384e4e

Please sign in to comment.