Skip to content

Commit

Permalink
MultiSelect支持BindValue。
Browse files Browse the repository at this point in the history
  • Loading branch information
kalxd committed Mar 17, 2024
1 parent dbe8a25 commit 110c447
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
3 changes: 1 addition & 2 deletions site/src/page/dropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,11 @@ const MultSelectS = (): m.Component => {
return {
view: () => {
const attr: MultiSelectAttr<Item> = {
value: state.get(),
bindValue: state,
itemList: items,
renderText: item => item.value,
renderItem: item => item.value,
placeholder: "来选一个",
connectChange: state.set
};

return m(MultiSelect, attr);
Expand Down
19 changes: 9 additions & 10 deletions src/widget/dropdown/MultiSelect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { mutable } from "../../data";
import { Outter, OutterAttr } from "../../abstract/outter";
import { AnimateFrame } from "../../abstract/animate";
import { pickKlass, selectKlass } from "../../data/internal/attr";
import { bindValue, BindValue } from "../../data/internal/value";

interface TextAttr<T> {
value: Array<T>;
Expand Down Expand Up @@ -68,15 +69,13 @@ const MultiSelectMenu = <T>(): m.Component<MultiSelectMenuAttr<T>> => ({
}
});

export interface MultiSelectAttr<T> {
value?: Array<T>;
export interface MultiSelectAttr<T> extends BindValue<Array<T>> {
itemList?: Array<T>;
placeholder?: string;
compareEq?: (value: T, item: T) => boolean;
renderText?: (value: T) => m.Children;
renderItem?: (item: T) => m.Children;
isFluid?: boolean;
connectChange?: (value: Array<T>) => void;
}

export const MultiSelect = <T>(): m.Component<MultiSelectAttr<T>> => {
Expand All @@ -93,8 +92,7 @@ export const MultiSelect = <T>(): m.Component<MultiSelectAttr<T>> => {

return {
view: ({ attrs }) => {
const mvalue = Maybe.fromNullable(attrs.value);
const mchangeE = Maybe.fromNullable(attrs.connectChange);
const mbindvalue = bindValue(attrs);

const dropdownAttr: m.Attributes = {
class: pickKlass([
Expand All @@ -105,12 +103,12 @@ export const MultiSelect = <T>(): m.Component<MultiSelectAttr<T>> => {
};

const textAttr: TextAttr<T> = {
value: mvalue.orDefault([]),
value: mbindvalue.value.orDefault([]),
placeholder: attrs.placeholder,
renderText: attrs.renderText ?? String,
connectRemove: index => {
mvalue.map(xs => xs.filter((_, i) => i !== index))
.ap(mchangeE);
mbindvalue.value.map(xs => xs.filter((_, i) => i !== index))
.ifJust(mbindvalue.connectChange);
state.set(true);
}
};
Expand All @@ -120,15 +118,16 @@ export const MultiSelect = <T>(): m.Component<MultiSelectAttr<T>> => {
.filter(state.get)
.map(xs => {
const submenuItemList = xs.filter(item => {
return mvalue.map(value => !value.some(x => compareItem(x, item)))
return mbindvalue.value.map(value => !value.some(x => compareItem(x, item)))
.orDefault(true);
});

const menuAttr: MultiSelectMenuAttr<T> = {
itemList: submenuItemList,
renderItem: attrs.renderItem ?? String,
connectClickItem: item => {
mvalue.map(xs => ([...xs, item])).ap(mchangeE);
mbindvalue.value.map(xs => ([...xs, item]))
.ifJust(mbindvalue.connectChange);
}
};
return m<MultiSelectMenuAttr<T>, {}>(MultiSelectMenu, menuAttr);
Expand Down

0 comments on commit 110c447

Please sign in to comment.