Skip to content

Commit

Permalink
👌 - fix: configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
Xaohs committed Aug 22, 2024
1 parent 1fec421 commit 28da7b9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
14 changes: 7 additions & 7 deletions src/components/data/datagrid/datagrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
sortAttributeDataArray,
} from "../../../lib/data/attributedata";
import { getByDotSeparatedPath } from "../../../lib/data/getByDotSeparatedPath";
import { field2Caption, isLink } from "../../../lib/format/string";
import { field2Title, isLink } from "../../../lib/format/string";
import { BadgeProps } from "../../badge";
import { BoolProps } from "../../boolean";
import { Button, ButtonProps } from "../../button";
Expand Down Expand Up @@ -690,7 +690,7 @@ export const DataGridCaption: React.FC<DataGridCaptionProps> = ({
{
name: "fields",
options: fields.map((f) => ({
label: field2Caption(f.name),
label: field2Title(f.name, { lowerCase: false }),
value: f.name,
selected: Boolean(selectFieldsActiveState[f.name]),
})),
Expand Down Expand Up @@ -780,14 +780,14 @@ export const DataGridHeading: React.FC<DataGridHeadingProps> = ({
{selectable && <th className="mykn-datagrid__cell--checkbox"></th>}
{renderableFields.map((field) => (
<DataGridHeadingCell
key={`${dataGridId}-heading-${field2Caption(field.name)}`}
key={`${dataGridId}-heading-${field2Title(field.name, { lowerCase: false })}`}
field={field}
onSort={onSort}
isSorted={sortField === field.name}
sortable={sortable}
sortDirection={sortDirection}
>
{field2Caption(field.name)}
{field2Title(field.name, { lowerCase: false })}
</DataGridHeadingCell>
))}
</tr>
Expand All @@ -806,7 +806,7 @@ export const DataGridHeading: React.FC<DataGridHeadingProps> = ({
></th>
)}
{renderableFields.map((field) => {
const placeholder = field2Caption(field.name);
const placeholder = field2Title(field.name, { lowerCase: false });

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { options, valueTransform, ...context } = field;
Expand All @@ -825,7 +825,7 @@ export const DataGridHeading: React.FC<DataGridHeadingProps> = ({

return (
<th
key={`${dataGridId}-filter-${field2Caption(field.name)}`}
key={`${dataGridId}-filter-${field2Title(field.name, { lowerCase: false })}`}
className={clsx(
"mykn-datagrid__cell",
"mykn-datagrid__cell--filter",
Expand Down Expand Up @@ -1268,7 +1268,7 @@ export const DataGridContentCell: React.FC<DataGridContentCellProps> = ({
"mykn-datagrid__cell--link": link,
},
)}
aria-description={field2Caption(field.name)}
aria-description={field2Title(field.name, { lowerCase: false })}
>
{valueIsPrimitive &&
isEditingRow &&
Expand Down
2 changes: 1 addition & 1 deletion src/components/data/kanban/kanban.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ export const KanbanItem: React.FC<KanbanItemProps> = ({
const titleField = fieldset[1].title || Object.keys(object)[0];
const urlField = urlFields.find((f) => object[f]);

const title = field2Title(String(object[titleField]));
const title = field2Title(String(object[titleField]), { unHyphen: false });
const href = urlField ? String(object[urlField]) || undefined : undefined;

/**
Expand Down
41 changes: 39 additions & 2 deletions src/lib/format/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,49 @@ export const addSpaces = (string: string) =>
export const field2Caption = (field: string): string =>
ucFirst(unHyphen(addSpaces(field)));

interface Field2TitleOptions {
addSpaces?: boolean;
lowerCase?: boolean;
ucFirst?: boolean;
unHyphen?: boolean;
}

/**
* Converts "field_name" to "Field name".
* @param field
*/
export const field2Title = (field: string): string =>
ucFirst(addSpaces(field).toLowerCase());
export const field2Title = (
field: string,
options?: Field2TitleOptions,
): string => {
// Set default values for options using the spread operator
const {
addSpaces: addSpacesBool = true,
lowerCase: lowerCaseBool = true,
ucFirst: ucFirstBool = true,
unHyphen: unHypenBool = true,
} = options || {};

let result = field;

if (unHypenBool) {
result = unHyphen(result); // Changed name to avoid conflict
}

if (addSpacesBool) {
result = addSpaces(result); // Changed name to avoid conflict
}

if (lowerCaseBool) {
result = result.toLowerCase();
}

if (ucFirstBool) {
result = ucFirst(result); // Changed name to avoid conflict
}

return result;
};

/**
* Converts "Some object name" to "some-object-name".
Expand Down

0 comments on commit 28da7b9

Please sign in to comment.