@yuneta/gobj-ui 7.32.2 → 7.32.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -4401,6 +4401,16 @@ header filter:
4401
4401
  - A column with no `field`, or with a control formatter (`rowSelection`),
4402
4402
  gets no filter. Column groups are walked.
4403
4403
 
4404
+ A column whose cell shows something else than its value (a boolean drawn as
4405
+ "enabled", a time drawn as a date, a list drawn as names) filters with
4406
+ `yui_text_filter(text_of)`. `text_of(data)` gives the text that the row
4407
+ shows, and the row passes when that text holds the term:
4408
+
4409
+ ```js
4410
+ {title: t("status"), field: "disabled",
4411
+ headerFilterFunc: yui_text_filter((d) => t(d.disabled ? "disabled" : "enabled"))}
4412
+ ```
4413
+
4404
4414
  A tree table (`dataTree`) filters each column with
4405
4415
  `yui_tree_filter(text_of)`. Tabulator filters a tree from its roots, so a
4406
4416
  parent that fails is hidden with all its children: a filter on the name of
@@ -4429,9 +4439,9 @@ and the paginator keys of `yui_tabulator_lang()`.
4429
4439
  Two things to look at when you apply it:
4430
4440
 
4431
4441
  - A text filter compares the value of the row, not what the formatter
4432
- shows. If a column shows a boolean as "Yes" or "No", give it a tick
4433
- (`headerFilter: "tickCross"`, `headerFilterParams: {tristate: true}`) or
4434
- a list.
4442
+ shows. If a column shows a boolean as "Yes" or "No", give it
4443
+ `yui_text_filter()`, a tick (`headerFilter: "tickCross"`,
4444
+ `headerFilterParams: {tristate: true}`) or a list.
4435
4445
  - A column with a fixed `width` under 150 px cuts the placeholder of its
4436
4446
  filter. Give it 150.
4437
4447
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yuneta/gobj-ui",
3
- "version": "7.32.2",
3
+ "version": "7.32.3",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "dist/gobj-ui.cjs.js",
@@ -184,6 +184,38 @@ function yui_table_filters(columns)
184
184
  });
185
185
  }
186
186
 
187
+ /***************************************************************
188
+ * The header filter of a column whose cell SHOWS something else
189
+ * than its value: a boolean drawn as "enabled", a time drawn as a
190
+ * date, a list drawn as names.
191
+ *
192
+ * `text_of(data)` answers the text that the row shows in the
193
+ * column. The row passes when it holds the term, in any case. A
194
+ * plain text filter compares the VALUE, so "enabled" finds nothing
195
+ * in a column of `false`.
196
+ *
197
+ * {title: t("status"), field: "disabled",
198
+ * headerFilterFunc: yui_text_filter((d) => t(d.disabled ? "disabled" : "enabled"))}
199
+ *
200
+ * `yui_table_filters()` then gives the column its text box.
201
+ ***************************************************************/
202
+ function yui_text_filter(text_of)
203
+ {
204
+ return function(term, value, data) {
205
+ let needle = String(term ?? "").trim().toLowerCase();
206
+ if(!needle) {
207
+ return true;
208
+ }
209
+ let text = "";
210
+ try {
211
+ text = String(text_of(data) ?? "");
212
+ } catch(e) {
213
+ text = "";
214
+ }
215
+ return text.toLowerCase().includes(needle);
216
+ };
217
+ }
218
+
187
219
  /***************************************************************
188
220
  * The header filter of a column of a TREE table (`dataTree`).
189
221
  *
@@ -273,6 +305,7 @@ export {
273
305
  YUI_TABLE_FILTER_MIN_WIDTH,
274
306
  yui_table_paging,
275
307
  yui_table_filters,
308
+ yui_text_filter,
276
309
  yui_tree_filter,
277
310
  yui_table_wire,
278
311
  };
@@ -17,6 +17,7 @@ import {
17
17
  YUI_TABLE_FILTER_MIN_WIDTH,
18
18
  yui_table_paging,
19
19
  yui_table_filters,
20
+ yui_text_filter,
20
21
  yui_tree_filter,
21
22
  yui_table_wire,
22
23
  } from "./yui_table_standard.js";
@@ -147,6 +148,29 @@ describe("the filters", () => {
147
148
  });
148
149
  });
149
150
 
151
+ describe("the filter of what a cell shows", () => {
152
+ const f = yui_text_filter((d) => d.disabled ? "Disabled" : "Enabled");
153
+
154
+ test("compares the text shown, not the value, in any case", () => {
155
+ expect(f("enab", false, {disabled: false})).toBe(true);
156
+ expect(f("enab", true, {disabled: true})).toBe(false);
157
+ expect(f("DIS", true, {disabled: true})).toBe(true);
158
+ });
159
+
160
+ test("an empty term passes everything", () => {
161
+ expect(f("", true, {disabled: true})).toBe(true);
162
+ expect(f(undefined, true, {disabled: true})).toBe(true);
163
+ });
164
+
165
+ test("a text_of that throws, or answers nothing, matches nothing", () => {
166
+ let bad = yui_text_filter(() => {
167
+ throw new Error("x");
168
+ });
169
+ expect(bad("a", null, {})).toBe(false);
170
+ expect(yui_text_filter(() => null)("a", null, {})).toBe(false);
171
+ });
172
+ });
173
+
150
174
  describe("the filter of a tree", () => {
151
175
  const node = {
152
176
  host: "artgins",