Skip to content

Commit

Permalink
Add support for readOnly to disable Menu button (#63)
Browse files Browse the repository at this point in the history
* Add support for readOnly to disable Menu button

* fix: remove unnecessary async keyword from test

* fix: remove explicitly defined true boolean for readOnly prop to satisfy linter

---------

Co-authored-by: christopherafbjur <chrisdevcabc@gmail.com>
  • Loading branch information
luizhop and christopherafbjur authored Jan 17, 2024
1 parent a82203d commit 65995ee
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/components/IconPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ import type { SearchResultsOnSelectCallback } from './SearchResults';
import type { IconObject, IconPickerOptions } from '../types';
import type { ObjectInputProps } from 'sanity';

const IconPicker = ({ schemaType, value = {}, onChange }: ObjectInputProps) => {
const IconPicker = ({
schemaType,
value = {},
readOnly,
onChange,
}: ObjectInputProps) => {
const options: IconPickerOptions = schemaType.options;
const [isPopupOpen, setIsPopupOpen] = useState(false);
const { query, loading, results, setQuery } = useQuery(options);
Expand Down Expand Up @@ -88,7 +93,11 @@ const IconPicker = ({ schemaType, value = {}, onChange }: ObjectInputProps) => {
<IconContext.Provider
value={{ style: { width: ICON_WIDTH, height: ICON_HEIGHT } }}
>
<Menu onClick={handleMenuClick} selected={selected} />
<Menu
onClick={handleMenuClick}
selected={selected}
readOnly={readOnly}
/>

<Popup onClose={closePopup} isOpen={isPopupOpen}>
<SearchBar value={query} onChange={onQueryChange} />
Expand Down
7 changes: 7 additions & 0 deletions src/components/Menu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,11 @@ describe('Menu', () => {
await user.click(getByText('Delete'));
expect(mockOnClick).toHaveBeenCalledWith(Action.delete);
});

it('renders disabled state when readOnly is true', () => {
const { getByText } = render(
<Menu onClick={mockOnClick} selected={null} readOnly />
);
expect(getByText('Add icon').closest('button')).toBeDisabled();
});
});
4 changes: 4 additions & 0 deletions src/components/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ export type MenuClickCallback = (action: Action) => void | Error;
const Menu = ({
onClick,
selected,
readOnly,
}: {
onClick: MenuClickCallback;
selected: IconObject | null;
readOnly?: boolean | undefined;
}) => {
return (
<>
{!selected && (
<Button
disabled={readOnly}
icon={AddIcon}
fontSize={[1, 1, 2]}
mode="ghost"
Expand All @@ -35,6 +38,7 @@ const Menu = ({
<MenuButton
button={
<Button
disabled={readOnly}
mode="ghost"
padding={[2, 2, 3]}
tone="default"
Expand Down

0 comments on commit 65995ee

Please sign in to comment.