|
| 1 | +import { Bindable, BindableFn, BindableParams } from "@zag-js/core" |
| 2 | +import { useMachine } from "@zag-js/react" |
| 3 | +import { isFunction } from "@zag-js/utils" |
| 4 | + |
| 5 | +interface ValidateParams<T> { |
| 6 | + validate?: (b: T) => boolean |
| 7 | + defaultValid?: boolean |
| 8 | + onValidityChange?: (props: { valid: boolean; lastValid: T }) => void |
| 9 | +} |
| 10 | + |
| 11 | +function withLastValid<T>(bindable: BindableFn, props: () => BindableParams<T> & ValidateParams<T>): Bindable<T> { |
| 12 | + const value = bindable(props) |
| 13 | + |
| 14 | + const lastValidValue = bindable(() => ({ |
| 15 | + defaultValue: props().validate?.(value.get()) ? value.get() : undefined, |
| 16 | + })) |
| 17 | + |
| 18 | + const valid = bindable<boolean>(() => ({ |
| 19 | + defaultValue: props().defaultValid, |
| 20 | + onChange(valid) { |
| 21 | + props().onValidityChange?.({ valid, lastValid: lastValidValue.get() }) |
| 22 | + }, |
| 23 | + })) |
| 24 | + |
| 25 | + return { |
| 26 | + ...value, |
| 27 | + set(valueOrFn) { |
| 28 | + const nextValue = isFunction(valueOrFn) ? valueOrFn(value.get()) : valueOrFn |
| 29 | + if (props().validate?.(nextValue)) { |
| 30 | + lastValidValue.set(nextValue) |
| 31 | + valid.set(true) |
| 32 | + } else { |
| 33 | + valid.set(false) |
| 34 | + } |
| 35 | + value.set(nextValue) |
| 36 | + }, |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +function withAutoreset<T>(bindable: BindableFn, props: () => BindableParams<T> & { resetAfter: number }): Bindable<T> { |
| 41 | + const value = bindable(props) |
| 42 | + |
| 43 | + const timer = bindable.ref<NodeJS.Timeout | undefined>(undefined) |
| 44 | + |
| 45 | + const resetValue = () => |
| 46 | + setTimeout(() => { |
| 47 | + value.set(value.initial) |
| 48 | + }, props().resetAfter) |
| 49 | + |
| 50 | + bindable.cleanup(() => { |
| 51 | + if (timer.get()) clearTimeout(timer.get()!) |
| 52 | + timer.set(undefined) |
| 53 | + }) |
| 54 | + |
| 55 | + return { |
| 56 | + ...value, |
| 57 | + set(valueOrFn) { |
| 58 | + value.set(valueOrFn) |
| 59 | + const currentTimer = timer.get() |
| 60 | + if (currentTimer) clearTimeout(currentTimer) |
| 61 | + timer.set(resetValue()) |
| 62 | + }, |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +export default function Page() { |
| 67 | + const service = useMachine({ |
| 68 | + initialState() { |
| 69 | + return "idle" |
| 70 | + }, |
| 71 | + |
| 72 | + context({ bindable }) { |
| 73 | + return { |
| 74 | + count: withLastValid<string>(bindable, () => ({ |
| 75 | + defaultValue: "hello", |
| 76 | + validate(value) { |
| 77 | + return value.length > 3 |
| 78 | + }, |
| 79 | + onValidityChange(valid) { |
| 80 | + console.log("valid", valid) |
| 81 | + }, |
| 82 | + })), |
| 83 | + index: withAutoreset<number>(bindable, () => ({ |
| 84 | + defaultValue: 0, |
| 85 | + resetAfter: 1000, |
| 86 | + })), |
| 87 | + } |
| 88 | + }, |
| 89 | + |
| 90 | + states: { |
| 91 | + idle: { |
| 92 | + on: { |
| 93 | + CLICK: { target: "active" }, |
| 94 | + }, |
| 95 | + }, |
| 96 | + }, |
| 97 | + }) |
| 98 | + return ( |
| 99 | + <main> |
| 100 | + <div>{service.context.get("count")}</div> |
| 101 | + <button onClick={() => service.context.set("count", "df")}>Click</button> |
| 102 | + <hr /> |
| 103 | + <div>{service.context.get("index")}</div> |
| 104 | + <button onClick={() => service.context.set("index", (prev) => prev + 1)}>Click</button> |
| 105 | + </main> |
| 106 | + ) |
| 107 | +} |
0 commit comments