mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Fix metamask-translation component for new Storybook format (#12892)
* metamask-translation * Replacing broken ArgsTable with manual markdown table * Grammer fix Co-authored-by: georgewrmarshall <george.marshall@consensys.net>
This commit is contained in:
parent
b705bbc809
commit
1794ee8b72
36
ui/components/app/metamask-translation/README.mdx
Normal file
36
ui/components/app/metamask-translation/README.mdx
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
import { Story, Canvas, ArgsTable } from '@storybook/addon-docs';
|
||||||
|
|
||||||
|
import MetaMaskTranslation from '.';
|
||||||
|
|
||||||
|
# MetaMask Translation
|
||||||
|
|
||||||
|
MetaMaskTranslation is a simple helper component for adding full translation
|
||||||
|
support to the template system. We do pass the translation function to the
|
||||||
|
template getValues function, but passing it React components as variables
|
||||||
|
would require React to be in scope, and breaks the object pattern paradigm.
|
||||||
|
|
||||||
|
This component gets around that by converting variables that are templates
|
||||||
|
themselves into tiny React trees. This component does additional validation
|
||||||
|
to make sure that the tree has a single root node, with maximum two leaves.
|
||||||
|
Each subnode can have a maximum of one child that must be a string.
|
||||||
|
|
||||||
|
This enforces a maximum recursion depth of 2, preventing translation strings
|
||||||
|
from being performance hogs. We could further limit this, and also attenuate
|
||||||
|
the safeComponentList for what kind of components we allow these special
|
||||||
|
trees to contain.
|
||||||
|
|
||||||
|
<Canvas>
|
||||||
|
<Story id="ui-components-app-metamask-translation-metamask-translation-stories-js--default-story" />
|
||||||
|
</Canvas>
|
||||||
|
|
||||||
|
## Component API
|
||||||
|
|
||||||
|
<!--
|
||||||
|
ArgsTable doesn't work with SectionShape
|
||||||
|
<ArgsTable of={MetaMaskTranslation} />
|
||||||
|
-->
|
||||||
|
|
||||||
|
| Name | Description |
|
||||||
|
| ---------------- | ------------------------------------------------------------------------------------------------------- |
|
||||||
|
| `translationKey` | Translation object key `string` |
|
||||||
|
| `variables` | Array of variables for the MetaMaskTranslation. Can be an array of `string`, `number` or `SectionShape` |
|
@ -6,7 +6,7 @@ import MetaMaskTemplateRenderer, {
|
|||||||
} from '../metamask-template-renderer/metamask-template-renderer';
|
} from '../metamask-template-renderer/metamask-template-renderer';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* MetaMaskTranslation is a simple helper Component for adding full translation
|
* MetaMaskTranslation is a simple helper component for adding full translation
|
||||||
* support to the template system. We do pass the translation function to the
|
* support to the template system. We do pass the translation function to the
|
||||||
* template getValues function, but passing it React components as variables
|
* template getValues function, but passing it React components as variables
|
||||||
* would require React to be in scope, and breaks the object pattern paradigm.
|
* would require React to be in scope, and breaks the object pattern paradigm.
|
||||||
@ -69,7 +69,13 @@ export default function MetaMaskTranslation({ translationKey, variables }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
MetaMaskTranslation.propTypes = {
|
MetaMaskTranslation.propTypes = {
|
||||||
|
/**
|
||||||
|
* Translation object key
|
||||||
|
*/
|
||||||
translationKey: PropTypes.string.isRequired,
|
translationKey: PropTypes.string.isRequired,
|
||||||
|
/**
|
||||||
|
* Array of variables for the MetaMaskTranslation component
|
||||||
|
*/
|
||||||
variables: PropTypes.arrayOf(
|
variables: PropTypes.arrayOf(
|
||||||
PropTypes.oneOfType([
|
PropTypes.oneOfType([
|
||||||
PropTypes.string,
|
PropTypes.string,
|
||||||
|
@ -1,67 +1,47 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { select, object } from '@storybook/addon-knobs';
|
|
||||||
import { groupBy } from 'lodash';
|
import { groupBy } from 'lodash';
|
||||||
import en from '../../../../app/_locales/en/messages.json';
|
import en from '../../../../app/_locales/en/messages.json';
|
||||||
|
import README from './README.mdx';
|
||||||
import MetaMaskTranslation from './metamask-translation';
|
import MetaMaskTranslation from './metamask-translation';
|
||||||
|
|
||||||
|
const { keysWithoutSubstitution } = groupBy(Object.keys(en), (key) => {
|
||||||
|
if (en[key].message.includes('$1')) {
|
||||||
|
return 'keysWithSubstitution';
|
||||||
|
}
|
||||||
|
return 'keysWithoutSubstitution';
|
||||||
|
});
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
title: 'Components/App/MetamaskTranslation',
|
title: 'Components/App/MetamaskTranslation',
|
||||||
id: __filename,
|
id: __filename,
|
||||||
|
component: MetaMaskTranslation,
|
||||||
|
parameters: {
|
||||||
|
docs: {
|
||||||
|
page: README,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
argTypes: {
|
||||||
|
translationKey: { options: keysWithoutSubstitution, control: 'select' },
|
||||||
|
variables: { control: 'array' },
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const { keysWithSubstitution, keysWithoutSubstitution } = groupBy(
|
export const DefaultStory = (args) => {
|
||||||
Object.keys(en),
|
return <MetaMaskTranslation {...args} />;
|
||||||
(key) => {
|
};
|
||||||
if (en[key].message.includes('$1')) {
|
|
||||||
return 'keysWithSubstitution';
|
|
||||||
}
|
|
||||||
return 'keysWithoutSubstitution';
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
export const WithoutSubstitutions = () => (
|
DefaultStory.storyName = 'Default';
|
||||||
|
DefaultStory.args = {
|
||||||
|
translationKey: keysWithoutSubstitution[0],
|
||||||
|
};
|
||||||
|
|
||||||
|
export const WithTemplate = (args) => (
|
||||||
<MetaMaskTranslation
|
<MetaMaskTranslation
|
||||||
translationKey={select(
|
{...args}
|
||||||
'translationKey',
|
variables={[<h1 key="link">{args.translationKey}</h1>]}
|
||||||
keysWithoutSubstitution,
|
|
||||||
keysWithoutSubstitution[0],
|
|
||||||
)}
|
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
|
||||||
export const WithSubstitutions = () => (
|
WithTemplate.args = {
|
||||||
<MetaMaskTranslation
|
translationKey: keysWithoutSubstitution[0],
|
||||||
translationKey={select(
|
};
|
||||||
'translationKey',
|
|
||||||
keysWithSubstitution,
|
|
||||||
keysWithSubstitution[0],
|
|
||||||
)}
|
|
||||||
variables={object('variables', [])}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
|
|
||||||
export const WithTemplate = () => (
|
|
||||||
<MetaMaskTranslation
|
|
||||||
translationKey={select(
|
|
||||||
'translationKey',
|
|
||||||
keysWithSubstitution,
|
|
||||||
keysWithSubstitution[0],
|
|
||||||
)}
|
|
||||||
variables={[
|
|
||||||
{
|
|
||||||
element: 'span',
|
|
||||||
key: 'link',
|
|
||||||
children: {
|
|
||||||
element: 'MetaMaskTranslation',
|
|
||||||
props: {
|
|
||||||
translationKey: select(
|
|
||||||
'innerTranslationKey',
|
|
||||||
keysWithoutSubstitution,
|
|
||||||
keysWithoutSubstitution[0],
|
|
||||||
),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
]}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user