From 5ee9a2543ff5c61143aa538e1b46ee3d65d94c71 Mon Sep 17 00:00:00 2001 From: Cole Lawrence Date: Mon, 23 May 2022 12:46:30 -0400 Subject: [PATCH] playground(dom): Add nested compound prop "favoriteFood" --- packages/playground/src/shared/dom/Scene.tsx | 52 +++++++++++++------- 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/packages/playground/src/shared/dom/Scene.tsx b/packages/playground/src/shared/dom/Scene.tsx index 6221556..3b32743 100644 --- a/packages/playground/src/shared/dom/Scene.tsx +++ b/packages/playground/src/shared/dom/Scene.tsx @@ -5,6 +5,7 @@ import React, {useLayoutEffect, useMemo, useState} from 'react' import type {IProject, ISheet} from '@theatre/core' import {onChange, types} from '@theatre/core' import type {IScrub, IStudio} from '@theatre/studio' +import type {ShorthandCompoundPropsToInitialValue} from '@theatre/core/propTypes/internals' studio.initialize({usePersistentStorage: false}) @@ -22,11 +23,43 @@ const boxObjectConfig = { test: types.string('Hello?', {interpolate: textInterpolate}), testLiteral: types.stringLiteral('a', {a: 'Option A', b: 'Option B'}), bool: types.boolean(false), + favoriteFood: types.compound({ + name: types.string('Pie'), + // if needing more compounds, consider adding weight with different units + price: types.compound({ + currency: types.stringLiteral('USD', {USD: 'USD', EUR: 'EUR'}), + amount: types.number(10, {range: [0, 1000], label: '$'}), + }), + }), x: types.number(200), y: types.number(200), color: types.rgba({r: 1, g: 0, b: 0, a: 1}), } +// this can also be inferred with +type _State = ShorthandCompoundPropsToInitialValue +type State = { + x: number + y: number + test: string + testLiteral: string + bool: boolean + // a compound compound prop + favoriteFood: { + name: string + price: { + amount: number + currency: string + } + } + color: { + r: number + g: number + b: number + a: number + } +} + const Box: React.FC<{ id: string sheet: ISheet @@ -37,19 +70,7 @@ const Box: React.FC<{ const isSelected = selection.includes(obj) - const [state, setState] = useState<{ - x: number - y: number - test: string - testLiteral: string - bool: boolean - color: { - r: number - g: number - b: number - a: number - } - }>(obj.value) + const [state, setState] = useState(obj.value) useLayoutEffect(() => { const unsubscribeFromChanges = onChange(obj.props, (newValues) => { @@ -77,12 +98,9 @@ const Box: React.FC<{ } scrub!.capture(({set}) => { set(obj.props, { + ...initial, x: x + initial.x, y: y + initial.y, - test: initial.test, - testLiteral: initial.testLiteral, - bool: initial.bool, - color: initial.color, }) }) },