-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support auto-assigning colors for certain components (#96)
* feat: support auto-assigning colors for certain components * Update package.json
- Loading branch information
Showing
10 changed files
with
137 additions
and
3 deletions.
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
34 changes: 32 additions & 2 deletions
34
packages/components/src/components/common/bindStylesToDataStreams.ts
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 |
---|---|---|
@@ -1,18 +1,48 @@ | ||
import { StyleSettingsMap, DataStream } from '@iot-app-kit/core'; | ||
import { colorPalette } from './colorPalette'; | ||
|
||
const assignDefaultColor = ({ | ||
dataStream, | ||
index, | ||
styleSettings, | ||
}: { | ||
dataStream: DataStream; | ||
index: number; | ||
styleSettings?: StyleSettingsMap; | ||
}): DataStream => { | ||
const associatedStyles = dataStream.refId != null && styleSettings != null ? styleSettings[dataStream.refId] : {}; | ||
const hasAssociatedColor = 'color' in associatedStyles; | ||
|
||
// Only provide default if one is not already present in the data stream, and none is specified in the associated style settings. | ||
if (dataStream.color == null && !hasAssociatedColor) { | ||
return { | ||
...dataStream, | ||
color: colorPalette[index % colorPalette.length], | ||
}; | ||
} | ||
return dataStream; | ||
}; | ||
|
||
// If the data stream has a reference id with associated styles, append those styles to the data stream. | ||
export const bindStylesToDataStreams = ({ | ||
dataStreams, | ||
styleSettings, | ||
assignDefaultColors, | ||
}: { | ||
dataStreams: DataStream[]; | ||
assignDefaultColors: boolean; | ||
styleSettings?: StyleSettingsMap; | ||
}): DataStream[] => | ||
dataStreams.map((dataStream) => | ||
}): DataStream[] => { | ||
const streams = dataStreams.map((dataStream, i) => | ||
styleSettings == null || dataStream.refId == null | ||
? dataStream | ||
: { | ||
...dataStream, | ||
...styleSettings[dataStream.refId], | ||
} | ||
); | ||
|
||
return assignDefaultColors | ||
? streams.map((dataStream, index) => assignDefaultColor({ dataStream, index, styleSettings })) | ||
: streams; | ||
}; |
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 @@ | ||
// CSS Color strings, in default order they'll be used. | ||
export const colorPalette = [ | ||
'#5e87b5', | ||
'#e6ac8c', | ||
'#7fc6b1', | ||
'#d99090', | ||
'#ae779c', | ||
'#f9da95', | ||
'#b088f5', | ||
'#c55305', | ||
'#018574', | ||
'#486de8', | ||
'#962249', | ||
'#096f64', | ||
'#8456ce', | ||
]; |
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
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