Skip to content

Commit

Permalink
fix: randomize state
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Jul 28, 2020
1 parent d1543b6 commit 8e74f55
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 22 deletions.
2 changes: 1 addition & 1 deletion core/store/src/serialization/BroadcastStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface MessageType {
}

export class BroadcastStore implements Store {
config: RunConfiguration | undefined;
config: RunConfiguration = {};
docs: Documents = {};
stories: StoreStories = {};
components: StoreComponents = {};
Expand Down
19 changes: 10 additions & 9 deletions core/store/src/state/context/controls.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import React, { FC, createContext, useContext } from 'react';
import { ComponentControls, ComponentControl } from '@component-controls/core';
import {
ComponentControls,
ComponentControl,
mergeControlValues,
} from '@component-controls/core';

import { StoryContext, useStoryById } from './story';

export type ControlUpdateFn = (name: string, newValue: any) => void;
export type ControlUpdateFn = (name: string | undefined, newValue: any) => void;

export interface ControlsContextProps {
controls?: ComponentControls;
Expand All @@ -19,15 +23,12 @@ export const ControlsContextStoryProvider: FC = ({ children }) => {
<ControlsContext.Provider
value={{
controls: story ? story.controls : undefined,
updateValue: (name: string, newValue: any) => {
updateValue: (name: string | undefined, newValue: any) => {
if (story) {
const storyControls = story.controls || {};
updateStory({
...story,
controls: {
...storyControls,
[name]: { ...storyControls[name], value: newValue },
},
controls: mergeControlValues(storyControls, name, newValue),
});
}
},
Expand Down Expand Up @@ -67,10 +68,10 @@ export const ControlsStateProvider: FC<ControlsStateProviderProps> = ({
};

export const useControl = <T extends ComponentControl>(
name: string,
name?: string,
): [T, (value: any) => void] => {
const { controls, updateValue } = useContext(ControlsContext);
const control = controls ? controls[name] : undefined;
const control = controls ? (name ? controls[name] : controls) : undefined;
const setValue = (value: any) => {
updateValue(name, value);
};
Expand Down
14 changes: 9 additions & 5 deletions core/store/src/state/recoil/controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import {
useRecoilState,
RecoilRoot,
} from 'recoil';
import { ComponentControls, ComponentControl } from '@component-controls/core';
import {
ComponentControls,
ComponentControl,
mergeControlValues,
} from '@component-controls/core';

import { currentStoryState } from './story';
import { storeState } from './store';
Expand Down Expand Up @@ -40,12 +44,12 @@ export const useStoryControls = (
): ComponentControls | undefined => useRecoilValue(storyControlsState(storyId));

export const useControl = <T extends ComponentControl>(
name: string,
name?: string,
): [T, (value: any) => void] => {
const [controls, setControls] = useRecoilState(currentControlsState);
const control = controls ? controls[name] : undefined;
const setValue = (value: any) => {
setControls({ ...controls, [name]: value });
const control = controls ? (name ? controls[name] : controls) : undefined;
const setValue = (newValue: any) => {
setControls(mergeControlValues(controls || {}, name, newValue));
};
return [control as T, setValue];
};
Expand Down
10 changes: 10 additions & 0 deletions ui/blocks/src/PropsTable/BasePropsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ComponentInfo,
visibleControls,
} from '@component-controls/core';
import { useControl } from '@component-controls/store';

import {
Table,
Expand Down Expand Up @@ -53,6 +54,7 @@ export const BasePropsTable: FC<BasePropsTableProps> = ({
const [copied, setCopied] = useState(false);
const story = useCurrentStory();
const info: Partial<ComponentInfo> = component.info || {};
const [, setControlValue] = useControl();
const controls = useMemo(() => {
const storyControls = visibility !== 'info' ? story?.controls || {} : {};
return visibleControls(storyControls, story);
Expand Down Expand Up @@ -323,6 +325,14 @@ export const BasePropsTable: FC<BasePropsTableProps> = ({
useControlsActions({
controls,
storyId: story?.id,
setControlValue: (
storyId: string,
propertyName: string | undefined,
value: any,
) => {
setControlValue(value);
console.log(storyId, propertyName, value);
},
}),
);
return <ActionContainer actions={actions}>{table}</ActionContainer>;
Expand Down
17 changes: 11 additions & 6 deletions ui/editors/src/ArrayEditor/ArrayEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ interface RowEditorProps {
control: ComponentControlArray;
row: any;
Editor: PropertyEditor;
onChange: (name: string, value: any) => void;
onChange: (name: string | undefined, value: any) => void;
}
const RowEditor: FC<RowEditorProps> = ({
propName,
Expand Down Expand Up @@ -83,11 +83,15 @@ export const ArrayEditor: PropertyEditor<ArrayEditorProps> = ({
const [control, setProp] = useControl<ComponentControlArray>(name);
const { editLabel: controlEditLabel } = control;
const [isOpen, setIsOpen] = React.useState(false);
const handleChange = (rowIndex: number, propName: string, value: any) => {
const handleChange = (
rowIndex: number,
propName: string | undefined,
value: any,
) => {
if (Array.isArray(control.value)) {
setProp(
control.value.map((row, index) =>
rowIndex === index ? { ...row, [propName]: value } : row,
rowIndex === index ? { ...row, [propName as string]: value } : row,
),
);
}
Expand Down Expand Up @@ -142,9 +146,10 @@ export const ArrayEditor: PropertyEditor<ArrayEditorProps> = ({
propName={key}
row={row}
Editor={Editor}
onChange={(propName: string, value: any) =>
handleChange(idx, propName, value)
}
onChange={(
propName: string | undefined,
value: any,
) => handleChange(idx, propName, value)}
/>
</Flex>
</td>
Expand Down
2 changes: 1 addition & 1 deletion ui/editors/src/ObjectEditor/ObjectEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const ObjectEditor: PropertyEditor<ObjectEditorProps> = ({

const { editLabel: controlEditLabel } = control;
const [isOpen, setIsOpen] = React.useState(false);
const handleChange = (childName: string, value: any) => {
const handleChange = (childName: string | undefined, value: any) => {
onChange(mergeControlValues(control.value as any, childName, value));
};
let children: ({
Expand Down

0 comments on commit 8e74f55

Please sign in to comment.