mirror of
https://github.com/strapi/strapi.git
synced 2025-09-27 01:09:49 +00:00
Merge pull request #12500 from godzzo/godzzo/issue12499
DynamicZone - extend FriendlyName with MainValue
This commit is contained in:
commit
e68bca1d56
@ -0,0 +1,19 @@
|
|||||||
|
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');
|
||||||
|
});
|
||||||
|
});
|
@ -0,0 +1,21 @@
|
|||||||
|
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;
|
@ -18,6 +18,7 @@ import { useContentTypeLayout } from '../../../../hooks';
|
|||||||
import { getTrad } from '../../../../utils';
|
import { getTrad } from '../../../../utils';
|
||||||
import FieldComponent from '../../../FieldComponent';
|
import FieldComponent from '../../../FieldComponent';
|
||||||
import Rectangle from './Rectangle';
|
import Rectangle from './Rectangle';
|
||||||
|
import { connect, select } from './utils';
|
||||||
|
|
||||||
const ActionStack = styled(Stack)`
|
const ActionStack = styled(Stack)`
|
||||||
svg {
|
svg {
|
||||||
@ -55,6 +56,8 @@ const Component = ({
|
|||||||
removeComponentFromDynamicZone,
|
removeComponentFromDynamicZone,
|
||||||
showDownIcon,
|
showDownIcon,
|
||||||
showUpIcon,
|
showUpIcon,
|
||||||
|
// Passed with the select function
|
||||||
|
mainValue,
|
||||||
}) => {
|
}) => {
|
||||||
const { formatMessage } = useIntl();
|
const { formatMessage } = useIntl();
|
||||||
const { getComponentLayout } = useContentTypeLayout();
|
const { getComponentLayout } = useContentTypeLayout();
|
||||||
@ -144,7 +147,7 @@ const Component = ({
|
|||||||
)}
|
)}
|
||||||
</ActionStack>
|
</ActionStack>
|
||||||
}
|
}
|
||||||
title={friendlyName}
|
title={`${friendlyName}${mainValue}`}
|
||||||
togglePosition="left"
|
togglePosition="left"
|
||||||
/>
|
/>
|
||||||
<AccordionContent>
|
<AccordionContent>
|
||||||
@ -186,6 +189,9 @@ Component.propTypes = {
|
|||||||
removeComponentFromDynamicZone: PropTypes.func.isRequired,
|
removeComponentFromDynamicZone: PropTypes.func.isRequired,
|
||||||
showDownIcon: PropTypes.bool.isRequired,
|
showDownIcon: PropTypes.bool.isRequired,
|
||||||
showUpIcon: PropTypes.bool.isRequired,
|
showUpIcon: PropTypes.bool.isRequired,
|
||||||
|
mainValue: PropTypes.string.isRequired,
|
||||||
};
|
};
|
||||||
|
|
||||||
export default memo(Component, isEqual);
|
const Memoized = memo(Component, isEqual);
|
||||||
|
|
||||||
|
export default connect(Memoized, select);
|
||||||
|
@ -0,0 +1,11 @@
|
|||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
function connect(WrappedComponent, select) {
|
||||||
|
return (props) => {
|
||||||
|
const selectors = select(props);
|
||||||
|
|
||||||
|
return <WrappedComponent {...props} {...selectors} />;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export default connect;
|
@ -0,0 +1,2 @@
|
|||||||
|
export { default as connect } from './connect';
|
||||||
|
export { default as select } from './select';
|
@ -0,0 +1,19 @@
|
|||||||
|
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;
|
Loading…
x
Reference in New Issue
Block a user