Skip to main content

Attribute Selection Block

AttributeSelection is a controlled checkbox-selection form for storing selected { key, value } attributes.

Installation

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

What You Need

ItemWhy it matters
dataControlled form data. Default shape is { attributes: [] }
onDataChangeReceives the next data object whenever a checkbox changes
options (optional)Checkbox choices rendered by the default CheckboxList
labels (optional)Copy for title, subtitle, field name, and submit button
layout (optional)Checkbox list layout: one-column or responsive grid
onSubmit (optional)Inherited form submit handler
Controlled component

AttributeSelection does not own selection state. Store data in your app, pass it back to the block, and update it from onDataChange. Selected items are stored in data.attributes as { key, value } pairs.

Code Examples

Live Editor
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 }));
      }}
    />
  );
}
Result
Loading...

Important Props

Core Props

PropTypeRequiredDefaultDescription
dataAttributeSelectionFormDataYes-Controlled data object: { attributes: { key: string; value: string }[] }
onDataChange(data: AttributeSelectionFormData, meta: { name: string; value: unknown; cause: 'input' | 'change' | 'blur' | 'clear' | 'reset' | 'programmatic'; event?: SyntheticEvent }) => voidYes-Called when selection changes. CheckboxField uses cause: 'change' and name: 'attributes'
optionsAttributeSelectionOption[]NoundefinedOptions rendered by CheckboxList; each option is { key, value, label }
errors{ [fieldName: string]: string | string[] }NoundefinedStored in context for custom blocks; default UI blocks do not render errors
layout'one-column' | 'grid'No'one-column'Controls CheckboxList layout

Content Props

PropTypeRequiredDefaultDescription
labels{ attributeSelectionTitle?: ReactNode; subtitle?: ReactNode; fieldName?: ReactNode; submitButton?: ReactNode }NoundefinedCopy consumed by default title, subtitle, field name, and submit button
labels.attributeSelectionTitleReactNodeNoundefinedTitle text rendered by AttributeSelection.Title
labels.subtitleReactNodeNoundefinedSubtitle text rendered by AttributeSelection.Subtitle
labels.fieldNameReactNodeNoundefinedField label rendered above the checkbox list
labels.submitButtonReactNodeNo'保存'Submit button content when no children are passed

Subcomponents:

Sub-componentMain PropsInheritsBuilt on
AttributeSelection.Titlechildren, labels.attributeSelectionTitleTypographyPropsTypography
AttributeSelection.Subtitlechildren, labels.subtitleTypographyPropsTypography
AttributeSelection.FieldNamechildren, labels.fieldNameTypographyPropsTypography
AttributeSelection.CheckboxListoptions, layout, childrenStackPropsStack
AttributeSelection.SubmitButtonchildren, labels.submitButtonButtonPropsButton

Layout and Composition Props

PropTypeRequiredDefaultDescription
childrenBlocksOverride | ReactNodeNoundefinedCompound children or function override returning blocks and blockOrder
classNamestringNoundefinedClass on the root form (nbb-attribute-selection)
sxSxPropsNoundefinedMUI system styles for the root form
onSubmitFormEventHandlerNoundefinedInherited form submit handler

AttributeSelection inherits StackProps except children. The root renders as component="form" and forwards non-context Stack/form props.

Default UI Blocks

BlockBuilt onNotes
AttributeSelection (root)StackRoot form with component="form"
AttributeSelection.TitleTypographyUses labels.attributeSelectionTitle; no built-in fallback text
AttributeSelection.SubtitleTypographyUses labels.subtitle; no built-in fallback text
AttributeSelection.FieldNameTypographyUses labels.fieldName; no built-in fallback text
AttributeSelection.CheckboxListStackMaps options into checkboxes; layout="grid" uses responsive 2/3/4-column grid
AttributeSelection.SubmitButtonButtonvariant="contained", size="large", fullWidth, type="submit", default text '保存'

Extra field primitives

PrimitiveBuilt onNotes
AttributeSelection.CheckboxFieldFormControlLabel + CheckboxManual checkbox primitive for custom flows. Pass name, value, and label; toggling adds/removes { key: name, value } in data.attributes. Default control is a MUI checkbox with sx={{ p: 0 }}

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'}}
/>;