Skip to content

Commit

Permalink
[EuiI18n] Move arguments of lookupToken to lookupTokenOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
ym committed Oct 5, 2021
1 parent a5a542a commit 49d238e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 62 deletions.
29 changes: 6 additions & 23 deletions src/components/i18n/__snapshots__/i18n.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ exports[`EuiI18n reading values from context render prop with single token rende
default="This is a basic string."
token="test"
>
A nifty thing: An overridden string.
A nifty thing: This is a basic string.
</EuiI18n>
</EuiContext>
`;
Expand All @@ -417,7 +417,7 @@ exports[`EuiI18n reading values from context render prop with single token rende
}
}
>
Here's something cool: An overridden string with values.
Here's something cool: This is a string with values.
</EuiI18n>
</EuiContext>
`;
Expand All @@ -427,22 +427,7 @@ exports[`EuiI18n reading values from context rendering to dom calls a mapped fun
i18n={
Object {
"mapping": Object {
"test": [MockFunction] {
"calls": Array [
Array [
Object {
"special": "values",
"type": "callback",
},
],
],
"results": Array [
Object {
"type": "return",
"value": "This is a mapped callback with values.",
},
],
},
"test": [MockFunction],
},
}
}
Expand All @@ -456,9 +441,7 @@ exports[`EuiI18n reading values from context rendering to dom calls a mapped fun
"type": "callback",
}
}
>
This is a mapped callback with values.
</EuiI18n>
/>
</EuiContext>
`;

Expand All @@ -476,7 +459,7 @@ exports[`EuiI18n reading values from context rendering to dom renders a mapped b
default="This is a basic string."
token="test"
>
An overridden string.
This is a basic string.
</EuiI18n>
</EuiContext>
`;
Expand All @@ -501,7 +484,7 @@ exports[`EuiI18n reading values from context rendering to dom renders a mapped s
}
}
>
An overridden string with values.
This is a string with values.
</EuiI18n>
</EuiContext>
`;
Expand Down
86 changes: 47 additions & 39 deletions src/components/i18n/i18n.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,31 @@ function errorOnMissingValues(token: string): never {
);
}

interface lookupTokenOptions<
T extends RenderableValues,
DEFAULT extends Renderable<T>
> {
token: string;
i18nMapping: I18nShape['mapping'];
valueDefault: DEFAULT;
i18nMappingFunc?: (token: string) => string;
values?: I18nTokenShape<T, DEFAULT>['values'];
render?: (children: any[]) => FunctionComponent<any>;
}

function lookupToken<
T extends RenderableValues,
DEFAULT extends Renderable<T>,
RESOLVED extends ResolvedType<DEFAULT>
>(
token: string,
i18nMapping: I18nShape['mapping'],
valueDefault: DEFAULT,
i18nMappingFunc?: (token: string) => string,
values?: I18nTokenShape<T, DEFAULT>['values'],
render?: (children: any[]) => FunctionComponent<any>
): RESOLVED {
>(options: lookupTokenOptions<T, DEFAULT>): RESOLVED {
const {
token,
i18nMapping,
valueDefault,
i18nMappingFunc,
values,
render,
} = options;
let renderable = (i18nMapping && i18nMapping[token]) || valueDefault;

if (typeof renderable === 'function') {
Expand All @@ -67,9 +80,7 @@ function lookupToken<

const Component: FunctionComponent<any> = render
? render(children)
: () => {
return <Fragment>{children}</Fragment>;
};
: () => <Fragment>{children}</Fragment>;

// same reasons as above, we can't promise the transforms match the default's type
return React.createElement(Component, values) as RESOLVED;
Expand Down Expand Up @@ -119,26 +130,24 @@ const EuiI18n = <
if (isI18nTokensShape(props)) {
return props.children(
props.tokens.map((token, idx) =>
lookupToken(
lookupToken({
token,
mapping,
props.defaults[idx],
mappingFunc,
undefined,
render
)
i18nMapping: mapping,
valueDefault: props.defaults[idx],
render,
})
)
);
}

const tokenValue = lookupToken(
props.token,
mapping,
props.default,
mappingFunc,
props.values,
render
);
const tokenValue = lookupToken({
token: props.token,
i18nMapping: mapping,
valueDefault: props.default,
i18nMappingFunc: mappingFunc,
values: props.values,
render,
});
if (props.children) {
return props.children(tokenValue);
} else {
Expand Down Expand Up @@ -175,25 +184,24 @@ function useEuiI18n(...props: any[]) {

if (typeof props[0] === 'string') {
const [token, defaultValue, values] = props;
return lookupToken(
return lookupToken({
token,
mapping,
defaultValue,
mappingFunc,
i18nMapping: mapping,
valueDefault: defaultValue,
i18nMappingFunc: mappingFunc,
values,
render
);
render,
});
} else {
const [tokens, defaultValues] = props as [string[], string[]];
return tokens.map((token, idx) =>
lookupToken(
lookupToken({
token,
mapping,
defaultValues[idx],
mappingFunc,
undefined,
render
)
i18nMapping: mapping,
valueDefault: defaultValues[idx],
i18nMappingFunc: mappingFunc,
render,
})
);
}
}
Expand Down

0 comments on commit 49d238e

Please sign in to comment.