This repository has been archived by the owner on Jul 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
[FRNT-485]: Generate map of component states #122
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
/* eslint-disable no-loop-func */ | ||
/* eslint-disable react/no-array-index-key */ | ||
import * as React from 'react'; | ||
import styled, { StyledComponent } from 'styled-components'; | ||
import { Global } from 'lib/global'; | ||
import { Grid, Heading } from 'ui'; | ||
|
||
export type SizeProps = 'N' | 'XS' | 'S' | 'M' | 'L' | 'XL' | 'H'; | ||
|
||
type ComponentValuesType = string | boolean | SizeProps; | ||
|
||
interface ComponentProps { | ||
name: string; | ||
values: Array<ComponentValuesType>; | ||
} | ||
|
||
interface GenerateMapProps { | ||
otherProps: Record<string, Array<unknown>>; | ||
sizes: ComponentProps; | ||
variants: ComponentProps; | ||
} | ||
|
||
interface SizesProps { | ||
size: SizeProps; | ||
} | ||
|
||
const map = ({ size }: SizesProps) => ({ | ||
'data-size': size, | ||
}); | ||
|
||
export const GenerateMap: React.FC< | ||
GenerateMapProps & { component: React.ComponentType<unknown> } | ||
> = ({ sizes, variants, otherProps, component }) => { | ||
const Component = component; | ||
const variantsMap = generateMap({ y: sizes, x: variants, otherProps }); | ||
|
||
if (!variantsMap) return null; | ||
|
||
return ( | ||
<Main> | ||
{variantsMap.map((variant, i) => ( | ||
<VariantBlock key={i}> | ||
<Header>{variant.name}</Header> | ||
<GridTemplate columns={variant.columns}> | ||
<AreaVariants> | ||
<div /> | ||
{/** TODO: replace */} | ||
{variant.values[0].values.map(({ variants, sizes, ...prop }, headKey) => ( | ||
<div key={`head-${headKey}`}>{headCreate(prop)}</div> | ||
))} | ||
{variant.values.map((props, j) => ( | ||
<> | ||
<b>{props.name}</b> | ||
{props.values.map((prop, propKey) => ( | ||
<SizeBlock size={props.name as SizeProps} key={j}> | ||
<Component prop={prop} key={`prop-${propKey}`} /> | ||
</SizeBlock> | ||
))} | ||
</> | ||
))} | ||
</AreaVariants> | ||
</GridTemplate> | ||
</VariantBlock> | ||
))} | ||
</Main> | ||
); | ||
}; | ||
|
||
/** TODO */ | ||
export const generateMap = ({ x, y, otherProps }: any) => { | ||
const result = []; | ||
|
||
if (x.values.length === 0 || y.values.length === 0) return; | ||
|
||
for (const valueX of x.values) { | ||
const yArr = []; | ||
let columns = 0; | ||
for (const valueY of y.values) { | ||
const main = { [x.name]: valueX, [y.name]: valueY }; | ||
|
||
const map = generatePropsMap(main, otherProps); | ||
yArr.push({ name: valueY, values: map }); | ||
columns = map.length + 1; | ||
} | ||
result.push({ name: valueX, values: yArr, columns }); | ||
} | ||
return result; | ||
}; | ||
|
||
const generatePropsMap = ( | ||
main: Record<string, unknown>, | ||
otherProps: Record<string, Array<unknown>>, | ||
) => { | ||
let tree = [main]; | ||
|
||
Object.keys(otherProps).forEach((key: string) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. а через reduce тут разве нельзя? |
||
const values: Array<unknown> = otherProps[key]; | ||
const buffer: Array<Record<string, unknown>> = []; | ||
|
||
values.forEach((value: unknown) => { | ||
const cloned = deepCopy(tree); | ||
cloned.forEach((item: Record<string, unknown>) => (item[key] = value)); | ||
buffer.push(...cloned); | ||
}); | ||
tree = buffer; | ||
}); | ||
|
||
return tree; | ||
}; | ||
|
||
const deepCopy = (obj: Array<Record<string, unknown>>) => JSON.parse(JSON.stringify(obj)); | ||
|
||
export const headCreate = (props: Record<string, unknown>) => | ||
Object.keys(props).reduce((acc, key) => acc + (props[key] ? key + '\n' : ''), ''); | ||
|
||
const VariantBlock = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
padding: 20px; | ||
`; | ||
|
||
const Header = styled(Heading)` | ||
padding-bottom: 15px; | ||
padding-left: 5px; | ||
`; | ||
|
||
const GridTemplate = styled(Grid)` | ||
gap: 10px; | ||
`; | ||
|
||
const AreaVariants = styled(Grid)` | ||
gap: 10px; | ||
|
||
color: #c4c4c4; | ||
white-space: pre-line; | ||
`; | ||
|
||
export const Column = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
& > * { | ||
margin: 5px; | ||
} | ||
`; | ||
|
||
export const Row = styled.div` | ||
display: flex; | ||
& > * { | ||
margin: 5px; | ||
} | ||
`; | ||
|
||
export const Main = styled(Global)` | ||
display: flex; | ||
overflow: scroll; | ||
|
||
white-space: nowrap; | ||
`; | ||
|
||
export const SizeBlock = styled.div.attrs(map)` | ||
display: flex; | ||
align-items: center; | ||
margin: 5px; | ||
--woly-font-size: 15px; | ||
|
||
&[data-size='N'] { | ||
--woly-component-level: 0; | ||
} | ||
&[data-size='XS'] { | ||
--woly-component-level: 1; | ||
} | ||
&[data-size='S'] { | ||
--woly-component-level: 2; | ||
} | ||
&[data-size='M'] { | ||
--woly-component-level: 3; | ||
} | ||
&[data-size='L'] { | ||
--woly-component-level: 4; | ||
--woly-font-size: 18px; | ||
} | ||
&[data-size='XL'] { | ||
--woly-component-level: 5; | ||
--woly-font-size: 21px; | ||
} | ||
&[data-size='H'] { | ||
--woly-component-level: 6; | ||
--woly-font-size: 21px; | ||
} | ||
` as StyledComponent<'div', Record<string, unknown>, SizesProps>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import * as React from 'react'; | ||
import { ButtonIcon } from 'ui'; | ||
import { GenerateMap, SizeProps } from 'lib/map-generation'; | ||
import { IconSearch } from 'static/icons'; | ||
|
||
interface ComponentProps { | ||
name: string; | ||
values: Array<string | boolean>; | ||
} | ||
|
||
interface ButtonIconProps { | ||
disabled: boolean; | ||
filled: boolean; | ||
sizes: SizeProps; | ||
variants: string; | ||
} | ||
|
||
export const variants: ComponentProps = { | ||
name: 'variants', | ||
values: ['primary', 'secondary'], | ||
}; | ||
|
||
export const sizes: ComponentProps = { | ||
name: 'sizes', | ||
values: ['N', 'XS', 'S', 'M', 'L', 'XL', 'H'], | ||
}; | ||
|
||
export const otherProps = { disabled: [false, true], filled: [false, true] }; | ||
|
||
export const ComponentButtonIcon = ({ prop }: { prop: ButtonIconProps }) => ( | ||
<ButtonIcon | ||
icon={<IconSearch />} | ||
onClick={() => console.info('ButtonIcon clicked')} | ||
variant={prop.variants} | ||
disabled={prop.disabled} | ||
filled={prop.filled} | ||
/> | ||
); | ||
|
||
export const GenerateButtonIconMap = () => ( | ||
<GenerateMap | ||
component={ComponentButtonIcon} | ||
otherProps={otherProps} | ||
sizes={sizes} | ||
variants={variants} | ||
/> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
name: button-icon-map | ||
category: map | ||
package: woly | ||
--- | ||
|
||
import {GenerateButtonIconMap } from './state-props' | ||
|
||
<GenerateButtonIconMap /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* eslint-disable react/jsx-props-no-spreading */ | ||
import * as React from 'react'; | ||
import { GenerateMap } from 'lib/map-generation'; | ||
import { IconSearch } from 'static/icons'; | ||
|
||
import { Button } from '.'; | ||
|
||
interface ComponentProps { | ||
name: string; | ||
values: Array<string | boolean>; | ||
} | ||
|
||
interface ComponentButtonProps { | ||
disabled: boolean; | ||
icon: boolean; | ||
outlined: boolean; | ||
sizes: 'N' | 'XS' | 'S' | 'M' | 'L' | 'XL' | 'H'; | ||
variants: string; | ||
} | ||
|
||
export const variants: ComponentProps = { | ||
name: 'variants', | ||
values: ['primary', 'secondary'], | ||
}; | ||
|
||
export const sizes: ComponentProps = { | ||
name: 'sizes', | ||
values: ['N', 'XS', 'S', 'M', 'L', 'XL', 'H'], | ||
}; | ||
|
||
export const otherProps: Record<string, Array<unknown>> = { | ||
disabled: [false, true], | ||
outlined: [false, true], | ||
icon: [false, true], | ||
}; | ||
|
||
export const ComponentButton: React.FC<{ prop: ComponentButtonProps }> = ({ prop }) => ( | ||
<Button | ||
disabled={prop.disabled} | ||
icon={prop.icon ? <IconSearch /> : null} | ||
onClick={() => console.log('Hi!')} | ||
outlined={prop.outlined} | ||
text="Button" | ||
variant={prop.variants} | ||
/> | ||
); | ||
|
||
export const GenerateButton = () => ( | ||
<GenerateMap | ||
component={ComponentButton} | ||
otherProps={otherProps} | ||
sizes={sizes} | ||
variants={variants} | ||
/> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
name: button-map | ||
category: map | ||
package: woly | ||
--- | ||
|
||
import {GenerateButton } from './state-props' | ||
|
||
<GenerateButton /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
а что прилетает в otherProps?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
любые пропсы состояний компонента