0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

React 版 Fluent UI ライブラリの DetailsList で全選択チェックボックスを非表示にする

Posted at

やりたいこと

React 版 FluentUI には DetailsList というデータテーブルのコントロールがあります。
このコントロールでは各行に選択用のチェックボックスを表示することができ、チェックボックスの表示をオンにすると便利機能としてヘッダーにも「すべて選択/解除」用のチェックボックスが表示されます。
image.png

今回は各行のチェックボックスは表示したまま、ヘッダーの「すべて選択/解除」チェックボックスのみを非表示にする必要がありました。

課題

DetailsListのヘッダー部品としてDetailsHeaderというコントロールがあり、それに対してIDetailsHeaderPropsで各種プロパティが定義されています。(ここではdetailsHeaderPropsという変数名)
image.png

IDetailsHeaderPropsの中にはcheckboxVisibilityというプロパティも存在し、これにはenumとしてalways/onHover/hiddenのいずれかが設定できるようになっています。
image.png

ここでalways(常に表示)とonHover(ホバー時に表示)を設定した場合は正常にチェックボックスの表示が機能するのですが、hidden(非表示)を設定しても非表示にはならず、onHoverと同じ挙動になってしまいます。
とりあえずissueは出したのですが、要素の非表示だけで実現できそうなのでCSSで対応してみます。

CSSでやってみる

CustomDetailsList.tsx
import { Component } from 'react';
import { DetailsList, IColumn, IDetailsListStyles, IDetailsHeaderProps, DetailsHeader } from '@fluentui/react';

const items = Array.from({ length: 10 }).map((item, index) => ({
    key: index.toString(),
    col1: `hoge${index}`,
    col2: `fuga${index}`,
}));


const columns: IColumn[] = [
    { key: 'col1', name: '列1', fieldName: 'col1', minWidth: 100 },
    { key: 'col2', name: '列2', fieldName: 'col2', minWidth: 100 },
];

const gridStyles: Partial<IDetailsListStyles> = {
    headerWrapper: {
         selectors: {
            '.ms-DetailsHeader-cellIsCheck':{
                visibility: 'hidden'
            }
    }
};

export class CustomDetailsListextends Component {
    render() {
      return (
        <DetailsList
          items={items}
          columns={columns}
          styles={gridStyles}
        />
      );
    }
  }

ポイントとしてはDetailsListのstylesプロパティに設定するgridStylesselectorsで、DevToolsで確認した「すべて選択/解除」チェックボックスを指す.ms-DetailsHeader-cellIsCheckクラスを指定してvisibility: 'hidden'を設定しているところです。
image.png

これで各行のチェックボックスは表示したまま「すべて選択/解除」チェックボックスのみを非表示にすることができました。

【改修前】
Animation19.gif

【改修後】
Animation18.gif

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?