Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix lodash imports #177

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions playwright/core/DynamicForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';

import {MobileProvider, ThemeProvider} from '@gravity-ui/uikit';

import _ from 'lodash';
import noop from 'lodash/noop';
import {Form} from 'react-final-form';

import {DynamicField} from '../../src/lib/core/components/Form/DynamicField';
Expand All @@ -15,7 +15,7 @@ export const DynamicForm = ({spec}: {spec: Spec}) => {
return (
<ThemeProvider>
<MobileProvider>
<Form initialValues={{}} onSubmit={_.noop}>
<Form initialValues={{}} onSubmit={noop}>
{() => (
<DynamicField
name="input"
Expand Down
4 changes: 2 additions & 2 deletions src/lib/core/components/Form/Controller/Controller.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';

import _ from 'lodash';
import omit from 'lodash/omit';

import {FormValue, Spec} from '../../../types';
import {useControllerMirror, useDynamicFormsCtx, useSearch} from '../hooks';
Expand Down Expand Up @@ -119,7 +119,7 @@ export const Controller = <
onItemRemove: methods.onItemRemove,
onDrop: methods.onDrop,
},
meta: {..._.omit(store.state, 'value'), submitFailed: store.tools.submitFailed},
meta: {...omit(store.state, 'value'), submitFailed: store.tools.submitFailed},
}),
[methods, store.name, store.state, store.tools.submitFailed],
);
Expand Down
77 changes: 44 additions & 33 deletions src/lib/core/components/Form/Controller/utils.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import React from 'react';

import _ from 'lodash';
import cloneDeep from 'lodash/cloneDeep';
import get from 'lodash/get';
import isArray from 'lodash/isArray';
import isBoolean from 'lodash/isBoolean';
import isEqual from 'lodash/isEqual';
import isFunction from 'lodash/isFunction';
import isNil from 'lodash/isNil';
import isString from 'lodash/isString';
import isUndefined from 'lodash/isUndefined';
import keys from 'lodash/keys';
import merge from 'lodash/merge';
import omit from 'lodash/omit';
import {isValidElementType} from 'react-is';

import {SpecTypes} from '../../../constants';
Expand Down Expand Up @@ -33,17 +44,17 @@ import {

const isErrorMutatorCorrect = (errorMutator: {value: ValidateError} | typeof EMPTY_MUTATOR) =>
errorMutator !== EMPTY_MUTATOR &&
(_.isString(errorMutator.value) ||
_.isBoolean(errorMutator.value) ||
_.isUndefined(errorMutator.value));
(isString(errorMutator.value) ||
isBoolean(errorMutator.value) ||
isUndefined(errorMutator.value));

const isValueMutatorCorrect = (
valueMutator: {value: FormValue} | typeof EMPTY_MUTATOR,
spec: Spec,
) =>
valueMutator !== EMPTY_MUTATOR &&
(typeof valueMutator.value === spec.type ||
(_.isArray(valueMutator.value) && spec.type === SpecTypes.Array) ||
(isArray(valueMutator.value) && spec.type === SpecTypes.Array) ||
valueMutator.value === undefined);

export const updateParentStore = <
Expand Down Expand Up @@ -78,10 +89,10 @@ export const getSpec = <SpecType extends Spec>({
spec,
mutatorsStore,
}: GetSpecParams<SpecType>): SpecType => {
const mutator = _.get(mutatorsStore.spec, name, EMPTY_MUTATOR);
const mutator = get(mutatorsStore.spec, name, EMPTY_MUTATOR);

if (mutator !== EMPTY_MUTATOR) {
const mutatedSpec = _.merge(_.cloneDeep(spec), mutator.value);
const mutatedSpec = merge(cloneDeep(spec), mutator.value);

if (isCorrectSpec(mutatedSpec)) {
return mutatedSpec;
Expand Down Expand Up @@ -114,7 +125,7 @@ export const getComponents = <
}
}

if (layouts && _.isString(spec.viewSpec.layout)) {
if (layouts && isString(spec.viewSpec.layout)) {
const Component = layouts[spec.viewSpec.layout];

if (isValidElementType(Component)) {
Expand All @@ -133,7 +144,7 @@ export const getRender = <DirtyValue extends FieldValue, SpecType extends Spec>(
Layout,
}: GetRenderParams<DirtyValue, SpecType>) => {
const render = (props: FieldRenderProps<DirtyValue>) => {
if (inputEntity && isCorrectSpec(spec) && _.isString(name)) {
if (inputEntity && isCorrectSpec(spec) && isString(name)) {
if (!spec.viewSpec.hidden) {
const {layoutProps, inputProps} = spec.viewSpec;
if (inputEntity.independent) {
Expand Down Expand Up @@ -191,13 +202,13 @@ export const getValidate = <

if (validators) {
if (
(!_.isString(spec.validator) || !spec.validator.length) &&
_.isFunction(validators.base)
(!isString(spec.validator) || !spec.validator.length) &&
isFunction(validators.base)
) {
validate = (value?: Value) => validators.base(spec, value);
}

if (_.isString(spec.validator) && _.isFunction(validators[spec.validator])) {
if (isString(spec.validator) && isFunction(validators[spec.validator])) {
validate = (value?: Value) => validators[spec.validator!]!(spec, value);
}
}
Expand All @@ -218,16 +229,16 @@ export const getFieldInitials = <
validate,
mutatorsStore,
}: GetFieldInitialsParams<DirtyValue, Value, SpecType>) => {
const valueMutator = transformArrIn(_.get(mutatorsStore.values, name, EMPTY_MUTATOR)) as
const valueMutator = transformArrIn(get(mutatorsStore.values, name, EMPTY_MUTATOR)) as
| {value: DirtyValue}
| typeof EMPTY_MUTATOR;
let value = _.cloneDeep(valueFromParent);
let value = cloneDeep(valueFromParent);

if (isValueMutatorCorrect(valueMutator, spec)) {
value = (valueMutator as {value: DirtyValue}).value;
}

if (_.isNil(value)) {
if (isNil(value)) {
if (spec.defaultValue) {
value = transformArrIn(spec.defaultValue) as DirtyValue;
}
Expand All @@ -242,7 +253,7 @@ export const getFieldInitials = <
}
}

let errorMutator: {value: BaseValidateError} | typeof EMPTY_MUTATOR = _.get(
let errorMutator: {value: BaseValidateError} | typeof EMPTY_MUTATOR = get(
mutatorsStore.errors,
name,
EMPTY_MUTATOR,
Expand All @@ -254,7 +265,7 @@ export const getFieldInitials = <

const error =
validate?.(transformArrOut(value)) || (errorMutator as {value: BaseValidateError})?.value;
const dirty = !_.isEqual(value, initialValue);
const dirty = !isEqual(value, initialValue);

return {
initialValue,
Expand Down Expand Up @@ -282,7 +293,7 @@ export const getFieldMethods = <
{valOrSetter, childErrors, errorMutator},
) => {
const {state, validate, spec} = store;
const _value = _.isFunction(valOrSetter) ? valOrSetter(state.value) : valOrSetter;
const _value = isFunction(valOrSetter) ? valOrSetter(state.value) : valOrSetter;
const error = validate?.(transformArrOut(_value)) || errorMutator;
let value = transformArrIn(_value);

Expand All @@ -293,15 +304,15 @@ export const getFieldMethods = <
let newChildErrors: Record<string, ValidateError> = {...state.childErrors};

if (childErrors) {
const nearestChildName = _.keys(childErrors).sort((a, b) => a.length - b.length)[0];
const nearestChildName = keys(childErrors).sort((a, b) => a.length - b.length)[0];

if (nearestChildName) {
const existingСhildNames = _.keys(newChildErrors).filter((childName) =>
const existingСhildNames = keys(newChildErrors).filter((childName) =>
childName.startsWith(nearestChildName),
);

newChildErrors = {
..._.omit(newChildErrors, existingСhildNames),
...omit(newChildErrors, existingСhildNames),
...childErrors,
};
}
Expand All @@ -311,7 +322,7 @@ export const getFieldMethods = <
...store,
state: {
...store.state,
dirty: !_.isEqual(value, state.initialValue),
dirty: !isEqual(value, state.initialValue),
error,
invalid: Boolean(error),
modified: true,
Expand Down Expand Up @@ -376,7 +387,7 @@ export const getFieldMethods = <
return onChange(store, {
valOrSetter: (currentValue) =>
currentValue
? (_.omit(
? (omit(
currentValue as FieldArrayValue | FieldObjectValue,
childName.split(`${name}.`)[1],
) as DirtyValue)
Expand Down Expand Up @@ -408,7 +419,7 @@ export const getFieldMethods = <
...store,
state: {
...store.state,
dirty: !_.isEqual(value, store.state.initialValue),
dirty: !isEqual(value, store.state.initialValue),
error,
invalid: Boolean(error),
modified: true,
Expand Down Expand Up @@ -467,7 +478,7 @@ export const initializeStore = <
valueFromParent,
validate,
mutatorsStore,
initialValue: _.get(tools.initialValue, name),
initialValue: get(tools.initialValue, name),
});

const initialsStore: ControllerStore<DirtyValue, Value, SpecType> = {
Expand All @@ -484,7 +495,7 @@ export const initializeStore = <
state,
};

if (!_.isEqual(valueFromParent, state.value) || state.error) {
if (!isEqual(valueFromParent, state.value) || state.error) {
initialsStore.afterStoreUpdateCB = () => updateParentStore(initialsStore);
}

Expand All @@ -508,17 +519,17 @@ export const updateStore = <
tools,
methodOnChange,
}: UpdateStoreParams<DirtyValue, Value, SpecType>) => {
const storeSpecMutator = _.get(store.mutatorsStore.spec, store.name, EMPTY_MUTATOR);
const storeValueMutator = _.get(store.mutatorsStore.values, store.name, EMPTY_MUTATOR) as
const storeSpecMutator = get(store.mutatorsStore.spec, store.name, EMPTY_MUTATOR);
const storeValueMutator = get(store.mutatorsStore.values, store.name, EMPTY_MUTATOR) as
| {value: DirtyValue}
| typeof EMPTY_MUTATOR;
const storeErrorMutator = _.get(store.mutatorsStore.errors, store.name, EMPTY_MUTATOR);
const storeErrorMutator = get(store.mutatorsStore.errors, store.name, EMPTY_MUTATOR);

const specMutator = _.get(mutatorsStore.spec, name, EMPTY_MUTATOR);
const valueMutator = _.get(mutatorsStore.values, name, EMPTY_MUTATOR) as
const specMutator = get(mutatorsStore.spec, name, EMPTY_MUTATOR);
const valueMutator = get(mutatorsStore.values, name, EMPTY_MUTATOR) as
| {value: DirtyValue}
| typeof EMPTY_MUTATOR;
const errorMutator = _.get(mutatorsStore.errors, name, EMPTY_MUTATOR);
const errorMutator = get(mutatorsStore.errors, name, EMPTY_MUTATOR);

const valueMutatorUpdated =
isValueMutatorCorrect(valueMutator, getSpec({name, spec: _spec, mutatorsStore})) &&
Expand All @@ -533,7 +544,7 @@ export const updateStore = <
tools.onChange !== store.tools.onChange ||
tools.onUnmount !== store.tools.onUnmount;
const updateAllStore =
!_.isEqual(_spec, store.initialSpec) ||
!isEqual(_spec, store.initialSpec) ||
config !== store.config ||
(specMutator !== EMPTY_MUTATOR && specMutator !== storeSpecMutator);
const updateAllStoreAndClearParentValues = name !== store.name;
Expand Down
10 changes: 6 additions & 4 deletions src/lib/core/components/Form/DynamicField.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React from 'react';

import _ from 'lodash';
import isFunction from 'lodash/isFunction';
import isString from 'lodash/isString';
import get from 'lodash/get';
import {isValidElementType} from 'react-is';
import type {MonacoEditorProps} from 'react-monaco-editor/lib/types';

Expand Down Expand Up @@ -68,13 +70,13 @@ export const DynamicField: React.FC<DynamicFieldProps> = ({
setField,
removeField,
isHiddenField,
searchFunction: _.isFunction(search) ? search : getDefaultSearchFunction(search),
searchFunction: isFunction(search) ? search : getDefaultSearchFunction(search),
}),
[isHiddenField, removeField, search, setField],
);

const correctParams = React.useMemo(
() => _.isString(name) && isCorrectSpec(spec) && isCorrectConfig(config),
() => isString(name) && isCorrectSpec(spec) && isCorrectConfig(config),
[name, spec, config],
);

Expand All @@ -96,7 +98,7 @@ export const DynamicField: React.FC<DynamicFieldProps> = ({
name={name}
parentOnChange={null}
parentOnUnmount={null}
value={_.get(store.values, name)}
value={get(store.values, name)}
/>
{watcher}
</SearchContext.Provider>
Expand Down
17 changes: 9 additions & 8 deletions src/lib/core/components/Form/__tests__/DynamicField.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import React from 'react';
import {ThemeProvider} from '@gravity-ui/uikit';
import {act, render} from '@testing-library/react';
import {FormApi} from 'final-form';
import _ from 'lodash';
import noop from 'lodash/noop';
import values from 'lodash/values';
import {Form, useForm} from 'react-final-form';

import {ErrorMessages, dynamicConfig} from '../../../../kit';
Expand Down Expand Up @@ -159,7 +160,7 @@ test('Form/hooks/DynamicField', () => {

render(
<ThemeProvider>
<Form initialValues={{}} onSubmit={_.noop}>
<Form initialValues={{}} onSubmit={noop}>
{() => {
const Caller = () => {
form = useForm();
Expand Down Expand Up @@ -208,7 +209,7 @@ test('Form/hooks/DynamicField', () => {
expect(form?.getState().values[name]).toMatchObject(value[name]);
expect(mirror.field.useStore?.store.errors).toMatchObject(errors);
expect(form?.getState().errors?.[name]).toBe(
_.values(mirror.field.useStore?.store.errors)
values(mirror.field.useStore?.store.errors)
.reverse()
.find((err) => Boolean(err)),
);
Expand Down Expand Up @@ -246,7 +247,7 @@ test('Form/hooks/DynamicField', () => {
expect(form?.getState().values[name]).toMatchObject(value1[name]);
expect(mirror.field.useStore?.store.errors).toMatchObject(errors1);
expect(form?.getState().errors?.[name]).toBe(
_.values(mirror.field.useStore?.store.errors)
values(mirror.field.useStore?.store.errors)
.reverse()
.find((err) => Boolean(err)),
);
Expand Down Expand Up @@ -278,7 +279,7 @@ test('Form/hooks/DynamicField', () => {
expect(form?.getState().values[name]).toMatchObject(value2[name]);
expect(mirror.field.useStore?.store.errors).toMatchObject(errors2);
expect(form?.getState().errors?.[name]).toBe(
_.values(mirror.field.useStore?.store.errors)
values(mirror.field.useStore?.store.errors)
.reverse()
.find((err) => Boolean(err)),
);
Expand All @@ -299,7 +300,7 @@ test('Form/hooks/DynamicField', () => {
expect(form?.getState().values[name]).toBe(value3[name]);
expect(mirror.field.useStore?.store.errors).toMatchObject(errors3);
expect(form?.getState().errors?.[name]).toBe(
_.values(mirror.field.useStore?.store.errors)
values(mirror.field.useStore?.store.errors)
.reverse()
.find((err) => Boolean(err)),
);
Expand Down Expand Up @@ -351,7 +352,7 @@ test('Form/hooks/DynamicField', () => {
expect(form?.getState().values[name]).toMatchObject(transformArrOut(value4)[name]);
expect(mirror.field.useStore?.store.errors).toMatchObject(errors4);
expect(form?.getState().errors?.[name]).toBe(
_.values(mirror.field.useStore?.store.errors)
values(mirror.field.useStore?.store.errors)
.reverse()
.find((err) => Boolean(err)),
);
Expand Down Expand Up @@ -400,7 +401,7 @@ test('Form/hooks/DynamicField', () => {
expect(form?.getState().values[name]).toMatchObject(transformArrOut(value5)[name]);
expect(mirror.field.useStore?.store.errors).toMatchObject(errors5);
expect(form?.getState().errors?.[name]).toBe(
_.values(mirror.field.useStore?.store.errors)
values(mirror.field.useStore?.store.errors)
.reverse()
.find((err) => Boolean(err)),
);
Expand Down
Loading
Loading