Skip to content

Commit 95ad078

Browse files
committed
allow null to be used as a valid initial input
1 parent f000c3f commit 95ad078

File tree

3 files changed

+5
-16
lines changed

3 files changed

+5
-16
lines changed

packages/plexus-core/src/state.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ export type PlexusState = <Value extends PlexusValidStateTypes = any>(
1717
input: Value
1818
) => StateInstance<Value>
1919

20-
type DestroyFn = () => void
21-
2220
export interface StateStore {
2321
_name: string
2422
_persist: boolean
@@ -329,9 +327,6 @@ export function _state<StateValue>(
329327
* @param item The default value to use when we generate the state
330328
* @returns A Plexus State Instance
331329
*/
332-
export function state<
333-
Override extends PlexusValidStateTypes = never,
334-
Value = Override extends AlmostAnything ? Override : any
335-
>(item: Value) {
336-
return _state(() => instance(), item)
330+
export function state<Override = never, Value = Override>(init: Value | null) {
331+
return _state(() => instance(), init as NonNullable<Value>)
337332
}

tests/react.test.tsx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,15 @@ import { render, act } from './test-utils'
55
import { collection, computed, instance, state, event } from '@plexusjs/core'
66
import React, { FC, useEffect, useState } from 'react'
77
import { useDeposit, usePlexusEvent, usePlexus } from '@plexusjs/react'
8-
// import * as renderer from "react-test-renderer"
98

10-
// function toJson(component: renderer.ReactTestRenderer) {
11-
// const result = component.toJSON()
12-
// expect(result).toBeDefined()
13-
// expect(result).not.toBeInstanceOf(Array)
14-
// return result as renderer.ReactTestRendererJSON
15-
// }
169

1710
type Payload = {
1811
name: string
1912
status: string
2013
}
2114

2215
const myEvents = event<Payload>()
23-
const stringState = state<string>('yes')
16+
const stringState = state('yes')
2417
const numberState = state(1)
2518
const objectState = state<Partial<{ name: string }>>({ name: 'test' })
2619
instance({ logLevel: 'debug' })
@@ -194,7 +187,7 @@ describe('Test react integration (useDeposit)', () => {
194187
const { value, save, edit } = useDeposit(
195188
{ ...def },
196189
{
197-
onEdit(k, v) {},
190+
onEdit(k, v) { },
198191
onSave(payload) {
199192
setVal(payload.name ?? '')
200193
},

tests/test-utils.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export const initialStateValues = {
5656
}
5757
export const booleanState = state(true)
5858
export const stringState = state('Hello Plexus!')
59+
export const stringStateStartingWithNull = state<string>(null)
5960
export const objectState = state<ObjectStateExample>(initialStateValues.object)
6061
export const arrayState = state<{ item?: string; item2?: { subitem?: string } }[]>(
6162
initialStateValues.array

0 commit comments

Comments
 (0)