mirror of
https://github.com/strapi/strapi.git
synced 2025-12-27 23:24:03 +00:00
refactor: refactor from another PR from main
This commit is contained in:
parent
8c83e9d508
commit
8f708964ea
@ -1,19 +0,0 @@
|
||||
import { getDisplayedValue } from '../useMainValue';
|
||||
|
||||
describe('getDisplayedValue', () => {
|
||||
it('returns the mainField value', () => {
|
||||
const modifiedData = {
|
||||
DeepComplex: [
|
||||
{
|
||||
Title: 'File',
|
||||
},
|
||||
],
|
||||
};
|
||||
const componentFieldPath = ['DeepComplex', 0];
|
||||
const mainField = 'Title';
|
||||
|
||||
const normalizedContent = getDisplayedValue(modifiedData, componentFieldPath, mainField);
|
||||
|
||||
expect(normalizedContent).toEqual('File');
|
||||
});
|
||||
});
|
||||
@ -1,21 +0,0 @@
|
||||
import { useMemo } from 'react';
|
||||
import get from 'lodash/get';
|
||||
import toString from 'lodash/toString';
|
||||
import { useCMEditViewDataManager } from '@strapi/helper-plugin';
|
||||
|
||||
export function getDisplayedValue(modifiedData, componentFieldPath, mainField) {
|
||||
return toString(get(modifiedData, [...componentFieldPath, mainField], ''));
|
||||
}
|
||||
|
||||
function useMainValue(schema, componentFieldPath) {
|
||||
const { modifiedData } = useCMEditViewDataManager();
|
||||
|
||||
const mainField = useMemo(() => get(schema, ['settings', 'mainField'], 'id'), [schema]);
|
||||
|
||||
const displayedValue =
|
||||
mainField === 'id' ? '' : getDisplayedValue(modifiedData, componentFieldPath, mainField);
|
||||
|
||||
return displayedValue.trim().length < 1 ? '' : ` - ${displayedValue}`;
|
||||
}
|
||||
|
||||
export default useMainValue;
|
||||
@ -1,11 +0,0 @@
|
||||
import React from 'react';
|
||||
|
||||
function connect(WrappedComponent, select) {
|
||||
return (props) => {
|
||||
const selectors = select(props);
|
||||
|
||||
return <WrappedComponent {...props} {...selectors} />;
|
||||
};
|
||||
}
|
||||
|
||||
export default connect;
|
||||
@ -1,2 +0,0 @@
|
||||
export { default as connect } from './connect';
|
||||
export { default as select } from './select';
|
||||
@ -1,19 +0,0 @@
|
||||
import { useMemo } from 'react';
|
||||
import useMainValue from '../hooks/useMainValue';
|
||||
import { useContentTypeLayout } from '../../../../../hooks';
|
||||
|
||||
function useSelect({ componentUid, name, index }) {
|
||||
const { getComponentLayout } = useContentTypeLayout();
|
||||
const componentLayoutData = useMemo(() => {
|
||||
const layout = getComponentLayout(componentUid);
|
||||
|
||||
return layout;
|
||||
}, [componentUid, getComponentLayout]);
|
||||
const mainValue = useMainValue(componentLayoutData, [name, index]);
|
||||
|
||||
return {
|
||||
mainValue,
|
||||
};
|
||||
}
|
||||
|
||||
export default useSelect;
|
||||
@ -17,6 +17,11 @@ jest.mock('../../../../hooks', () => ({
|
||||
}),
|
||||
}));
|
||||
|
||||
jest.mock('@strapi/helper-plugin', () => ({
|
||||
...jest.requireActual('@strapi/helper-plugin'),
|
||||
useCMEditViewDataManager: jest.fn().mockImplementation(() => ({ modifiedData: {} })),
|
||||
}));
|
||||
|
||||
/**
|
||||
* We _could_ unmock this and use it, but it requires more
|
||||
* harnessing then is necessary and it's not worth it for these
|
||||
|
||||
@ -15,6 +15,7 @@ const toggleNotification = jest.fn();
|
||||
|
||||
jest.mock('@strapi/helper-plugin', () => ({
|
||||
...jest.requireActual('@strapi/helper-plugin'),
|
||||
useCMEditViewDataManager: jest.fn().mockImplementation(() => ({ modifiedData: {} })),
|
||||
useNotification: jest.fn().mockImplementation(() => toggleNotification),
|
||||
NotAllowedInput: () => 'This field is not allowed',
|
||||
}));
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user