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-565] Implement priorities fabric #166
Draft
tatinacher
wants to merge
11
commits into
master
Choose a base branch
from
feat/FRNT-565-implement-priorities-fabric
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
09c362c
feat: implement priorities fabric
tatinacher 18db9ac
refactor: remove examples, remove some weight variants
7032ca9
fix: correct white priority
7e88cee
fix: correct paths
bf01474
refactor: return example, update branch
5a8f668
fix: correct path
cbe5e8c
feat: add palette example
0a6ac11
fix: correct vw palette
e4ecbb5
feat: add all palettes to example
36ac2f3
refactor: explicitly pass palette name
267c156
feat: add priority-weight matrix
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
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,35 @@ | ||
type VaritationsMap = Record<string, readonly unknown[]>; | ||
|
||
export function createCombinations(obj: VaritationsMap) { | ||
const fieldNames = Object.keys(obj); | ||
|
||
if (fieldNames.length === 0) return [{}]; | ||
|
||
function _combinations<Combination extends string[]>( | ||
[key, ...restKeys]: Combination, | ||
combinations: Record<string, unknown[]>, | ||
): Record<string, unknown>[] { | ||
const possibleValues = obj[key]; | ||
|
||
if (!Array.isArray(possibleValues) || possibleValues.length === 0) { | ||
throw new Error(`Please provide a non-empty array of possible values for prop ${key}`); | ||
} | ||
|
||
const variation = possibleValues.map((fieldValue) => ({ | ||
...combinations, | ||
[key]: fieldValue, | ||
})); | ||
|
||
if (restKeys.length === 0) { | ||
return variation; | ||
} | ||
|
||
return flatMap(variation, (newAcc) => _combinations(restKeys, newAcc)); | ||
} | ||
|
||
return _combinations(fieldNames, {}); | ||
} | ||
|
||
function flatMap<T>(arr: Array<T>, fn: (value: T, index: number, array: Array<T>) => Array<T>) { | ||
return arr.map(fn).reduce((a, b) => a.concat(b)); | ||
} |
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,16 @@ | ||
export function groupByKey<Variants extends Record<string, unknown>, Key extends keyof Variants>( | ||
arr: Variants[], | ||
key: Key, | ||
keyMapper?: (fn: Variants[Key]) => string, | ||
) { | ||
return arr.reduce<Record<string, Variants[]>>((all, current) => { | ||
const valueAsKey = keyMapper ? keyMapper(current[key]) : `${current[key]}`; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
if (all[valueAsKey] === undefined) { | ||
all[valueAsKey] = []; | ||
} | ||
all[valueAsKey].push(current); | ||
return all; | ||
}, {}); | ||
} |
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,111 @@ | ||
import React from 'react'; | ||
import styled from 'styled-components'; | ||
import { Global } from 'dev/global'; | ||
import { Grid, Heading } from 'ui'; | ||
|
||
import { createCombinations } from './common/combination'; | ||
import { groupByKey } from './common/group-by-key'; | ||
|
||
export const priorities = ['default', 'primary', 'secondary', 'white', 'danger', 'success']; | ||
|
||
interface ThemeProps { | ||
weight: string; | ||
priority: string; | ||
disabled: boolean; | ||
} | ||
|
||
interface PriorityWeightMapProps { | ||
weights: ('fill' | 'outline' | 'goast' | 'transparent')[]; | ||
render: (props: ThemeProps) => React.ReactElement; | ||
} | ||
|
||
export const PriorityWeightMap = ({ weights, render }: PriorityWeightMapProps) => { | ||
const allCombinations = createCombinations({ | ||
weight: weights, | ||
priority: priorities, | ||
disabled: [true, false], | ||
}); | ||
|
||
if (Object.keys(allCombinations).length === 0) return null; | ||
|
||
return ( | ||
<Wrapper> | ||
{Object.entries(groupByKey(allCombinations, 'priority')).map( | ||
([priority, combinations], index) => { | ||
return ( | ||
// eslint-disable-next-line react/no-array-index-key | ||
<PriorityGroup key={index}> | ||
<Header>{priority}</Header> | ||
<GridTemplate columns={weights.length + 1}> | ||
<div /> {/* plug to make empty space at top left corner */} | ||
{weights.map((weight, index) => ( | ||
<Centered key={index}> | ||
<b>{weight}</b> | ||
</Centered> | ||
))} | ||
{Object.entries( | ||
groupByKey(combinations, 'disabled', (key) => (key ? 'normal' : 'disable')), | ||
).map(([state, variations], index) => { | ||
return ( | ||
<React.Fragment key={index}> | ||
<Centered> | ||
<b>{state}</b> | ||
</Centered> | ||
{variations.map((variation, index) => { | ||
const { disabled, ...props } = variation as ThemeProps; | ||
|
||
return ( | ||
<VariantCell key={index}> | ||
{render({ ...props, disabled: state === 'disable' })} | ||
</VariantCell> | ||
); | ||
})} | ||
</React.Fragment> | ||
); | ||
})} | ||
</GridTemplate> | ||
</PriorityGroup> | ||
); | ||
}, | ||
)} | ||
</Wrapper> | ||
); | ||
}; | ||
|
||
const Wrapper = styled(Global)` | ||
display: flex; | ||
flex-direction: row; | ||
overflow-y: auto; | ||
`; | ||
|
||
const PriorityGroup = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
padding: 20px; | ||
`; | ||
|
||
const VariantCell = styled.div` | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
min-width: 100px; | ||
padding: 20px; | ||
`; | ||
|
||
const Header = styled(Heading)` | ||
padding-bottom: 15px; | ||
padding-left: 5px; | ||
`; | ||
|
||
const GridTemplate = styled(Grid)` | ||
gap: 10px; | ||
|
||
color: #c4c4c4; | ||
white-space: pre-line; | ||
`; | ||
|
||
const Centered = styled.div` | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
`; |
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
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,39 @@ | ||
import { css } from 'styled-components'; | ||
|
||
interface PriorityType { | ||
bwPaletteName: string; | ||
paletteName: string; | ||
priorityName: string; | ||
} | ||
|
||
export const createPriority = ({ bwPaletteName, paletteName, priorityName }: PriorityType) => { | ||
const colors: Record<string, string> = { | ||
'shape-default': `hsla(var(--${paletteName}-500), 1)`, | ||
'shape-disabled': `hsla(var(--${paletteName}-200), 1)`, | ||
'shape-hover': `hsla(var(--${paletteName}-600), 1)`, | ||
'shape-active': `hsla(var(--${paletteName}-700), 1)`, | ||
|
||
'shape-text-default': `hsla(var(--${bwPaletteName}-0), 1)`, | ||
'shape-text-disabled': `hsla(var(--${paletteName}-300), 1)`, | ||
'shape-text-hover': `hsla(var(--${bwPaletteName}-0), 1)`, | ||
'shape-text-active': `hsla(var(--${bwPaletteName}-0), 1)`, | ||
|
||
'canvas-default': `transparent`, | ||
'canvas-disabled': `hsla(var(--${bwPaletteName}-200), 1)`, | ||
'canvas-hover': `hsla(var(--${paletteName}-600), 1)`, | ||
'canvas-active': `hsla(var(--${paletteName}-700), 1)`, | ||
|
||
'canvas-text-default': `hsla(var(--${bwPaletteName}-1000), 1)`, | ||
'canvas-text-disabled': `hsla(var(--${bwPaletteName}-300), 1)`, | ||
'canvas-text-hover': `hsla(var(--${bwPaletteName}-1000), 1)`, | ||
'canvas-text-active': `hsla(var(--${bwPaletteName}-1000), 1)`, | ||
}; | ||
|
||
const priorityPalette = Object.keys(colors).map((key) => `--woly-${key}: ${colors[key]};`); | ||
|
||
return css` | ||
[data-priority='${priorityName}'] { | ||
${priorityPalette.join('\n')} | ||
} | ||
`; | ||
}; |
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.
suggestion: очень сложный jsx, может стоит декомпозировать?