Skip to content

Commit

Permalink
FixSelect支持BindValue。
Browse files Browse the repository at this point in the history
  • Loading branch information
kalxd committed Mar 17, 2024
1 parent 6f4a9fb commit dbe8a25
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
5 changes: 2 additions & 3 deletions site/src/page/dropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ const FixSelectS = (): m.Component => {
};

const fixAttr: FixSelectAttr<Item> = {
value: fixState.get(),
bindValue: fixState,
itemList: items,
renderItem: propOf("value"),
renderText: propOf("value"),
connectChange: fixState.set
renderText: propOf("value")
};

return m.fragment({}, [
Expand Down
16 changes: 7 additions & 9 deletions src/widget/dropdown/FixSelect.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,33 @@
import * as m from "mithril";
import { Just, Maybe } from "purify-ts";
import { ComponentPanic } from "../../data";
import { bindValue, BindValue } from "../../data/internal/value";
import { Select, SelectAttr } from "./Select";

const E = new ComponentPanic("FixSelect");

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

export const FixSelect = <T>(): m.Component<FixSelectAttr<T>> => ({
view: ({ attrs }) => {
const value = E.panicNil(attrs.value, "value不能为空!");
E.panicEmpty(attrs.itemList ?? [], "itemList不能为空!");
const mbindvalue = bindValue(attrs);

const mchangeE = Maybe.fromNullable(attrs.connectChange);
E.panicWhen(mbindvalue.value.isNothing(), "value不能为空!");
E.panicEmpty(attrs.itemList ?? [], "itemList不能为空!");

const menuAttr: SelectAttr<T> = {
value: Just(value),
value: mbindvalue.value,
placeholder: attrs.placeholder,
itemList: attrs.itemList,
renderItem: attrs.renderItem,
renderText: attrs.renderText,
isShowRemoveIcon: false,
connectChange: item =>
item.ap(mchangeE)
item.ifJust(mbindvalue.connectChange)
};

return m<SelectAttr<T>, {}>(Select, menuAttr);
Expand Down

0 comments on commit dbe8a25

Please sign in to comment.