Skip to content

Commit

Permalink
Review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaustubh9031 committed Dec 26, 2024
1 parent 1349004 commit d99be12
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
9 changes: 5 additions & 4 deletions src/Filter/LabelFilter/LabelFilter.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Label } from "../../LabelSelector/Label.types";
import LabelFilter from "./LabelFilter";
import React from "react";
import { action } from "@storybook/addon-actions";
Expand All @@ -9,9 +10,9 @@ export default {

// example options
const options = [
{ _id: 1, color: "#005FA8", description: "first label", name: "label 1" },
{ _id: 2, color: "#f542e0", description: "second label", name: "label 2" },
{ _id: 3, color: "#ffa500", description: "third label", name: "label 3" }
{ _id: "1", color: "#005FA8", description: "first label", name: "label 1" },
{ _id: "2", color: "#f542e0", description: "second label", name: "label 2" },
{ _id: "3", color: "#ffa500", description: "third label", name: "label 3" }
];

// story template with state for selection
Expand All @@ -20,7 +21,7 @@ const Template = args => {
React.useEffect(() => {
setValue(args.value);
}, [args.value]);
const onChange = (value: typeof options) => {
const onChange = (value: Label[]) => {
setValue(value);
action("onChange")(value);
};
Expand Down
28 changes: 22 additions & 6 deletions src/Filter/LabelFilter/LabelFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,32 @@ function LabelFilterPopper({
);
}

// render for label chips
// Render for label chips
/**
* Renders the selected labels as chips with delete functionality.
* @param labels - The array of selected labels to display as chips.
* @param getTagProps - A function provided by Material-UI to generate props for each tag.
* @param ownerState - The owner state of the Autocomplete component, including `value` and `onChange` handler.
* @returns An array of LabelChip components representing the selected labels.
*/
function Tags(
labels: Label[],
getTagProps: AutocompleteRenderGetTagProps,
ownerState: AutocompleteOwnerState<Label, true, undefined, undefined, "div">
) {
const value = ownerState.value || []; // Current selected labels
const onChange = ownerState.onChange || (() => {}); // No-op if onChange is not provided
// Extract current selected labels and onChange handler from ownerState
const { value = [], onChange = () => {} } = ownerState;
return labels.map((label, index) => (
<LabelChip
key={label._id}
label={label.name}
color={label.color}
style={{ marginLeft: 2 }}
onDelete={e => {
const updatedValue = value.filter(l => l._id !== label._id); // Remove label from the value
// Remove label from the value
const updatedValue = value.filter(l => l._id !== label._id);
// Trigger onChange with event, updated value, reason, and details
onChange(e as React.SyntheticEvent, updatedValue, "removeOption", {
onChange(e, updatedValue, "removeOption", {
option: label
});
}}
Expand All @@ -83,12 +91,20 @@ function Tags(
}

// Render label options
/**
* Renders a single option in the dropdown for selection.
* @param props - HTML attributes for the list item element.
* @param option - The label object representing the current option.
* @param selected - Indicates whether the current option is selected.
* @returns A list item element representing the label option.
*/
function Option(
props: React.HTMLAttributes<HTMLLIElement>,
option: Label,
{ selected }: { selected: boolean }
) {
const { key, ...restProps } = props as { key?: React.Key }; // Extract key explicitly
// Extract key explicitly
const { key, ...restProps } = props as { key?: React.Key };
return (
<li key={key} {...restProps}>
<Checkbox
Expand Down
23 changes: 23 additions & 0 deletions src/Filter/LabelFilter/LabelFilter.types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
import type { Label } from "../../LabelSelector/Label.types";
import LabelFilter from "./LabelFilter";

/**
* Props definition for the LabelFilter component
*/
export type LabelFilterProps = {
/**
* Label for the filter
*/
label?: string;
/**
* How many tags to show before truncating and showing the plus sign, -1 is no limit.
*/
limitTags?: number;
/**
* Name of the filter
*/
name?: string;
/**
* OnChange handler for the filter box
* @param value the value to add/delete from the filtered values
* @returns void
*/
onChange?: (value: Label[]) => void;
/**
* Options to choose from in the dropdown box
*/
options: Label[];
/**
* Current value of what's been selected
*/
value?: Label[];
};

Expand Down

0 comments on commit d99be12

Please sign in to comment.