-
Notifications
You must be signed in to change notification settings - Fork 53
fix(Loader): Adding labeling when loader get focus #2352
Conversation
|
||
componentDidUpdate(prevProps) { | ||
if (this.getIdFromShorthand(this.props.label) !== this.getIdFromShorthand(prevProps.label)) { | ||
this.setState(prevState => ({ labelLoaderId: getOrGenerateIdFromShorthand('loader-label-', this.props.label, prevState.labelLoaderId) })) | ||
} |
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.
Why don't use getDerivedStateFromProps()
? This will produce an additional render
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.
do you mean getAutoControlledStateFromProps
@layershifter? - please see Dialog.tsx
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.
getDerivedStateFromProps
should be enough. This is not Autocontrolled component.
}) | ||
|
||
test('do NOT add aria-labelledby, when aria-labelled was set already', () => { | ||
props['aria-labelledby'] = 'id' |
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.
Let's avoid mutation of props
:
const expectedResult = loaderBehavior({ labelLoaderId: "label-id",'aria-labelledby':'id' })
docs/src/examples/components/Loader/BestPractices/LoaderBestPractices.tsx
Outdated
Show resolved
Hide resolved
…actices.tsx Co-Authored-By: Juraj Kapsiar <jurokapsiar@gmail.com>
Co-Authored-By: Juraj Kapsiar <jurokapsiar@gmail.com>
Perf comparison
Perf tests with no regressions
Generated by 🚫 dangerJS |
} | ||
|
||
static getDerivedStateFromProps(props, state) { | ||
if (Loader.getIdFromShorthand(props.label) !== Loader.getIdFromShorthand(state.labelLoaderId)) { |
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.
Shouldn't we compare here getIdFromShorthand(props.label)
with getIdFromShorthand(this.props.label)
?
@@ -94,9 +97,40 @@ class Loader extends UIComponent<WithAsProp<LoaderProps>, LoaderState> { | |||
|
|||
this.state = { | |||
visible: this.props.delay === 0, | |||
labelLoaderId: getOrGenerateIdFromShorthand('loader-label-', props.label, ''), |
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.
We don't need this anymore as getDerivedStateFromProps()
is executed before each render
|
||
type LoaderBehaviorProps = { | ||
/** id of the loader label element. */ | ||
labelLoaderId?: string |
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.
I expect that it will be just labelId
state.labelLoaderId, | ||
) | ||
return { | ||
labelLoaderId: nextLabelLoaderId, |
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.
I believe that it can used in the same way as in Dialog
, just:
return {
labelId: getOrGenerateIdFromShorthand('loader-label-', props.label, state.loaderId),
}
return result | ||
} | ||
|
||
static getDerivedStateFromProps(props, state) { |
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.
Let's type this method as in Dialog
if (props['aria-label'] || props['aria-labelledby']) { | ||
return undefined | ||
} | ||
return props['tabIndex'] !== undefined ? props.labelLoaderId : undefined |
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: Let's invert this condition to make more readable:
return props['tabIndex'] === undefined ? undefined : props.labelLoaderId
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.
Plz fix UTs before merging 👍
Fixing the ticket:
#2254