Skip to content

Commit

Permalink
fix respect isEditable context in PostMeta component
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiankaegy committed Oct 11, 2024
1 parent 1ca549c commit e5a46ed
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions components/post-meta/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { RichText } from '@wordpress/block-editor';
import { __experimentalNumberControl as NumberControl, ToggleControl } from '@wordpress/components';
import type { ToggleControlProps } from '@wordpress/components/src/toggle-control/types';
import { usePostMetaValue, useIsSupportedMetaField } from '../../hooks';
import { usePostMetaValue, useIsSupportedMetaField, usePost } from '../../hooks';
import { toSentence } from './utilities';

interface MetaStringProps
extends Omit<React.ComponentPropsWithoutRef<typeof RichText>, 'value' | 'onChange'> {
extends Omit<
React.ComponentPropsWithoutRef<typeof RichText>,
'value' | 'onChange' | 'multiline'
> {
/**
* The meta key to use.
*/
Expand All @@ -15,6 +18,11 @@ interface MetaStringProps
const MetaString: React.FC<MetaStringProps> = (props) => {
const { metaKey, tagName = 'p' } = props;
const [metaValue, setMetaValue] = usePostMetaValue<string>(metaKey);
const { isEditable } = usePost();

if (!isEditable) {
return <RichText.Content value={metaValue ?? ''} tagName={tagName} {...props} />;
}

return (
<RichText
Expand All @@ -36,11 +44,13 @@ interface MetaNumberProps {
const MetaNumber: React.FC<MetaNumberProps> = (props) => {
const { metaKey } = props;
const [metaValue, setMetaValue] = usePostMetaValue<number>(metaKey);
const { isEditable } = usePost();

return (
<NumberControl
value={metaValue}
onChange={(value) => setMetaValue(parseInt(value ?? '', 10))}
disabled={!isEditable}
{...props}
/>
);
Expand All @@ -56,8 +66,16 @@ interface MetaBooleanProps extends Pick<ToggleControlProps, 'label'> {
const MetaBoolean: React.FC<MetaBooleanProps> = (props) => {
const { metaKey } = props;
const [metaValue, setMetaValue] = usePostMetaValue<boolean>(metaKey);
const { isEditable } = usePost();

return <ToggleControl checked={metaValue} onChange={setMetaValue} {...props} />;
return (
<ToggleControl
checked={metaValue}
onChange={setMetaValue}
disabled={!isEditable}
{...props}
/>
);
};

interface PostMetaProps {
Expand Down

0 comments on commit e5a46ed

Please sign in to comment.