UI Components Library
Relevant source files
The following files were used as context for generating this wiki page:
The Medical Calculators project features a small, dedicated library of reusable React components located in the src/components/ directory. These components form the building blocks for the user interface of all medical calculators within the application. They are designed to provide a consistent look and feel, ensure accessibility, and streamline the development process for new calculators.
The components are built using TypeScript for type safety and styled with Tailwind CSS, leveraging a set of custom CSS variables for a consistent dark theme. The library includes essential form elements like buttons, inputs, and selectors, as well as structural components like cards for layout. The CONTRIBUTING.md guide explicitly recommends using these pre-built components when adding new calculators to the project.
Sources: CONTRIBUTING.md, README.md, src/calculators/PotassiumCorrectionCalculator.tsx
Core Components
The library consists of several key components used to construct the calculator interfaces.
Card
The Card component is a container used to wrap each calculator, providing a consistent border, background, and padding. It can include an optional title and subtitle.
The diagram below shows the props interface for the Card component.
| Prop | Type | Description |
|---|---|---|
title | string | The main title displayed at the top of the card. |
subtitle | string | An optional subtitle displayed below the title. |
className | string | Optional additional CSS classes for styling. |
children | React.ReactNode | The content to be rendered inside the card. |
Sources: src/components/Card.tsx, src/calculators/PotassiumCorrectionCalculator.tsx:101
Button
The Button component is used for all clickable actions, such as submitting a form or resetting inputs. It supports different visual variants and sizes.
The diagram below shows the props interface for the Button component.
| Prop | Type | Description |
|---|---|---|
variant | "primary" | "outline" | The visual style of the button. primary for main actions, outline for secondary actions. |
size | "sm" | "lg" | The size of the button. Defaults to a standard size if not specified. |
onClick | () => void | The function to execute when the button is clicked. |
children | React.ReactNode | The content of the button, typically text or an icon. |
className | string | Optional additional CSS classes for styling. |
Sources: src/components/Button.tsx, CONTRIBUTING.md:27-29, src/app/page.tsx:32-38
NumberInput
The NumberInput component is a specialized input field for numeric values. It includes a label, unit display, and supports constraints like min/max values.
The diagram below shows the props interface for the NumberInput component.
| Prop | Type | Description |
|---|---|---|
label | string | The text label displayed above the input field. |
value | number | null | The current value of the input. |
onChange | (value: number | null) => void | Callback function triggered when the input value changes. |
min | number | The minimum allowed value. |
max | number | The maximum allowed value. |
step | number | The increment/decrement step for the input. |
unit | string | An optional unit string (e.g., "kg", "mmol/L") displayed in the input. |
required | boolean | Whether the input is mandatory. |
precision | number | The number of decimal places to allow. |
helperText | string | Optional text displayed below the input for additional guidance. |
Sources: src/components/NumberInput.tsx, src/calculators/PotassiumCorrectionCalculator.tsx:107-116, src/calculators/PediatricDoseCalculator.tsx:288-296
Select
The Select component is a dropdown menu for selecting one option from a list.
The diagram below shows the props interface for the Select component.
| Prop | Type | Description |
|---|---|---|
label | string | The text label displayed above the select field. |
value | string | The currently selected value. |
onChange | (value: string) => void | Callback function triggered when the selection changes. |
options | { value: string, label: string }[] | An array of objects representing the selectable options. |
required | boolean | Whether a selection is mandatory. |
Sources: src/components/Select.tsx, src/calculators/InfusionTherapyCalculator.tsx:325-331
Checkbox
The Checkbox component is used for boolean options that a user can toggle on or off.
The diagram below shows the props interface for the Checkbox component.
| Prop | Type | Description |
|---|---|---|
id | string | A unique identifier for the checkbox and its label. |
checked | boolean | The current state of the checkbox (true for checked). |
onChange | (checked: boolean) => void | Callback function triggered when the state changes. |
children | React.ReactNode | The label or content displayed next to the checkbox. |
Sources: src/components/Checkbox.tsx, src/calculators/PotassiumCorrectionCalculator.tsx:128-132
MobileMenu
The MobileMenu component provides a slide-out navigation menu for smaller screen sizes. It is controlled by the main page component.
The diagram below shows the props interface for the MobileMenu component.
| Prop | Type | Description |
|---|---|---|
isOpen | boolean | Controls the visibility of the menu. |
onClose | () => void | Function to call to close the menu. |
onCalculatorSelect | (calculator: string) => void | Function to call when a calculator is selected from the menu. |
activeCalculator | string | The key of the currently active calculator to highlight it in the menu. |
t | object | The translation object for rendering menu item labels. |
Sources: src/components/MobileMenu.tsx, src/app/page.tsx:309-315
Component Composition
The UI components are composed within each calculator component to build the input forms. A typical calculator uses a Card as the main container, which holds various NumberInput, Select, and Checkbox components for data entry, and Button components to trigger calculation and reset actions.
This diagram illustrates the typical component hierarchy for a calculator page.
Styling
The project uses Tailwind CSS for styling, with a custom dark theme defined through CSS variables in src/app/globals.css. These variables control the color palette for the entire application, ensuring a consistent look across all components.
The primary color variables are:
| Variable | Value | Description |
|---|---|---|
--background | #1a202c | Main application background color. |
--foreground | #ffffff | Main text color. |
--card | #2d3748 | Background color for Card components. |
--border | #4a5568 | Border color for components. |
--input | #2d3748 | Background color for input fields. |
--primary | #63b3ed | Primary accent color for buttons and links. |
--primary-foreground | #ffffff | Text color used on primary backgrounds. |
--muted-foreground | #a0aec0 | Color for less important text. |
These variables are then consumed by Tailwind's theme configuration. The file also includes custom styles for scrollbars and hover effects.
Sources: src/app/globals.css:3-29
Usage Guidelines
The CONTRIBUTING.md file provides clear instructions for developers on how to use the UI component library to create a new calculator. The process involves:
- Wrapping the entire calculator UI in a
<Card>component. - Using
NumberInput,Select, andCheckboxfor the input form fields. - Using
Buttoncomponents withprimaryandoutlinevariants for the "Calculate" and "Reset" actions. - Conditionally rendering the results section within the card.
The following snippet from the contribution guide demonstrates the standard composition of these components:
// From CONTRIBUTING.md
return (
<Card title="Название калькулятора" className="max-w-2xl mx-auto">
{/* Форма ввода */}
<div className="space-y-6">
{/* Компоненты ввода */}
<div className="flex space-x-4">
<Button onClick={calculate} variant="primary">
Рассчитать
</Button>
<Button onClick={reset} variant="outline">
Сбросить
</Button>
</div>
{/* Отображение результатов */}
{result && (
<div className="mt-6 p-4 bg-accent rounded-lg">
{/* Результаты */}
</div>
)}
</div>
</Card>
);
Sources: CONTRIBUTING.md:25-45