-
Notifications
You must be signed in to change notification settings - Fork 72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Enhance] Implement Validation for Icon Input Source & Set Default Icon to Beaker
#1137
Merged
joshuarrrr
merged 7 commits into
opensearch-project:main
from
willie-hung:feature/beaker
Dec 8, 2023
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
303d5a4
Revise the logic to handle default icon
willie-hung f6cbb85
Categorize different input icon types
willie-hung 7680f68
Remove unused code
willie-hung 575d63e
Update CHANGELOG.md
willie-hung 80a1741
Use readable parameter name
willie-hung 3f57ba3
Handle different input cases and add tests
willie-hung f26c170
Merge branch 'main' into feature/beaker
joshuarrrr 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
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 |
---|---|---|
|
@@ -42,7 +42,7 @@ import { CommonProps, keysOf } from '../common'; | |
// because we'd need to dynamically know if we're importing the | ||
// TS file (dev/docs) or the JS file (distributed), and it's more effort than worth | ||
// to generate & git track a TS module definition for each icon component | ||
import { icon as empty } from './assets/empty.js'; | ||
import { icon as defaultIcon } from './assets/beaker.js'; | ||
import { enqueueStateChange } from '../../services/react'; | ||
|
||
import { htmlIdGenerator } from '../../services'; | ||
|
@@ -582,22 +582,16 @@ interface State { | |
neededLoading: boolean; // controls the fade-in animation, cached icons are immediately rendered | ||
} | ||
|
||
function isOuiIconType(x: OuiIconProps['type']): x is OuiIconType { | ||
return typeof x === 'string' && typeToPathMap.hasOwnProperty(x); | ||
function isOuiIconType(type: OuiIconProps['type']): type is OuiIconType { | ||
return typeof type === 'string' && typeToPathMap.hasOwnProperty(type); | ||
} | ||
|
||
function getInitialIcon(icon: OuiIconProps['type']) { | ||
if (icon == null) { | ||
return undefined; | ||
} | ||
if (isOuiIconType(icon)) { | ||
if (iconComponentCache.hasOwnProperty(icon)) { | ||
return iconComponentCache[icon]; | ||
} | ||
return undefined; | ||
} | ||
function isUrl(type: OuiIconProps['type']) { | ||
return typeof type === 'string' && (type.includes('.') || type.includes('/')); | ||
} | ||
|
||
return icon; | ||
function isCachedIcon(type: OuiIconProps['type']) { | ||
return isOuiIconType(type) && iconComponentCache.hasOwnProperty(type); | ||
} | ||
|
||
const generateId = htmlIdGenerator(); | ||
|
@@ -628,13 +622,22 @@ export class OuiIcon extends PureComponent<OuiIconProps, State> { | |
super(props); | ||
|
||
const { type } = props; | ||
const initialIcon = getInitialIcon(type); | ||
let initialIcon; | ||
let isLoading = false; | ||
|
||
if (isOuiIconType(type) && initialIcon == null) { | ||
// Category 1: cached oui icons | ||
if (isCachedIcon(type)) { | ||
initialIcon = iconComponentCache[type as string]; | ||
// Category 2: URL (relative, absolute) | ||
} else if (isUrl(type)) { | ||
initialIcon = type; | ||
// Category 3: non-cached oui icon or new icon | ||
} else if (typeof type === 'string') { | ||
isLoading = true; | ||
this.loadIconComponent(type); | ||
this.loadIconComponent(type as OuiIconType); | ||
} else { | ||
// Category 4: custom icon component | ||
initialIcon = type; | ||
this.onIconLoad(); | ||
} | ||
Comment on lines
+628
to
642
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. This could be rewritten to not use a mutable variable. I'll make a followup issue, no need to worry about pushing it in this PR 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. |
||
|
||
|
@@ -683,12 +686,14 @@ export class OuiIcon extends PureComponent<OuiIconProps, State> { | |
return; | ||
} | ||
|
||
const iconPath = typeToPathMap[iconType] || 'beaker'; | ||
|
||
import( | ||
/* webpackChunkName: "icon.[request]" */ | ||
// It's important that we don't use a template string here, it | ||
// stops webpack from building a dynamic require context. | ||
// eslint-disable-next-line prefer-template | ||
'./assets/' + typeToPathMap[iconType] + '.js' | ||
'./assets/' + iconPath + '.js' | ||
).then(({ icon }) => { | ||
iconComponentCache[iconType] = icon; | ||
enqueueStateChange(() => { | ||
|
@@ -761,7 +766,7 @@ export class OuiIcon extends PureComponent<OuiIconProps, State> { | |
className | ||
); | ||
|
||
const icon = this.state.icon || (empty as ComponentType); | ||
const icon = this.state.icon || (defaultIcon as ComponentType); | ||
|
||
// This is a fix for IE and Edge, which ignores tabindex="-1" on an SVG, but respects | ||
// focusable="false". | ||
|
@@ -771,6 +776,7 @@ export class OuiIcon extends PureComponent<OuiIconProps, State> { | |
// - For all other values, the consumer wants the icon to be focusable. | ||
const focusable = tabIndex == null || tabIndex === -1 ? 'false' : 'true'; | ||
|
||
// relative, absolute path | ||
if (typeof icon === 'string') { | ||
return ( | ||
<img | ||
|
@@ -784,15 +790,15 @@ export class OuiIcon extends PureComponent<OuiIconProps, State> { | |
} else { | ||
const Svg = icon; | ||
|
||
// If it's an empty icon, or if there is no aria-label, aria-labelledby, or title it gets aria-hidden true | ||
// If it's a default icon, or if there is no aria-label, aria-labelledby, or title it gets aria-hidden true | ||
const isAriaHidden = | ||
icon === empty || | ||
icon === defaultIcon || | ||
!( | ||
this.props['aria-label'] || | ||
this.props['aria-labelledby'] || | ||
this.props.title | ||
); | ||
const hideIconEmpty = isAriaHidden && { 'aria-hidden': true }; | ||
const hideDefaultIcon = isAriaHidden && { 'aria-hidden': true }; | ||
|
||
let titleId: any; | ||
|
||
|
@@ -817,7 +823,7 @@ export class OuiIcon extends PureComponent<OuiIconProps, State> { | |
title={title} | ||
{...titleId} | ||
{...rest} | ||
{...hideIconEmpty} | ||
{...hideDefaultIcon} | ||
/> | ||
); | ||
} | ||
|
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.
nit - maybe it's better to test custom svg rendering with an asset that isn't used as a fallback.
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.
@joshuarrrr should I raise an issue so that we can revise this unit test or just leave it as it is?
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.
Sure, if you think it's a worthwhile change.