メインコンテンツまでスキップ

属性選択ブロック

AttributeSelection は、選択した { key, value } 属性を保存するための制御されたチェックボックス選択フォームです。

インストール

npm install @nodeblocks/frontend-attribute-selection-block

必要なもの

項目理由
data制御されたフォームデータ。デフォルトの形は { attributes: [] } です
onDataChangeチェックボックスが変更されるたびに次のデータオブジェクトを受け取ります
options (optional)デフォルトの CheckboxList によって描画されるチェックボックスの選択肢
labels (optional)タイトル、サブタイトル、フィールド名、送信ボタンの文言
layout (optional)チェックボックスリストのレイアウト: one-column またはレスポンシブな grid
onSubmit (optional)継承されたフォーム送信ハンドラ
制御されたコンポーネント

AttributeSelection は選択状態を保持しません。data をアプリ内に保存し、ブロックへ渡し返し、onDataChange から更新してください。選択された項目は data.attributes{ key, value } のペアとして保存されます。

コード例

ライブエディター
function Example() {
  const defaultData = {attributes: []};
  const [data, setData] = React.useState(defaultData);

  const options = [
    {key: 'role', value: 'frontend', label: 'Frontend engineer'},
    {key: 'role', value: 'backend', label: 'Backend engineer'},
    {key: 'role', value: 'fullstack', label: 'Full-stack engineer'},
    {key: 'role', value: 'data', label: 'Data engineer'},
  ];

  const labels = {
    attributeSelectionTitle: 'Role',
    subtitle: 'Select the roles that match your profile',
    fieldName: 'Role options',
    submitButton: 'Save',
  };

  return (
    <AttributeSelection
      data={data}
      onDataChange={setData}
      options={options}
      labels={labels}
      layout="grid"
      onSubmit={event => {
        event.preventDefault();
        setData((current) => ({ ...current }));
      }}
    />
  );
}
結果
Loading...

重要なプロパティ

コアプロパティ

プロパティ必須デフォルト説明
dataAttributeSelectionFormDataはい-制御されたデータオブジェクト: { attributes: { key: string; value: string }[] }
onDataChange(data: AttributeSelectionFormData, meta: { name: string; value: unknown; cause: 'input' | 'change' | 'blur' | 'clear' | 'reset' | 'programmatic'; event?: SyntheticEvent }) => voidはい-選択が変更されたときに呼び出されます。CheckboxFieldcause: 'change'name: 'attributes' を使います
optionsAttributeSelectionOption[]いいえundefinedCheckboxList によって描画されるオプション。各オプションは { key, value, label } です
errors{ [fieldName: string]: string | string[] }いいえundefinedカスタムブロック用にコンテキストへ保存されます。デフォルト UI ブロックはエラーを描画しません
layout'one-column' | 'grid'いいえ'one-column'CheckboxList のレイアウトを制御します

コンテンツプロパティ

プロパティ必須デフォルト説明
labels{ attributeSelectionTitle?: ReactNode; subtitle?: ReactNode; fieldName?: ReactNode; submitButton?: ReactNode }いいえundefinedデフォルトのタイトル、サブタイトル、フィールド名、送信ボタンで使われる文言
labels.attributeSelectionTitleReactNodeいいえundefinedAttributeSelection.Title によって描画されるタイトルテキスト
labels.subtitleReactNodeいいえundefinedAttributeSelection.Subtitle によって描画されるサブタイトルテキスト
labels.fieldNameReactNodeいいえundefinedチェックボックス一覧の上に描画されるフィールドラベル
labels.submitButtonReactNodeいいえ'保存'children が渡されない場合の送信ボタンのコンテンツ

サブコンポーネント:

サブコンポーネント主な props継承元基盤
AttributeSelection.Titlechildrenlabels.attributeSelectionTitleTypographyPropsTypography
AttributeSelection.Subtitlechildrenlabels.subtitleTypographyPropsTypography
AttributeSelection.FieldNamechildrenlabels.fieldNameTypographyPropsTypography
AttributeSelection.CheckboxListoptionslayoutchildrenStackPropsStack
AttributeSelection.SubmitButtonchildrenlabels.submitButtonButtonPropsButton

レイアウトと構成のプロパティ

プロパティ必須デフォルト説明
childrenBlocksOverride | ReactNodeいいえundefinedコンパウンド子要素、または blocksblockOrder を返す関数オーバーライド
classNamestringいいえundefinedルートフォームのクラス名(nbb-attribute-selection が適用されます)
sxSxPropsいいえundefinedルートフォーム用の MUI システムスタイル
onSubmitFormEventHandlerいいえundefined継承されたフォーム送信ハンドラ

AttributeSelectionchildren を除く StackProps を継承します。ルートは component="form" として描画され、コンテキスト外の Stack/form props を引き渡します。

デフォルト UI ブロック

ブロック基盤備考
AttributeSelection(ルート)Stackcomponent="form" のルートフォーム
AttributeSelection.TitleTypographylabels.attributeSelectionTitle を使用します。組み込みのフォールバックテキストはありません
AttributeSelection.SubtitleTypographylabels.subtitle を使用します。組み込みのフォールバックテキストはありません
AttributeSelection.FieldNameTypographylabels.fieldName を使用します。組み込みのフォールバックテキストはありません
AttributeSelection.CheckboxListStackoptions をチェックボックスに変換します。layout="grid" はレスポンシブな 2/3/4 列グリッドを使います
AttributeSelection.SubmitButtonButtonvariant="contained"size="large"fullWidthtype="submit"、デフォルトテキストは '保存'

追加フィールドプリミティブ

プリミティブ基盤備考
AttributeSelection.CheckboxFieldFormControlLabel + Checkboxカスタムフロー用の手動チェックボックスプリミティブ。namevaluelabel を渡します。切り替えると data.attributes{ key: name, value } が追加・削除されます。デフォルトの controlsx={{ p: 0 }} の MUI チェックボックスです

TypeScript

import {
AttributeSelection,
type AttributeSelectionFormData,
type AttributeSelectionOption,
} from '@nodeblocks/frontend-attribute-selection-block';

const options: AttributeSelectionOption[] = [
{key: 'role', value: 'frontend', label: 'Frontend engineer'},
{key: 'role', value: 'backend', label: 'Backend engineer'},
];

const [data, setData] = React.useState<AttributeSelectionFormData>({
attributes: [],
});

<AttributeSelection
data={data}
onDataChange={setData}
options={options}
labels={{attributeSelectionTitle: 'Role', fieldName: 'Roles', submitButton: 'Save'}}
/>;