Add HorizontalRule component and update Category component

This commit is contained in:
Simon Larsen 2023-12-16 19:18:22 +00:00
parent b42a69b154
commit c95f845a5c
No known key found for this signature in database
GPG Key ID: AB45983AA9C81CDE
3 changed files with 20 additions and 7 deletions

View File

@ -5,6 +5,7 @@ import {
} from './CategoryCheckboxTypes';
import CheckboxElement from '../Checkbox/Checkbox';
import CheckBoxList from './CheckboxList';
import HorizontalRule from '../HorizontalRule/HorizontalRule';
export type CategoryCheckboxValue = string | number | boolean;
@ -13,6 +14,7 @@ export interface CategoryProps {
options: Array<CategoryCheckboxOption>;
onChange: (value: Array<CategoryCheckboxValue>) => void;
initialValue?: undefined | Array<CategoryCheckboxValue>;
isLastCategory: boolean;
}
const Category: FunctionComponent<CategoryProps> = (
@ -86,7 +88,7 @@ const Category: FunctionComponent<CategoryProps> = (
/>
</div>
)}
<div className={`${props.category ? 'ml-3' : ''}`}>
<div className={`${props.category ? 'ml-7' : ''}`}>
<CheckBoxList
options={props.options}
onChange={(newValues: Array<CategoryCheckboxValue>) => {
@ -95,6 +97,8 @@ const Category: FunctionComponent<CategoryProps> = (
}}
/>
</div>
{!props.isLastCategory ? <div className="-mb-3 -mt-3"><HorizontalRule /></div> : <></>}
</div>
);
};

View File

@ -35,7 +35,8 @@ const CategoryCheckbox: FunctionComponent<CategoryCheckboxProps> = (
}
const getCategory: Function = (
category?: CheckboxCategory
category?: CheckboxCategory,
isLastCategory: boolean = false
): ReactElement => {
return (
<Category
@ -46,6 +47,7 @@ const CategoryCheckbox: FunctionComponent<CategoryCheckboxProps> = (
return option.categoryId === category?.id;
}
)}
isLastCategory={isLastCategory}
onChange={(value: Array<CategoryCheckboxValue>) => {
// remove any value from currentValue that belongs to this category.
@ -66,6 +68,7 @@ const CategoryCheckbox: FunctionComponent<CategoryCheckboxProps> = (
tempCurrentValues.splice(index, 1);
}
});
// add the new values to currentValue
value.forEach((value: CategoryCheckboxValue) => {
@ -82,9 +85,9 @@ const CategoryCheckbox: FunctionComponent<CategoryCheckboxProps> = (
return (
<div>
{getCategory(undefined)}
{getCategory(undefined, props.categories.length === 0)}
{props.categories.map((category: CheckboxCategory, i: number) => {
return <div key={i}>{getCategory(category)}</div>;
return <div key={i}>{getCategory(category, i === props.categories.length - 1)}</div>;
})}
{props.error && (
<p

View File

@ -1,7 +1,13 @@
import React, { ReactElement } from 'react';
import React, { FunctionComponent, ReactElement } from 'react';
const HorizontalRule: () => JSX.Element = (): ReactElement => {
return <div className="border-b border-gray-900/10 mb-8 mt-8"></div>;
export interface ComponentProps {
className?: string | undefined;
}
const HorizontalRule: FunctionComponent<ComponentProps> = (
props: ComponentProps
): ReactElement => {
return <div className={`border-b border-gray-900/10 mb-8 mt-8 ${props.className}`}></div>;
};
export default HorizontalRule;