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.
Why don't use getDerivedStateFromProps()? This will produce an additional render
There was a problem hiding this comment.
do you mean getAutoControlledStateFromProps @layershifter? - please see Dialog.tsx
There was a problem hiding this comment.
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.
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.
Shouldn't we compare here getIdFromShorthand(props.label) with getIdFromShorthand(this.props.label)?
|
|
||
| this.state = { | ||
| visible: this.props.delay === 0, | ||
| labelLoaderId: getOrGenerateIdFromShorthand('loader-label-', props.label, ''), |
There was a problem hiding this comment.
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.
I expect that it will be just labelId
| state.labelLoaderId, | ||
| ) | ||
| return { | ||
| labelLoaderId: nextLabelLoaderId, |
There was a problem hiding this comment.
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.
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.
Nit: Let's invert this condition to make more readable:
return props['tabIndex'] === undefined ? undefined : props.labelLoaderId
layershifter
left a comment
There was a problem hiding this comment.
Plz fix UTs before merging 👍
Fixing the ticket:
#2254