ViewAttributeSelectionブロック
ViewAttributeSelection は、MUI 上に構築されたキーと値の表示ブロックで、ヘッダー(タイトルと任意のアクションボタン)と任意のサブヘッダーを備えています。
インストール
- npm
- yarn
- pnpm
- bun
npm install @nodeblocks/frontend-view-attribute-selection-block
yarn add @nodeblocks/frontend-view-attribute-selection-block
pnpm add @nodeblocks/frontend-view-attribute-selection-block
bun add @nodeblocks/frontend-view-attribute-selection-block
必要なもの
| 項目 | 用途 |
|---|---|
labels.headerTitle | ヘッダーのセクションタイトル(デフォルト 職種) |
data | 選択された属性。値のみの行には ReactNode[]、各行にラベルが必要な場合は Record<string, ReactNode> |
labels.headerActionButton + headerAction(任意) | アウトラインのヘッダーボタンのラベル(デフォルト 編集)とクリックハンドラー |
labels.subheaderTitle(任意) | ヘッダー下の補助行 |
layout(任意) | one-column はキー/値の行を縦に積み、two-column は横並びに表示(デフォルト) |
ViewAttributeSelection は選択状態を内部で保持しません — ページから labels、data、任意の headerAction を渡してください。行が値のみの場合(選択された職種のリストなど)は ReactNode[] を使用します。各属性カテゴリに独自のラベルが必要な場合(例: one-column レイアウトのスキル領域)はオブジェクトを使用します。ヘッダーの編集ボタンを表示するには labels.headerActionButton と headerAction を設定します。
コード例
- クイックスタート
- ラベル
- レイアウト
- 複合コンポーネント
- ブロックのオーバーライド
function Example() { const data = [ <Box key="roles" sx={{ display: 'flex', flexDirection: 'column', gap: 0.5 }}> <Typography variant="body2">フロントエンドエンジニア(未選択)</Typography> <Typography variant="body2">バックエンドエンジニア(5年)</Typography> <Typography variant="body2">データベースエンジニア(5年)</Typography> </Box>, ]; return <ViewAttributeSelection data={data} />; }
ヘッダーのコピー、編集アクション、サブヘッダー、行の内容をカスタマイズします。各ラベル付きカテゴリを縦に積む場合は layout="one-column" を使用します。
function Example() { const [lastAction, setLastAction] = React.useState(''); const labels = { headerTitle: '担当職種', subheaderTitle: '最終更新: 2026年3月12日', headerActionButton: '職種を編集', }; const data = { フロントエンド: 'React.js / Vue.js / JavaScript (ES6+) / HTML5 / Tailwind CSS', バックエンド: 'Node.js / Flask / PHP', フルスタック: '-', データベース: 'MongoDB / MySQL / DynamoDB / Firebase', モバイル: '-', テスト: '-', DevOps: '-', セキュリティ: '-', }; return ( <Box> <ViewAttributeSelection labels={labels} data={data} layout="one-column" headerAction={() => setLastAction('職種を編集')} /> {lastAction ? ( <Typography variant="caption" color="text.secondary" sx={{ display: 'block', mt: 1 }}> 最後のアクション: {lastAction} </Typography> ) : null} </Box> ); }
headerTitle、subheaderTitle、headerActionButton はルートの labels オブジェクト(または複合レイアウトでは ViewAttributeSelection.Header / ViewAttributeSelection.HeaderActionButton)に設定します。labels を省略すると、組み込みのデフォルト 職種 と 編集 が使われます。オブジェクト形式の data を使用する場合、行ラベルは data オブジェクトのキーから取得されます。
ラベル付き属性行にさまざまなレイアウトを試します。
function Example() { const [layout, setLayout] = React.useState('two-column'); const labels = { headerTitle: 'スキルプロフィール', headerActionButton: '編集', }; const data = { フロントエンド: 'React · TypeScript · Tailwind CSS', バックエンド: 'Node.js · PostgreSQL', DevOps: 'Docker · GitHub Actions', モバイル: 'React Native', }; return ( <Box> <Box sx={{ display: 'flex', gap: 1, mb: 2 }}> <Box component="button" type="button" onClick={() => setLayout('two-column')} sx={{ px: 1.5, py: 0.75, borderRadius: 1, border: '1px solid', borderColor: layout === 'two-column' ? 'primary.main' : 'divider', bgcolor: layout === 'two-column' ? 'primary.main' : 'background.paper', color: layout === 'two-column' ? 'primary.contrastText' : 'text.primary', cursor: 'pointer', fontSize: 14, fontWeight: layout === 'two-column' ? 600 : 400, }} > 2列 </Box> <Box component="button" type="button" onClick={() => setLayout('one-column')} sx={{ px: 1.5, py: 0.75, borderRadius: 1, border: '1px solid', borderColor: layout === 'one-column' ? 'primary.main' : 'divider', bgcolor: layout === 'one-column' ? 'primary.main' : 'background.paper', color: layout === 'one-column' ? 'primary.contrastText' : 'text.primary', cursor: 'pointer', fontSize: 14, fontWeight: layout === 'one-column' ? 600 : 400, }} > 1列 </Box> </Box> <ViewAttributeSelection labels={labels} data={data} layout={layout} /> </Box> ); }
two-column(デフォルト)は各フィールドラベルと値を横並びに表示します。one-column はラベルを値の上に積み、余白を広げます — 長いスキルリストや狭いビューポートに適しています。
ViewAttributeSelection.Header と ViewAttributeSelection.ItemList を直接構成します。ルートだけでなく子コンポーネントに labels、data、headerAction を渡します。
function Example() { const [lastAction, setLastAction] = React.useState(''); const data = [ <Box key="roles" sx={{ display: 'flex', flexDirection: 'column', gap: 0.5 }}> <Typography variant="body2">プロダクトデザイナー(3年)</Typography> <Typography variant="body2">UXリサーチャー(2年)</Typography> <Typography variant="body2">デザインシステム(1年)</Typography> </Box>, ]; return ( <Box> <ViewAttributeSelection layout="two-column"> <ViewAttributeSelection.Header labels={{ headerTitle: '登録済みの職種', headerActionButton: '職種を編集', }} headerAction={() => setLastAction('職種を編集')} sx={{ px: 2, py: 1.5, bgcolor: 'grey.50', borderRadius: 2, border: '1px solid', borderColor: 'divider', }} /> <ViewAttributeSelection.ItemList data={data} sx={{ border: '1px solid', borderColor: 'divider', borderRadius: 2, overflow: 'hidden', '& .MuiListItem-root': { bgcolor: 'background.paper' }, }} /> </ViewAttributeSelection> {lastAction ? ( <Typography variant="caption" color="text.secondary" sx={{ display: 'block', mt: 1 }}> 最後のアクション: {lastAction} </Typography> ) : null} </Box> ); }
defaultBlocks と defaultBlockOrder を使う関数の children で、ブロックの注入、デフォルトの差し替え、順序の制御を行います。
function Example() { const [noticeVisible, setNoticeVisible] = React.useState(true); const labels = { headerTitle: '担当職種', subheaderTitle: 'このページでは選択内容は読み取り専用です', }; const data = [ <Box key="roles" sx={{ display: 'flex', flexDirection: 'column', gap: 0.5 }}> <Typography variant="body2">フロントエンドエンジニア(未選択)</Typography> <Typography variant="body2">バックエンドエンジニア(5年)</Typography> <Typography variant="body2">データベースエンジニア(5年)</Typography> </Box>, ]; return ( <ViewAttributeSelection labels={labels} data={data}> {({ defaultBlocks, defaultBlockOrder }) => ({ blocks: { ...defaultBlocks, selectionNotice: noticeVisible ? ( <Box sx={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 2, px: 2, py: 1.5, mb: 1, borderRadius: 2, bgcolor: 'info.main', color: 'info.contrastText', }} > <Typography variant="body2" sx={{ fontWeight: 600 }}> デモ: 属性リストの上に表示するカスタムブロックです </Typography> <Box component="button" type="button" onClick={() => setNoticeVisible(false)} aria-label="通知を閉じる" sx={{ flexShrink: 0, border: '1px solid rgba(255,255,255,0.35)', bgcolor: 'rgba(255,255,255,0.12)', color: 'inherit', borderRadius: '50%', width: 28, height: 28, cursor: 'pointer', fontSize: 16, lineHeight: 1, }} > × </Box> </Box> ) : null, header: ( <ViewAttributeSelection.Header labels={labels} sx={{ px: 2, py: 2, borderRadius: 2, border: '1px solid', borderColor: 'divider', bgcolor: 'background.paper', }} /> ), itemList: ( <ViewAttributeSelection.ItemList data={data} sx={{ mt: 2, border: '1px solid', borderColor: 'divider', borderRadius: 2, '& .MuiListItem-root:nth-of-type(even)': { bgcolor: 'grey.50' }, }} /> ), }, blockOrder: [ ...(noticeVisible ? ['selectionNotice'] : []), 'header', 'subheader', 'itemList', ], })} </ViewAttributeSelection> ); }
ブロックのオーバーライドを使うタイミング
デフォルトの defaultBlockOrder は header、subheader、itemList、item、itemKey、itemValue です。デフォルトのレンダリングは data をマッピングするために itemList を使用します。個別の item / itemKey / itemValue エントリは、きめ細かいオーバーライド用です。この例では、閉じられる selectionNotice を先頭に追加し、header と itemList をスタイル付きバリアントに差し替え、blockOrder から未使用のブロックキーを省略しています。
主要プロパティ
コアプロパティ
| プロパティ | 型 | 必須 | デフォルト | 説明 |
|---|---|---|---|---|
data | Record<string, ReactNode> | ReactNode[] | いいえ | [] | 行の内容: オブジェクトのキーがフィールドラベルになり、配列は値のみの行をレンダリング |
labels | { headerTitle?: string; headerActionButton?: string; subheaderTitle?: string } | いいえ | {}(下記のブロックデフォルトとマージ) | ヘッダータイトル、任意のアクションボタンラベル、サブヘッダーのコピー |
コンテンツプロパティ
| プロパティ | 型 | 必須 | デフォルト | 説明 |
|---|---|---|---|---|
labels.headerTitle | string | いいえ | 職種 | ViewAttributeSelection.Header のメイン見出し |
labels.subheaderTitle | string | いいえ | undefined | ヘッダー下の補助行(ViewAttributeSelection.Subheader) |
labels.headerActionButton | string | いいえ | 編集 | アウトラインのヘッダーボタンのラベル。labels.headerActionButton または headerAction が設定されている場合に表示されます。 |
headerAction | () => void | いいえ | undefined | デフォルトのヘッダーアクションボタンがクリックされたときに呼び出されます。 |
layout | 'one-column' | 'two-column' | いいえ | 'two-column' | 行レイアウト: キー/値を縦に積むか、横並びの列にするか |
レイアウトと構成プロパティ
| プロパティ | 型 | 必須 | デフォルト | 説明 |
|---|---|---|---|---|
children | BlocksOverride | ReactNode | いいえ | undefined | 複合 JSX の子、または blocks と blockOrder を返す関数オーバーライド |
className | string | いいえ | undefined | ルートスタックのクラス名(nbb-view-attribute-selection) |
sx | SxProps | いいえ | undefined | ルートスタック用の MUI システムスタイル(デフォルトレンダリングには margin: 'auto' を含む) |
ViewAttributeSelection は StackProps(children を除く)を継承します。デフォルトの defaultBlockOrder: header、subheader、itemList、item、itemKey、itemValue。
サブコンポーネントのプロパティ
サブコンポーネントはコンテキストから読み取り、同じコンテンツキーをプロパティとして渡すとローカルで上書きできます。
| サブコンポーネント | 主要プロパティ | 継承 | ベース |
|---|---|---|---|
ViewAttributeSelection.Header | labels(headerTitle、headerActionButton)、headerAction、children、className、sx | StackProps | Stack + Typography + 任意の ViewAttributeSelection.HeaderActionButton |
ViewAttributeSelection.HeaderActionButton | labels(headerActionButton)、headerAction、children、className、onClick | ButtonProps | Button(variant="outlined"、size="medium") |
ViewAttributeSelection.Subheader | labels(subheaderTitle、headerTitle、headerActionButton)、children、className、sx | StackProps | Stack + Typography |
ViewAttributeSelection.ItemList | data、children、className、sx | ListProps | List(component="dl")+ ViewAttributeSelection.Item 行 |
ViewAttributeSelection.Item | layout、children、className、sx | ListItemProps | ListItem |
ViewAttributeSelection.Item.Key | layout、children、className、sx | TypographyProps | Typography(component="dt"、variant="subtitle1") |
ViewAttributeSelection.Item.Value | layout、children、className、sx | TypographyProps | Typography(component="dd"、variant="body2") |
デフォルト UI ブロック
| ブロック | ベース | 備考 |
|---|---|---|
ViewAttributeSelection(ルート) | Stack | spacing={3}、margin: 'auto' の縦レイアウト(nbb-view-attribute-selection) |
header(ViewAttributeSelection.Header) | Stack | labels.headerTitle、labels.headerActionButton、またはカスタム children が設定されている場合にレンダリング。headerAction または labels.headerActionButton が設定されている場合は ViewAttributeSelection.HeaderActionButton を含む |
subheader(ViewAttributeSelection.Subheader) | Stack | labels.subheaderTitle またはカスタム children が設定されている場合にレンダリング |
itemList(ViewAttributeSelection.ItemList) | List | data オブジェクトのエントリまたは配列の値をキー/値の行にマッピング |
デフォルトのルートレンダリング順序: header → subheader → itemList。
TypeScript
import * as React from 'react';
import { ViewAttributeSelection } from '@nodeblocks/frontend-view-attribute-selection-block';
import type { ReactNode } from 'react';
type AttributeSelectionLabels = {
headerTitle?: string;
headerActionButton?: string;
subheaderTitle?: string;
};
type AttributeSelectionData = Record<string, ReactNode> | ReactNode[];
export function AttributeSelectionDetailView() {
const labels: AttributeSelectionLabels = {
headerTitle: '担当職種',
headerActionButton: '職種を編集',
};
const data: AttributeSelectionData = [
<div key="roles">
<div>フロントエンドエンジニア(未選択)</div>
<div>バックエンドエンジニア(5年)</div>
</div>,
];
return (
<ViewAttributeSelection
labels={labels}
data={data}
headerAction={() => {
/* 遷移または編集ダイアログを開く */
}}
/>
);
}