react-cosmos-renderer 6.0.3 → 6.0.4-canary.2806ea7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,6 +4,10 @@ import { useSetValue } from './useSetValue.js';
4
4
  export function useSelect(selectName, args) {
5
5
  if (!args || !args.options || !args.options.length)
6
6
  throw new Error('No options provided to useSelect');
7
+ if (typeof args.options[0] === 'object') {
8
+ if (!args.options[0].options.length)
9
+ throw new Error('No options provided to useSelect');
10
+ }
7
11
  useCreateFixtureState(selectName, args);
8
12
  const currentValue = useCurrentValue(selectName, args);
9
13
  const setValue = useSetValue(selectName);
@@ -1,6 +1,11 @@
1
- export type UseSelectArgs<Option extends string> = {
1
+ type GroupedOptions<Option extends string> = {
2
+ group: string;
2
3
  options: Option[];
4
+ };
5
+ export type UseSelectArgs<Option extends string> = {
6
+ options: Option[] | GroupedOptions<Option>[];
3
7
  defaultValue?: Option;
4
8
  };
5
9
  export type SetSelectValue<Option extends string> = (value: Option) => void;
6
10
  export declare function getDefaultSelectValue<Option extends string>({ options, defaultValue, }: UseSelectArgs<Option>): Option;
11
+ export {};
@@ -1,3 +1,10 @@
1
1
  export function getDefaultSelectValue({ options, defaultValue, }) {
2
- return typeof defaultValue === 'string' ? defaultValue : options[0];
2
+ if (typeof defaultValue === 'string') {
3
+ return defaultValue;
4
+ }
5
+ const [firstOption] = options;
6
+ if (typeof firstOption === 'object') {
7
+ return firstOption.options[0];
8
+ }
9
+ return firstOption;
3
10
  }
@@ -25,5 +25,5 @@ export function useCreateFixtureState(selectName, args) {
25
25
  };
26
26
  });
27
27
  // eslint-disable-next-line react-hooks/exhaustive-deps
28
- }, [String(args.options), defaultValue, selectName, setFixtureState]);
28
+ }, [JSON.stringify(args.options), defaultValue, selectName, setFixtureState]);
29
29
  }
@@ -1,7 +1,6 @@
1
- import { FixtureStateData } from 'react-cosmos-core';
2
- import { SetValue } from './shared.js';
3
- type Opts<T extends FixtureStateData> = {
1
+ /// <reference types="react" />
2
+ type Opts<T> = {
4
3
  defaultValue: T;
5
4
  };
6
- export declare function useValue<T extends FixtureStateData>(inputName: string, { defaultValue }: Opts<T>): [T, SetValue<T>];
5
+ export declare function useValue<T>(inputName: string, { defaultValue }: Opts<T>): [T, React.Dispatch<React.SetStateAction<T>>];
7
6
  export {};
@@ -1,2 +1 @@
1
- import { FixtureStateData } from 'react-cosmos-core';
2
- export declare function useCreateFixtureState(inputName: string, defaultValue: FixtureStateData): void;
1
+ export declare function useCreateFixtureState(inputName: string, defaultValue: unknown): void;
@@ -1,2 +1 @@
1
- import { FixtureStateData } from 'react-cosmos-core';
2
- export declare function useCurrentValue<T extends FixtureStateData>(inputName: string, defaultValue: T): T;
1
+ export declare function useCurrentValue<T>(inputName: string, defaultValue: T): T;
@@ -1,10 +1,10 @@
1
- import { extendWithValue, } from 'react-cosmos-core';
1
+ import { extendWithValue } from 'react-cosmos-core';
2
2
  import { useFixtureState } from '../useFixtureState.js';
3
3
  export function useCurrentValue(inputName, defaultValue) {
4
4
  const [fixtureState] = useFixtureState('controls');
5
5
  const controlFs = fixtureState && fixtureState[inputName];
6
6
  return controlFs && controlFs.type === 'standard'
7
- ? // Types of fixture state values cannot be guaranteed at read time, which
7
+ ? // Types of fixture state values cannot be guaranteed at run time, which
8
8
  // means that tampering with the fixture state can cause runtime errors
9
9
  extendWithValue(defaultValue, controlFs.currentValue)
10
10
  : defaultValue;
@@ -1,3 +1,2 @@
1
- import { FixtureStateData } from 'react-cosmos-core';
2
- import { SetValue } from './shared.js';
3
- export declare function useSetValue<T extends FixtureStateData>(inputName: string, defaultValue: T): SetValue<T>;
1
+ import React from 'react';
2
+ export declare function useSetValue<T>(inputName: string, defaultValue: T): React.Dispatch<React.SetStateAction<T>>;
@@ -5,19 +5,21 @@ export function useSetValue(inputName, defaultValue) {
5
5
  const [, setFixtureState] = useFixtureState('controls');
6
6
  return React.useCallback(stateChange => {
7
7
  setFixtureState(prevFs => {
8
- const currentValue = typeof stateChange === 'function'
9
- ? stateChange(
10
- // Types of fixture state values cannot be guaranteed at read
11
- // time, which means that tampering with the fixture state can
12
- // cause runtime errors
13
- getCurrentValueFromFixtureState(prevFs, inputName, defaultValue))
14
- : stateChange;
8
+ // Types of fixture state values cannot be guaranteed at run
9
+ // time, which means that tampering with the fixture state can
10
+ // cause runtime errors
11
+ function getNewState() {
12
+ if (typeof stateChange !== 'function')
13
+ return stateChange;
14
+ const stateUpdater = stateChange;
15
+ return stateUpdater(getCurrentValueFromFixtureState(prevFs, inputName, defaultValue));
16
+ }
15
17
  return {
16
18
  ...prevFs,
17
19
  [inputName]: {
18
20
  type: 'standard',
19
21
  defaultValue: createValue(defaultValue),
20
- currentValue: createValue(currentValue),
22
+ currentValue: createValue(getNewState()),
21
23
  },
22
24
  };
23
25
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-cosmos-renderer",
3
- "version": "6.0.3",
3
+ "version": "6.0.4-canary.2806ea7.0+2806ea7",
4
4
  "description": "React Cosmos Renderer",
5
5
  "repository": "https://github.com/react-cosmos/react-cosmos/tree/main/packages/react-cosmos-renderer",
6
6
  "license": "MIT",
@@ -12,8 +12,8 @@
12
12
  },
13
13
  "dependencies": {
14
14
  "lodash-es": "4.17.21",
15
- "react-cosmos-core": "^6.0.1",
15
+ "react-cosmos-core": "6.0.4-canary.2806ea7.0+2806ea7",
16
16
  "react-is": "18.2.0"
17
17
  },
18
- "gitHead": "a046d36222582e9750fc23fee9eaeed146f44633"
18
+ "gitHead": "2806ea711b679d1f47726cdc1dc48065e7a3c1ee"
19
19
  }
@@ -1,3 +0,0 @@
1
- import React from 'react';
2
- import { FixtureStateData } from 'react-cosmos-core';
3
- export type SetValue<T extends FixtureStateData> = React.Dispatch<React.SetStateAction<T>>;
@@ -1 +0,0 @@
1
- export {};