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

CreateTemplatePartModal: avoid identity warning in useSelect #67786

Merged
merged 1 commit into from
Dec 10, 2024
Merged
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
46 changes: 23 additions & 23 deletions packages/fields/src/components/create-template-part-modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ type CreateTemplatePartModalContentsProps = {
defaultTitle?: string;
};

type TemplatePartArea = {
area: string;
label: string;
icon: string;
description: string;
};

/**
* A React component that renders a modal for creating a template part. The modal displays a title and the contents for creating the template part.
* This component should not live in this package, it should be moved to a dedicated package responsible for managing template.
Expand Down Expand Up @@ -124,22 +131,14 @@ export function CreateTemplatePartModalContents( {
const [ isSubmitting, setIsSubmitting ] = useState( false );
const instanceId = useInstanceId( CreateTemplatePartModal );

const defaultTemplatePartAreas = useSelect( ( select ) => {
const areas =
const defaultTemplatePartAreas = useSelect(
( select ) =>
// @ts-expect-error getEntityRecord is not typed with unstableBase as argument.
select( coreStore ).getEntityRecord< {
default_template_part_areas: Array< {
area: string;
label: string;
icon: string;
description: string;
} >;
} >( 'root', '__unstableBase' )?.default_template_part_areas || [];

return areas.map( ( item ) => {
return { ...item, icon: getTemplatePartIcon( item.icon ) };
} );
}, [] );
default_template_part_areas: Array< TemplatePartArea >;
} >( 'root', '__unstableBase' )?.default_template_part_areas,
[]
);

async function createTemplatePart() {
if ( ! title || isSubmitting ) {
Expand Down Expand Up @@ -219,32 +218,33 @@ export function CreateTemplatePartModalContents( {
}
checked={ area }
>
{ defaultTemplatePartAreas.map(
( { icon, label, area: value, description } ) => (
{ ( defaultTemplatePartAreas ?? [] ).map( ( item ) => {
const icon = getTemplatePartIcon( item.icon );
return (
<Radio
__next40pxDefaultSize
key={ label }
value={ value }
key={ item.label }
value={ item.area }
className="fields-create-template-part-modal__area-radio"
>
<Flex align="start" justify="start">
<FlexItem>
<Icon icon={ icon } />
</FlexItem>
<FlexBlock className="fields-create-template-part-modal__option-label">
{ label }
<div>{ description }</div>
{ item.label }
<div>{ item.description }</div>
</FlexBlock>

<FlexItem className="fields-create-template-part-modal__checkbox">
{ area === value && (
{ area === item.area && (
<Icon icon={ check } />
) }
</FlexItem>
</Flex>
</Radio>
)
) }
);
} ) }
</RadioGroup>
</BaseControl>
<HStack justify="right">
Expand Down
Loading