Skip to content

Commit 7c5f9f4

Browse files
committed
[IMP] add_dimension_button: add computeFuzzySearchKey props
This commit adds a way to give a custom function to compute fuzzy search keys. It will be used in Odoo to provide better fuzzy search results by taking into account the technical names of the fields. Task: 5226786
1 parent c093c6a commit 7c5f9f4

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/components/side_panel/pivot/pivot_layout_configurator/add_dimension_button/add_dimension_button.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { Popover } from "../../../../popover";
1717
interface Props {
1818
onFieldPicked: (field: string) => void;
1919
fields: PivotField[];
20+
computeFuzzySearchKey: (field: PivotField) => string;
2021
}
2122

2223
export class AddDimensionButton extends Component<Props, SpreadsheetChildEnv> {
@@ -25,8 +26,12 @@ export class AddDimensionButton extends Component<Props, SpreadsheetChildEnv> {
2526
static props = {
2627
onFieldPicked: Function,
2728
fields: Array,
29+
computeFuzzySearchKey: { type: Function, optional: true },
2830
slots: { type: Object, optional: true },
2931
};
32+
static defaultProps = {
33+
computeFuzzySearchKey: (field: PivotField) => field.string,
34+
};
3035

3136
private buttonRef = useRef("button");
3237
private popover = useState({ isOpen: false });
@@ -61,15 +66,17 @@ export class AddDimensionButton extends Component<Props, SpreadsheetChildEnv> {
6166
get proposals(): AutoCompleteProposal[] {
6267
let fields: PivotField[];
6368
if (this.search.input) {
64-
fields = fuzzyLookup(this.search.input, this.props.fields, (field) => field.string);
69+
fields = fuzzyLookup(this.search.input, this.props.fields, (field) =>
70+
this.props.computeFuzzySearchKey(field)
71+
);
6572
} else {
6673
fields = this.props.fields;
6774
}
6875
return fields.map((field) => {
6976
const text = field.string;
7077
return {
7178
text,
72-
fuzzySearchKey: text,
79+
fuzzySearchKey: this.props.computeFuzzySearchKey(field),
7380
htmlContent: getHtmlContentFromPattern(
7481
this.search.input,
7582
text,

tests/pivots/add_dimension_button.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,26 @@ describe("Add dimension button", () => {
4848
await keyDown({ key: "Enter" });
4949
expect(onFieldPicked).toHaveBeenCalledWith("Amount");
5050
});
51+
52+
test("Can give a custom fuzzy search key compute", async () => {
53+
const computeFuzzySearchKey = jest.fn();
54+
const { fixture } = await mountAddDimensionButton({
55+
fields: [
56+
{ name: "Amount", type: "integer", string: "Amount" },
57+
{ name: "Product", type: "char", string: "Product" },
58+
],
59+
computeFuzzySearchKey,
60+
});
61+
await click(fixture.querySelector(".add-dimension")!);
62+
expect(computeFuzzySearchKey).toHaveBeenNthCalledWith(1, {
63+
name: "Amount",
64+
type: "integer",
65+
string: "Amount",
66+
});
67+
expect(computeFuzzySearchKey).toHaveBeenNthCalledWith(2, {
68+
name: "Product",
69+
type: "char",
70+
string: "Product",
71+
});
72+
});
5173
});

0 commit comments

Comments
 (0)