Skip to content

Commit

Permalink
use unseen icon
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Aug 6, 2024
1 parent 534d451 commit 87230af
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 65 deletions.
138 changes: 77 additions & 61 deletions packages/dataviews/src/components/dataviews-view-config/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import clsx from 'clsx';
import type { ChangeEvent } from 'react';

/**
* WordPress dependencies
Expand All @@ -19,10 +19,11 @@ import {
__experimentalHStack as HStack,
__experimentalHeading as Heading,
__experimentalText as Text,
privateApis as componentsPrivateApis,
} from '@wordpress/components';
import { __, _x } from '@wordpress/i18n';
import { memo, useContext, useState, useMemo } from '@wordpress/element';
import { cog, seen } from '@wordpress/icons';
import { cog, seen, unseen } from '@wordpress/icons';
import warning from '@wordpress/warning';

/**
Expand All @@ -32,54 +33,77 @@ import { SORTING_DIRECTIONS, sortLabelsShort } from '../../constants';
import { VIEW_LAYOUTS, getMandatoryFields } from '../../layouts';
import type { SupportedLayouts } from '../../types';
import DataViewsContext from '../dataviews-context';
import { unlock } from '../../lock-unlock';

interface ViewActionsProps {
const {
DropdownMenuV2: DropdownMenu,
DropdownMenuRadioItemV2: DropdownMenuRadioItem,
DropdownMenuItemLabelV2: DropdownMenuItemLabel,
} = unlock( componentsPrivateApis );

interface ViewTypeMenuProps {
defaultLayouts?: SupportedLayouts;
}

function LayoutPicker( {
function ViewTypeMenu( {
defaultLayouts = { list: {}, grid: {}, table: {} },
}: ViewActionsProps ) {
}: ViewTypeMenuProps ) {
const { view, onChangeView } = useContext( DataViewsContext );
const availableLayouts = Object.keys( defaultLayouts );
if ( availableLayouts.length <= 1 ) {
return null;
}
const activeView = VIEW_LAYOUTS.find( ( v ) => view.type === v.type );
return (
<ToggleGroupControl
__nextHasNoMarginBottom
__next40pxDefaultSize
isBlock
label={ __( 'Layout' ) }
value={ view.type }
onChange={ ( newLayout ) => {
switch ( newLayout ) {
case 'list':
case 'grid':
case 'table':
return onChangeView( {
...view,
type: newLayout,
...defaultLayouts[ newLayout ],
} );
}
warning( 'Invalid dataview' );
} }
<DropdownMenu
trigger={
<Button
size="compact"
icon={ activeView?.icon }
label={ __( 'Layout' ) }
/>
}
>
{ availableLayouts.map( ( layout ) => {
const config = VIEW_LAYOUTS.find( ( v ) => v.type === layout );
if ( ! config ) {
return null;
}
return (
<ToggleGroupControlOption
<DropdownMenuRadioItem
key={ layout }
value={ layout }
label={ config.label }
/>
name="view-actions-available-view"
checked={ layout === view.type }
hideOnClick
onChange={ ( e: ChangeEvent< HTMLInputElement > ) => {
switch ( e.target.value ) {
case 'list':
case 'grid':
case 'table':
return onChangeView( {
...view,
type: e.target.value,
...defaultLayouts[ e.target.value ],
} );
}
warning( 'Invalid dataview' );
} }
>
<DropdownMenuItemLabel>
{ config.label }
</DropdownMenuItemLabel>
</DropdownMenuRadioItem>
);
} ) }
</ToggleGroupControl>
</DropdownMenu>
);
}

interface ViewActionsProps {
defaultLayouts?: SupportedLayouts;
}

function SortFieldControl() {
const { view, fields, onChangeView } = useContext( DataViewsContext );
const orderOptions = useMemo( () => {
Expand Down Expand Up @@ -213,12 +237,7 @@ function FieldControl() {
<HStack expanded>
<span>{ field.label }</span>
<Button
className={ clsx(
'dataviews-view-config__field-control-button',
{
'is-hidden': ! isVisible,
}
) }
className="'dataviews-view-config__field-control-button"
size="compact"
onClick={ () =>
onChangeView( {
Expand All @@ -230,7 +249,7 @@ function FieldControl() {
: [ ...viewFields, field.id ],
} )
}
icon={ seen }
icon={ isVisible ? seen : unseen }
label={
isVisible
? __( 'Hide field' )
Expand Down Expand Up @@ -260,7 +279,6 @@ function SettingsSection( {
<Heading
level={ 2 }
className="dataviews-settings-section__title"
size={ 15 }
>
{ title }
</Heading>
Expand All @@ -282,16 +300,13 @@ function SettingsSection( {
);
}

function DataviewsViewConfigContent( {
defaultLayouts = { list: {}, grid: {}, table: {} },
}: ViewActionsProps ) {
function DataviewsViewConfigContent() {
return (
<VStack className="dataviews-view-config" spacing={ 6 }>
<SettingsSection
title={ __( 'Appearance' ) }
description={ __( 'Customize the display of data' ) }
>
<LayoutPicker defaultLayouts={ defaultLayouts } />
<HStack expanded className="is-divided-in-two">
<SortFieldControl />
<SortDirectionControl />
Expand All @@ -315,27 +330,28 @@ function _DataViewsViewConfig( {
useState< boolean >( false );

return (
<div>
<Button
size="compact"
icon={ cog }
label={ _x( 'View options', 'View is used as a noun' ) }
onClick={ () => setIsShowingViewPopover( true ) }
/>
{ isShowingViewPopover && (
<Popover
placement="bottom-end"
onClose={ () => {
setIsShowingViewPopover( false );
} }
focusOnMount
>
<DataviewsViewConfigContent
defaultLayouts={ defaultLayouts }
/>
</Popover>
) }
</div>
<>
<ViewTypeMenu defaultLayouts={ defaultLayouts } />
<div>
<Button
size="compact"
icon={ cog }
label={ _x( 'View options', 'View is used as a noun' ) }
onClick={ () => setIsShowingViewPopover( true ) }
/>
{ isShowingViewPopover && (
<Popover
placement="bottom-end"
onClose={ () => {
setIsShowingViewPopover( false );
} }
focusOnMount
>
<DataviewsViewConfigContent />
</Popover>
) }
</div>
</>
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@

.dataviews-settings-section__title.dataviews-settings-section__title {
line-height: $grid-unit-30;
}

.dataviews-view-config__field-control-button.is-hidden {
opacity: 0.3;
font-size: 15px;
}

.dataviews-settings-section__sidebar {
Expand Down

0 comments on commit 87230af

Please sign in to comment.