A React component demonstrating an interactive option selector with customizable options and styling. This component provides a flexible way to present and select from a list of options with visual feedback.
# Clone the repository
git clone [repository-url]
# Install dependencies
npm install
# Start the development server
npm run dev
import { OptionSelector } from './OptionSelector';
// Sample options data
const options = [
{ value: 'happy', text: 'Happy' },
{ value: 'pleased', text: 'Pleased' },
{ value: 'sad', text: 'Sad' }
];
// Rendering the option selector
const OptionSelectorDemo = () => {
const [selected, setSelected] = React.useState('happy');
const handleSelect = (e, value) => {
setSelected(value);
};
return (
<OptionSelector
selected={selected}
onSelect={handleSelect}
options={options}
layout="vertical" // or "horizontal"
/>
);
};
The option selector expects options in the following format:
{
value: string, // Unique identifier for the option
text: string // Display text for the option
}
Prop | Type | Required | Description |
---|---|---|---|
options |
Array | Yes | Array of option objects |
selected |
String/Array | Yes | Currently selected value(s) |
onSelect |
Function | Yes | Callback for option selection |
layout |
String | No | Layout direction ('vertical' or 'horizontal') |
The component uses SCSS for styling with the following features:
.option-selector {
display: flex;
&.horizontal {
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
}
&.vertical {
flex-direction: column;
}
.option {
cursor: pointer;
background-color: #f89c13;
color: #fff;
padding: 2px 4px;
margin: 3px;
width: max-content;
border-radius: 5px;
&.selected {
background-color: adjust-color(palevioletred, $lightness: -15%);
}
&:hover {
background-color: palevioletred;
}
}
}
This project uses Vite for development. To get started:
npm install
npm run dev
http://localhost:5173
npm run build
The build artifacts will be stored in the dist/
directory.
MIT