Skip to content

Commit

Permalink
Add getChildTag fn to Inline component (#6453)
Browse files Browse the repository at this point in the history
* move getChildTag to util, apply to Inline component as well

* update comment

* changeset

* replace padding-inline-start with more standard padding-left
  • Loading branch information
gwyneplaine authored Sep 2, 2021
1 parent ad0bbed commit 069265b
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/empty-panthers-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/keystone': patch
---

Dashboard cards now enclosed in unordered list, for clearer semantics.
5 changes: 5 additions & 0 deletions .changeset/long-birds-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-ui/core': minor
---

Added functionality to ensure that Inline elements that are 'ul' or 'ol' automatically wrap children in 'li' rather than 'div'
7 changes: 4 additions & 3 deletions design-system/packages/core/src/components/Inline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { Children, ReactNode } from 'react';
import { jsx } from '../emotion';

import { forwardRefWithAs } from '../utils';
import { forwardRefWithAs, getChildTag } from '../utils';
import { Theme } from '../types';
import { useTheme } from '../theme';
import { Box, BoxProps } from './Box';
Expand All @@ -30,6 +30,7 @@ export const Inline = forwardRefWithAs<'div', InlineProps>(
const { spacing } = useTheme();
const resolvedAlign = alignment[align];
const resolvedGap = spacing[gap];
const ChildWrapper = getChildTag(props.as);

return (
<Box
Expand All @@ -45,7 +46,7 @@ export const Inline = forwardRefWithAs<'div', InlineProps>(
>
{Children.map(children, child =>
child !== null && child !== undefined ? (
<div
<ChildWrapper
css={{
display: 'flex',
flexWrap: 'wrap',
Expand All @@ -54,7 +55,7 @@ export const Inline = forwardRefWithAs<'div', InlineProps>(
}}
>
{child}
</div>
</ChildWrapper>
) : null
)}
</Box>
Expand Down
14 changes: 2 additions & 12 deletions design-system/packages/core/src/components/Stack.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/** @jsxRuntime classic */
/** @jsx jsx */

import { ElementType, Children, Fragment, ReactNode, isValidElement } from 'react';
import { Children, Fragment, ReactNode, isValidElement } from 'react';

import { jsx } from '../emotion';
import { useMediaQuery } from '../hooks/useMediaQuery';
import { useTheme } from '../theme';
import { Theme } from '../types';
import { forwardRefWithAs, mapResponsiveProp } from '../utils';
import { forwardRefWithAs, mapResponsiveProp, getChildTag } from '../utils';

import { Box, BoxProps } from './Box';
import { Divider } from './Divider';
Expand Down Expand Up @@ -45,16 +45,6 @@ export type StackProps = {
gap?: keyof Theme['spacing'];
} & BoxProps;

const getChildTag = (parentTag?: ElementType<any>) => {
switch (parentTag) {
case 'ul':
case 'ol':
return 'li';
default:
return 'div';
}
};

export const Stack = forwardRefWithAs<'div', StackProps>(
({ across, align = 'stretch', children, dividers = 'none', gap = 'none', ...props }, ref) => {
const { spacing } = useTheme();
Expand Down
14 changes: 14 additions & 0 deletions design-system/packages/core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ import {
} from 'react';
import { createPortal } from 'react-dom';

/*
Simple switch to return a child tag from a parent tag argument.
Returns a div by default.
*/
export const getChildTag = (parentTag?: ElementType<any>) => {
switch (parentTag) {
case 'ul':
case 'ol':
return 'li';
default:
return 'div';
}
};

/*
@johannes' one weird trick for fixing TypeScript autocomplete
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,15 @@ export const HomePage = () => {
<LoadingDots label="Loading lists" size="large" tone="passive" />
</Center>
) : (
<Inline gap="large" paddingY="xlarge">
<Inline
as="ul"
gap="large"
paddingY="xlarge"
css={{
paddingLeft: '0px',
marginBottom: '0px',
}}
>
{(() => {
if (visibleLists.state === 'error') {
return (
Expand Down

0 comments on commit 069265b

Please sign in to comment.