Skip to content

Commit c630afb

Browse files
committed
[state] Re-use in components
1 parent ee772b6 commit c630afb

File tree

3 files changed

+27
-43
lines changed

3 files changed

+27
-43
lines changed
Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
import type {Cell} from '../@types/index.d.ts';
21
import type {EditableCellView as EditableCellViewDecl} from '../@types/ui-react-dom/index.d.ts';
32
import type {CellProps} from '../@types/ui-react/index.d.ts';
43
import {CELL} from '../common/strings.ts';
5-
import {
6-
useCell,
7-
useSetCellCallback,
8-
useStoreOrStoreById,
9-
} from '../ui-react/hooks.ts';
4+
import {useCellState, useStoreOrStoreById} from '../ui-react/hooks.ts';
105
import {EditableThing} from './common/components.tsx';
116
import {EDITABLE} from './common/index.tsx';
127

@@ -17,19 +12,15 @@ export const EditableCellView: typeof EditableCellViewDecl = ({
1712
store,
1813
className,
1914
showType,
20-
}: CellProps & {readonly className?: string; readonly showType?: boolean}) => (
21-
<EditableThing
22-
thing={useCell(tableId, rowId, cellId, store)}
23-
onThingChange={useSetCellCallback(
24-
tableId,
25-
rowId,
26-
cellId,
27-
(cell: Cell) => cell,
28-
[],
29-
store,
30-
)}
31-
className={className ?? EDITABLE + CELL}
32-
showType={showType}
33-
hasSchema={useStoreOrStoreById(store)?.hasTablesSchema}
34-
/>
35-
);
15+
}: CellProps & {readonly className?: string; readonly showType?: boolean}) => {
16+
const [cell, setCell] = useCellState(tableId, rowId, cellId, store);
17+
return (
18+
<EditableThing
19+
thing={cell}
20+
onThingChange={setCell}
21+
className={className ?? EDITABLE + CELL}
22+
showType={showType}
23+
hasSchema={useStoreOrStoreById(store)?.hasTablesSchema}
24+
/>
25+
);
26+
};
Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
import type {Value} from '../@types/index.d.ts';
21
import type {EditableValueView as EditableValueViewDecl} from '../@types/ui-react-dom/index.d.ts';
32
import type {ValueProps} from '../@types/ui-react/index.d.ts';
43
import {VALUE} from '../common/strings.ts';
5-
import {
6-
useSetValueCallback,
7-
useStoreOrStoreById,
8-
useValue,
9-
} from '../ui-react/hooks.ts';
4+
import {useStoreOrStoreById, useValueState} from '../ui-react/hooks.ts';
105
import {EditableThing} from './common/components.tsx';
116
import {EDITABLE} from './common/index.tsx';
127

@@ -15,17 +10,15 @@ export const EditableValueView: typeof EditableValueViewDecl = ({
1510
store,
1611
className,
1712
showType,
18-
}: ValueProps & {readonly className?: string; readonly showType?: boolean}) => (
19-
<EditableThing
20-
thing={useValue(valueId, store)}
21-
onThingChange={useSetValueCallback(
22-
valueId,
23-
(value: Value) => value,
24-
[],
25-
store,
26-
)}
27-
className={className ?? EDITABLE + VALUE}
28-
showType={showType}
29-
hasSchema={useStoreOrStoreById(store)?.hasValuesSchema}
30-
/>
31-
);
13+
}: ValueProps & {readonly className?: string; readonly showType?: boolean}) => {
14+
const [value, setValue] = useValueState(valueId, store);
15+
return (
16+
<EditableThing
17+
thing={value}
18+
onThingChange={setValue}
19+
className={className ?? EDITABLE + VALUE}
20+
showType={showType}
21+
hasSchema={useStoreOrStoreById(store)?.hasValuesSchema}
22+
/>
23+
);
24+
};

src/ui-react-dom/common/components.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ export const EditableThing = <Thing extends Cell | Value>({
136136
showType = true,
137137
}: {
138138
readonly thing: Thing | undefined;
139-
readonly onThingChange: (thing: Thing | undefined) => void;
139+
readonly onThingChange: (thing: Thing) => void;
140140
readonly className: string;
141141
readonly hasSchema: (() => boolean) | undefined;
142142
readonly showType?: boolean;

0 commit comments

Comments
 (0)