Skip to main content

Attribute Selection Block

The AttributeSelection Component is a fully customizable and accessible attribute selection form built with React and TypeScript. It provides a complete interface for attribute selection with form validation, error handling, and modern design patterns.


๐Ÿš€ Installationโ€‹

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

๐Ÿ“– Usageโ€‹

import {AttributeSelection} from '@nodeblocks/frontend-attribute-selection-block';
Live Editor
function BasicAttributeSelection() {
  return (
    <AttributeSelection
      onSubmit={formData => {
        console.log('Attributes selected:', formData);
      }}
      onChange={(setError, getValues) => {
        const values = getValues();
        console.log('Form values:', values);
      }}
      onCancel={reset => {
        reset();
        console.log('Form cancelled');
      }}
      defaultValues={{example: true}}
      style={{
        display: 'flex',
        flexDirection: 'column',
        gap: '16px',
      }}>
      <AttributeSelection.Title style={{color: 'black'}}>Title</AttributeSelection.Title>
      <AttributeSelection.Subtitle style={{color: 'black'}}>Subtitle</AttributeSelection.Subtitle>
      <AttributeSelection.FieldName style={{color: 'black'}}>FieldName</AttributeSelection.FieldName>
      <input type="checkbox" name="example" />
      <AttributeSelection.SubmitButton>Submit</AttributeSelection.SubmitButton>
      <AttributeSelection.CancelButton>Cancel</AttributeSelection.CancelButton>
    </AttributeSelection>
  );
}
Result
Loading...

๐Ÿ”ง Props Referenceโ€‹

Main Component Propsโ€‹

PropTypeDefaultDescription
onSubmit(data: T) => voidRequiredCallback function triggered when form is submitted with valid data
onChange(setError: UseFormSetError<T>, getValues: UseFormGetValues<T>) => voidRequiredCallback function triggered when form values change
onCancel(reset: UseFormReset<T>) => voidRequiredCallback function triggered when cancel button is clicked
defaultValuesDefaultValues<T>undefinedDefault form values to populate fields on initial render
classNamestringundefinedAdditional CSS class name for styling the form container
childrenBlocksOverrideundefinedCustom blocks override for form components

Note: This component inherits all HTML form element props.

Sub-Componentsโ€‹

The AttributeSelection component provides several sub-components. All sub-components receive their default values from the main component's context and can override these values through props.

AttributeSelection.Titleโ€‹

PropTypeDefaultDescription
childrenReactNode"่ท็จฎ"Title text content for the form header
classNamestringundefinedAdditional CSS class name for styling

Note: This component inherits all HTML h2 element props.

AttributeSelection.Subtitleโ€‹

PropTypeDefaultDescription
childrenReactNode"ใ”่‡ช่บซใฎ่ท็จฎใ‚’้ธๆŠžใ—ใฆใใ ใ•ใ„"Subtitle text content providing form instructions
classNamestringundefinedAdditional CSS class name for styling

Note: This component inherits all HTML div element props.

AttributeSelection.FieldNameโ€‹

PropTypeDefaultDescription
childrenReactNode"ๅฝนๅ‰ฒ"Field name text content displayed above form inputs
classNamestringundefinedAdditional CSS class name for styling

Note: This component inherits all HTML div element props.

AttributeSelection.Checkboxโ€‹

PropTypeDefaultDescription
namestring"example"Field name used for form data and validation
valuebooleanfalseDefault checked state of the checkbox
labelstringundefinedLabel text displayed next to the checkbox
isRequiredbooleanfalseWhether the checkbox is required for form validation
isDisabledbooleanfalseWhether the checkbox is disabled
classNamestringundefinedAdditional CSS class name for styling

Note: This component inherits all HTML checkbox element props.

AttributeSelection.SubmitButtonโ€‹

PropTypeDefaultDescription
childrenReactNode"ๆฌกใธ"Button text content
classNamestringundefinedAdditional CSS class name for styling
fillenum"fill"Button fill style
iconenumundefinedOptional icon for the left-hand side of the button
iconColorenumundefinedColor for the left-hand side icon. When not provided, a default color for the fill type will be used.
isDisabledbooleanfalseWhether the button is disabled and cannot be used
onClickfunctionundefinedFunction to handle button click
sizeenum"M"Button vertical size
textAlignenum"center"Button icon and text positioning within the button
textColorenum"default"Button text color
textEmphasisbooleanfalseButton text weight
textSizeenum"M"Button text size
typeenum"submit"Button purpose (affects html type)

AttributeSelection.CancelButtonโ€‹

PropTypeDefaultDescription
childrenReactNode"ๆˆปใ‚‹"Button text content
classNamestringundefinedAdditional CSS class name for styling
fillenum"fill"Button fill style
iconenumundefinedOptional icon for the left-hand side of the button
iconColorenumundefinedColor for the left-hand side icon. When not provided, a default color for the fill type will be used.
isDisabledbooleanfalseWhether the button is disabled and cannot be used
onClickfunctionundefinedFunction to handle button click
sizeenum"M"Button vertical size
textAlignenum"center"Button icon and text positioning within the button
textColorenum"default"Button text color
textEmphasisbooleanfalseButton text weight
textSizeenum"M"Button text size
typeenum"button"Button purpose (affects html type)

๐Ÿ”ง TypeScript Supportโ€‹

Full TypeScript support with comprehensive type definitions:

import {AttributeSelection} from '@nodeblocks/frontend-attribute-selection-block';
import {UseFormGetValues, UseFormSetError} from 'react-hook-form';

// Define your own form data interface
interface CustomAttributeFormData {
role: boolean;
department: boolean;
experience: boolean;
skills: boolean;
}

const handleSubmit = (formData: CustomAttributeFormData) => {
// Type-safe form handling
console.log(formData.role, formData.department, formData.experience);
};

const handleChange = (
setError: UseFormSetError<CustomAttributeFormData>,
getValues: UseFormGetValues<CustomAttributeFormData>,
) => {
const values = getValues();
// Validate form values and set errors if needed
if (!values.role && !values.department) {
setError('role', {message: 'Please select at least one option'});
}
};

const AttributeSelectionForm = () => {
return (
<AttributeSelection<CustomAttributeFormData>
onSubmit={handleSubmit}
onChange={handleChange}
onCancel={reset => reset()}
defaultValues={{role: false, department: false, experience: false, skills: false}}>
<AttributeSelection.Title>Select Your Attributes</AttributeSelection.Title>
<AttributeSelection.Subtitle>Choose the options that apply to you</AttributeSelection.Subtitle>
<AttributeSelection.FieldName>Professional Information</AttributeSelection.FieldName>
<AttributeSelection.Checkbox name="role" label="Management Role" />
<AttributeSelection.Checkbox name="department" label="Technical Department" />
<AttributeSelection.Checkbox name="experience" label="5+ Years Experience" />
<AttributeSelection.Checkbox name="skills" label="Leadership Skills" />
<AttributeSelection.SubmitButton>Continue</AttributeSelection.SubmitButton>
<AttributeSelection.CancelButton>Go Back</AttributeSelection.CancelButton>
</AttributeSelection>
);
};

Built with โค๏ธ using React, TypeScript, and modern web standards.