Skip to content

Commit 99e83a1

Browse files
committed
VanUI 0.11.4: Simplify the implementation of choose
1 parent 64c2719 commit 99e83a1

30 files changed

+159
-162
lines changed

components/README.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ import { <components you want to import> } from "vanjs-ui"
5252
Alternatively, you can import **VanUI** from CDN via a `<script type="text/javascript">` tag:
5353

5454
```html
55-
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected].3/dist/van-ui.nomodule.min.js"></script>
55+
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected].4/dist/van-ui.nomodule.min.js"></script>
5656
```
5757

58-
`https://cdn.jsdelivr.net/npm/[email protected].3/dist/van-ui.nomodule.js` can be used for the non-minified version.
58+
`https://cdn.jsdelivr.net/npm/[email protected].4/dist/van-ui.nomodule.js` can be used for the non-minified version.
5959

6060
Note that: **VanJS** needs to be imported via a `<script type="text/javascript">` tag for **VanUI** to work properly.
6161

@@ -770,12 +770,12 @@ You can override the default stacking behavior by specifying `{customStacking: t
770770

771771
### choose
772772

773-
Creates a [`Modal`](#modal) component that lets the user choose among given options, returns a [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) that resolves when user makes the choice (resolves to the selected string), or cancels (resolves to `undefined`).
773+
Creates a [`Modal`](#modal) component that lets the user choose among given options, returns a [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) that resolves when user makes the choice (resolves to the chosen string), or cancels (resolves to `null`).
774774

775775
#### Signature
776776

777777
```js
778-
choose({...props}) => Promise<string | undefined>
778+
choose({...props}) => Promise<string | null>
779779
```
780780

781781
#### Examples
@@ -785,17 +785,17 @@ Preview with [CodeSandbox](https://codesandbox.io/p/sandbox/github/vanjs-org/van
785785
Example 1:
786786

787787
```ts
788-
const selected = await choose({
788+
const choice = await choose({
789789
label: "Choose a color:",
790790
options: ["Red", "Green", "Blue"],
791791
})
792-
selected && van.add(document.body, div("You chose: ", b(selected)))
792+
choice && van.add(document.body, div("You chose: ", b(choice)))
793793
```
794794

795795
Example 2:
796796

797797
```ts
798-
const selected = await choose({
798+
const choice = await choose({
799799
label: "Choose a South American country:",
800800
options: [
801801
"🇦🇷 Argentina", "🇧🇴 Bolivia", "🇧🇷 Brazil", "🇨🇱 Chile", "🇨🇴 Colombia", "🇪🇨 Ecuador",
@@ -809,7 +809,7 @@ const selected = await choose({
809809
selectedColor: "blue",
810810
selectedStyleOverrides: {color: "white"},
811811
})
812-
selected && van.add(document.body, div("You chose: ", b(selected)))
812+
choice && van.add(document.body, div("You chose: ", b(choice)))
813813
```
814814

815815
#### Property Reference
@@ -825,8 +825,8 @@ selected && van.add(document.body, div("You chose: ", b(selected)))
825825
* `optionsContainerStyleOverrides`: Type `Record<string, string | number>`. Default `{}`. Optional. A [property bag](#property-bag-for-style-overrides) for the styles you want to override for the container of all options.
826826
* `optionClass`: Type `string`. Default `""`. Optional. The `class` attribute of an individual option. You can specify multiple CSS classes separated by `" "`.
827827
* `optionStyleOverrides`: Type `Record<string, string | number>`. Default `{}`. Optional. A [property bag](#property-bag-for-style-overrides) for the styles you want to override for an individual option.
828-
* `selectedClass`: Type `string`. Default `""`. Optional. The `class` attribute of the selected option. You can specify multiple CSS classes separated by `" "`.
829-
* `selectedStyleOverrides`: Type `Record<string, string | number>`. Default `{}`. Optional. A [property bag](#property-bag-for-style-overrides) for the styles you want to override for the selected option.
828+
* `selectedClass`: Type `string`. Default `""`. Optional. The `class` attribute of the currently selected option. You can specify multiple CSS classes separated by `" "`.
829+
* `selectedStyleOverrides`: Type `Record<string, string | number>`. Default `{}`. Optional. A [property bag](#property-bag-for-style-overrides) for the styles you want to override for the currently selected option.
830830

831831
### Property Bag for Style Overrides
832832

components/dist/van-ui.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,5 +166,5 @@ export interface ChooseProps {
166166
readonly selectedClass?: string;
167167
readonly selectedStyleOverrides?: CSSPropertyBag;
168168
}
169-
export declare const choose: ({ label, options, showTextFilter, selectedColor, customModalProps, textFilterClass, textFilterStyleOverrides, optionsContainerClass, optionsContainerStyleOverrides, optionClass, optionStyleOverrides, selectedClass, selectedStyleOverrides, }: ChooseProps) => Promise<string | undefined>;
169+
export declare const choose: ({ label, options, showTextFilter, selectedColor, customModalProps, textFilterClass, textFilterStyleOverrides, optionsContainerClass, optionsContainerStyleOverrides, optionClass, optionStyleOverrides, selectedClass, selectedStyleOverrides, }: ChooseProps) => Promise<string | null>;
170170
export {};

components/dist/van-ui.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,6 @@ export const FloatingWindow = ({ title, closed = van.state(false), x = 100, y =
437437
style: toStyleStr(childrenContainerStyleOverrides)
438438
}, children));
439439
};
440-
let chooseId = 0;
441440
export const choose = ({ label, options, showTextFilter = false, selectedColor = "#f5f5f5", customModalProps = {}, textFilterClass = "", textFilterStyleOverrides = {}, optionsContainerClass = "", optionsContainerStyleOverrides = {}, optionClass = "", optionStyleOverrides = {}, selectedClass = "", selectedStyleOverrides = {}, }) => {
442441
const closed = van.state(false);
443442
const { modalStyleOverrides, ...otherModalProps } = customModalProps;
@@ -471,7 +470,7 @@ export const choose = ({ label, options, showTextFilter = false, selectedColor =
471470
class: textFilterClass,
472471
style: toStyleStr(textFilterStyle),
473472
oninput: e => query.val = e.target.value
474-
}) : undefined;
473+
}) : null;
475474
const optionStyle = {
476475
padding: "0.5rem",
477476
...optionStyleOverrides,
@@ -480,25 +479,25 @@ export const choose = ({ label, options, showTextFilter = false, selectedColor =
480479
"background-color": selectedColor,
481480
...selectedStyleOverrides,
482481
};
483-
const id = "vanui-choose-" + (++chooseId);
484-
document.head.appendChild(van.tags["style"](`#${id} .vanui-choose-selected, #${id} .vanui-choose-option:hover { ${toStyleStr(selectedStyle)} }`));
485-
van.add(document.body, Modal(modalProps, div(label), showTextFilter ? div(textFilterDom) : undefined, () => div({ id, class: optionsContainerClass, style: toStyleStr(optionsContainerStyle) }, filtered.val.map((o, i) => div({
482+
van.add(document.head, () => closed.val ? null :
483+
van.tags["style"](`.vanui-choose-selected, .vanui-choose-option:hover { ${toStyleStr(selectedStyle)} }`));
484+
van.add(document.body, Modal(modalProps, div(label), showTextFilter ? div(textFilterDom) : null, () => div({ class: optionsContainerClass, style: toStyleStr(optionsContainerStyle) }, filtered.val.map((o, i) => div({
486485
class: () => ["vanui-choose-option"].concat(optionClass ? optionClass : [], i === index.val ? "vanui-choose-selected" : [], i === index.val && selectedClass ? selectedClass : []).join(" "),
487486
style: toStyleStr(optionStyle),
488487
onclick: () => (resolve(o), closed.val = true),
489488
}, o)))));
490489
textFilterDom?.focus();
491490
van.derive(() => {
492491
index.val;
493-
setTimeout(() => document.querySelector(`#${id} .vanui-choose-selected`)?.scrollIntoView(false), 10);
492+
setTimeout(() => document.querySelector(".vanui-choose-selected")?.scrollIntoView(false), 10);
494493
});
495494
const navByKey = (e) => {
496495
if (e.key === "Enter" && index.val < filtered.val.length) {
497496
resolve(filtered.val[index.val]);
498497
closed.val = true;
499498
}
500499
else if (e.key === "Escape") {
501-
resolve(undefined);
500+
resolve(null);
502501
closed.val = true;
503502
}
504503
else if (e.key === "ArrowDown")

components/dist/van-ui.nomodule.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,6 @@
437437
style: toStyleStr(childrenContainerStyleOverrides)
438438
}, children));
439439
};
440-
let chooseId = 0;
441440
window.choose = ({ label, options, showTextFilter = false, selectedColor = "#f5f5f5", customModalProps = {}, textFilterClass = "", textFilterStyleOverrides = {}, optionsContainerClass = "", optionsContainerStyleOverrides = {}, optionClass = "", optionStyleOverrides = {}, selectedClass = "", selectedStyleOverrides = {}, }) => {
442441
const closed = van.state(false);
443442
const { modalStyleOverrides, ...otherModalProps } = customModalProps;
@@ -471,7 +470,7 @@
471470
class: textFilterClass,
472471
style: toStyleStr(textFilterStyle),
473472
oninput: e => query.val = e.target.value
474-
}) : undefined;
473+
}) : null;
475474
const optionStyle = {
476475
padding: "0.5rem",
477476
...optionStyleOverrides,
@@ -480,25 +479,25 @@
480479
"background-color": selectedColor,
481480
...selectedStyleOverrides,
482481
};
483-
const id = "vanui-choose-" + (++chooseId);
484-
document.head.appendChild(van.tags["style"](`#${id} .vanui-choose-selected, #${id} .vanui-choose-option:hover { ${toStyleStr(selectedStyle)} }`));
485-
van.add(document.body, Modal(modalProps, div(label), showTextFilter ? div(textFilterDom) : undefined, () => div({ id, class: optionsContainerClass, style: toStyleStr(optionsContainerStyle) }, filtered.val.map((o, i) => div({
482+
van.add(document.head, () => closed.val ? null :
483+
van.tags["style"](`.vanui-choose-selected, .vanui-choose-option:hover { ${toStyleStr(selectedStyle)} }`));
484+
van.add(document.body, Modal(modalProps, div(label), showTextFilter ? div(textFilterDom) : null, () => div({ class: optionsContainerClass, style: toStyleStr(optionsContainerStyle) }, filtered.val.map((o, i) => div({
486485
class: () => ["vanui-choose-option"].concat(optionClass ? optionClass : [], i === index.val ? "vanui-choose-selected" : [], i === index.val && selectedClass ? selectedClass : []).join(" "),
487486
style: toStyleStr(optionStyle),
488487
onclick: () => (resolve(o), closed.val = true),
489488
}, o)))));
490489
textFilterDom?.focus();
491490
van.derive(() => {
492491
index.val;
493-
setTimeout(() => document.querySelector(`#${id} .vanui-choose-selected`)?.scrollIntoView(false), 10);
492+
setTimeout(() => document.querySelector(".vanui-choose-selected")?.scrollIntoView(false), 10);
494493
});
495494
const navByKey = (e) => {
496495
if (e.key === "Enter" && index.val < filtered.val.length) {
497496
resolve(filtered.val[index.val]);
498497
closed.val = true;
499498
}
500499
else if (e.key === "Escape") {
501-
resolve(undefined);
500+
resolve(null);
502501
closed.val = true;
503502
}
504503
else if (e.key === "ArrowDown")

0 commit comments

Comments
 (0)