Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/ when switching to input mode from a referenced Border token, all… #2850

Merged
merged 2 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/polite-impalas-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tokens-studio/figma-plugin": patch
---

When editing a border token and switching from reference to input mode we now populate the contents.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import get from 'just-safe-get';
import { TokensIcon, LinkBreak2Icon } from '@radix-ui/react-icons';
import { useTranslation } from 'react-i18next';
import { IconButton, Heading } from '@tokens-studio/ui';
import { EditTokenObject } from '@/types/tokens';
import { EditTokenObject, SingleBorderToken } from '@/types/tokens';
import { TokenTypes } from '@/constants/TokenTypes';
import { ResolveTokenValuesResult } from '@/utils/tokenHelpers';
import Stack from './Stack';
Expand All @@ -28,6 +28,7 @@ export default function BorderTokenForm({
handleBorderValueDownShiftInputChange,
handleBorderAliasValueChange,
handleDownShiftInputChange,
setBorderValue,
onSubmit,
}: {
internalEditToken: Extract<EditTokenObject, { type: TokenTypes.BORDER }>;
Expand All @@ -36,61 +37,65 @@ export default function BorderTokenForm({
handleBorderValueDownShiftInputChange: (newInputValue: string, property: string) => void;
handleBorderAliasValueChange: (property: string, value: string) => void;
handleDownShiftInputChange: (newInputValue: string) => void;
onSubmit: () => void
setBorderValue: (newBorderValue: SingleBorderToken['value']) => void;
onSubmit: () => void;
}) {
const seed = useUIDSeed();
const isAliasMode = (internalEditToken.value && typeof internalEditToken.value === 'string');
const isAliasMode = internalEditToken.value && typeof internalEditToken.value === 'string';
const [mode, setMode] = useState(isAliasMode ? 'alias' : 'input');
const [alias, setAlias] = useState('');
const { t } = useTranslation(['tokens']);
const [inputHelperOpen, setInputHelperOpen] = useState<boolean>(false);

const selectedToken = React.useMemo(() => {
const selectedToken = React.useMemo<SingleBorderToken | null>(() => {
const search = findReferences(String(internalEditToken.value));
if (search && search.length > 0) {
const foundToken = resolvedTokens.find((t) => t.name === search[0]);
if (foundToken) return foundToken;
if (foundToken) return foundToken as SingleBorderToken;
}
return null;
}, [internalEditToken, resolvedTokens]);

const handleToggleInputHelper = React.useCallback(() => setInputHelperOpen(!inputHelperOpen), [inputHelperOpen]);
const onColorChange = React.useCallback((color: string) => {
handleBorderValueDownShiftInputChange(color, 'color');
}, [handleBorderValueDownShiftInputChange]);
const onColorChange = React.useCallback(
(color: string) => {
handleBorderValueDownShiftInputChange(color, 'color');
},
[handleBorderValueDownShiftInputChange],
);

const handleMode = React.useCallback(() => {
const changeMode = (mode === 'input') ? 'alias' : 'input';
setMode(changeMode);
if (mode === 'alias' && typeof internalEditToken.value === 'string') {
setBorderValue(selectedToken?.rawValue ?? {});
}
setMode(mode === 'input' ? 'alias' : 'input');
setAlias('');
}, [mode]);
}, [mode, selectedToken, internalEditToken, setBorderValue]);
Comment on lines +68 to +73
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pertinent change in this file. Lots of prettier formatting in this file as well


return (
<Stack direction="column" gap={2}>
<Stack direction="row" gap={2} justify="between" align="center">
<Heading>{t('value')}</Heading>
{
mode === 'input' ? (
<IconButton
tooltip={t('reference-mode')}
data-testid="mode-change-button"
onClick={handleMode}
icon={<TokensIcon />}
/>
) : (
<IconButton
tooltip={t('input-mode')}
data-testid="mode-change-button"
onClick={handleMode}
icon={<LinkBreak2Icon />}
/>
)
}
{mode === 'input' ? (
<IconButton
tooltip={t('reference-mode')}
data-testid="mode-change-button"
onClick={handleMode}
icon={<TokensIcon />}
/>
) : (
<IconButton
tooltip={t('input-mode')}
data-testid="mode-change-button"
onClick={handleMode}
icon={<LinkBreak2Icon />}
/>
)}
</Stack>
{(mode === 'input' && internalEditToken.schema.schemas.value.type === 'object') ? (
{mode === 'input' && internalEditToken.schema.schemas.value.type === 'object' ? (
<Stack gap={2} direction="column">
{Object.entries(internalEditToken.schema.schemas.value.properties ?? {}).map(([key], keyIndex) => (
<>
<React.Fragment key={`border-input-fragment-${seed(keyIndex)}`}>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was a react child unique key error, hence I replaced the blank fragment with a React Fragment(with a key)

<BorderTokenDownShiftInput
name={key}
key={`border-input-${seed(keyIndex)}`}
Expand All @@ -103,9 +108,12 @@ export default function BorderTokenForm({
onSubmit={onSubmit}
/>
{inputHelperOpen && key === 'color' && (
<ColorPicker value={typeof internalEditToken.value === 'object' && get(internalEditToken.value, key, '')} onChange={onColorChange} />
<ColorPicker
value={typeof internalEditToken.value === 'object' && get(internalEditToken.value, key, '')}
onChange={onColorChange}
/>
)}
</>
</React.Fragment>
))}
</Stack>
) : (
Expand All @@ -124,12 +132,11 @@ export default function BorderTokenForm({
onSubmit={onSubmit}
/>

{isAliasMode && typeof internalEditToken.value === 'string' && checkIfContainsAlias(internalEditToken.value) && (
<ResolvedTokenDisplay
alias={alias}
selectedToken={selectedToken}
/>
)}
{isAliasMode &&
typeof internalEditToken.value === 'string' &&
checkIfContainsAlias(internalEditToken.value) && (
<ResolvedTokenDisplay alias={alias} selectedToken={selectedToken} />
)}
</Stack>
)}
</Stack>
Expand Down
Loading
Loading