-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
CustomSelectControl V2 legacy adapter: stabilize experimental props #63248
Changes from all commits
093a1fd
8bae16c
585e566
f529ae0
2e42cbe
b0db404
b12ab44
96299c6
9ad93e5
0f127e8
8e02b5f
2c98210
4d118af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,18 +12,41 @@ import CustomSelectItem from '../item'; | |
import type { LegacyCustomSelectProps } from '../types'; | ||
import * as Styled from '../styles'; | ||
|
||
function useDeprecatedProps( { | ||
__experimentalShowSelectedHint, | ||
...otherProps | ||
}: LegacyCustomSelectProps ) { | ||
return { | ||
showSelectedHint: __experimentalShowSelectedHint, | ||
...otherProps, | ||
}; | ||
} | ||
|
||
// The removal of `__experimentalHint` in favour of `hint` doesn't happen in | ||
// the `useDeprecatedProps` hook in order not to break consumers that rely | ||
// on object identity (see https://github.com/WordPress/gutenberg/pull/63248#discussion_r1672213131) | ||
function applyOptionDeprecations( { | ||
__experimentalHint, | ||
...rest | ||
}: LegacyCustomSelectProps[ 'options' ][ number ] ) { | ||
return { | ||
hint: __experimentalHint, | ||
...rest, | ||
}; | ||
} | ||
|
||
function CustomSelectControl( props: LegacyCustomSelectProps ) { | ||
const { | ||
__experimentalShowSelectedHint = false, | ||
__next40pxDefaultSize = false, | ||
describedBy, | ||
options, | ||
onChange, | ||
size = 'default', | ||
value, | ||
className: classNameProp, | ||
showSelectedHint = false, | ||
...restProps | ||
} = props; | ||
} = useDeprecatedProps( props ); | ||
|
||
// Forward props + store from v2 implementation | ||
const store = Ariakit.useSelectStore( { | ||
|
@@ -60,16 +83,17 @@ function CustomSelectControl( props: LegacyCustomSelectProps ) { | |
defaultValue: options[ 0 ]?.name, | ||
} ); | ||
|
||
const children = options.map( | ||
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. The following block of code is just about changing from mostly |
||
( { name, key, __experimentalHint, style, className } ) => { | ||
const children = options | ||
.map( applyOptionDeprecations ) | ||
.map( ( { name, key, hint, style, className } ) => { | ||
const withHint = ( | ||
<Styled.WithHintItemWrapper> | ||
<span>{ name }</span> | ||
<Styled.WithHintItemHint | ||
// TODO: Legacy classname. Add V1 styles are removed from the codebase | ||
// className="components-custom-select-control__item-hint" | ||
> | ||
{ __experimentalHint } | ||
{ hint } | ||
</Styled.WithHintItemHint> | ||
</Styled.WithHintItemWrapper> | ||
); | ||
|
@@ -78,38 +102,39 @@ function CustomSelectControl( props: LegacyCustomSelectProps ) { | |
<CustomSelectItem | ||
key={ key } | ||
value={ name } | ||
children={ __experimentalHint ? withHint : name } | ||
children={ hint ? withHint : name } | ||
style={ style } | ||
className={ clsx( | ||
// TODO: Legacy classname. Add V1 styles are removed from the codebase | ||
// 'components-custom-select-control__item', | ||
className | ||
// TODO: Legacy classname. Add V1 styles are removed from the codebase | ||
// { | ||
// 'has-hint': __experimentalHint, | ||
// 'has-hint': hint, | ||
// } | ||
) } | ||
/> | ||
); | ||
} | ||
); | ||
} ); | ||
|
||
const renderSelectedValueHint = () => { | ||
const { value: currentValue } = store.getState(); | ||
|
||
const currentHint = options?.find( | ||
( { name } ) => currentValue === name | ||
); | ||
const selectedOptionHint = options | ||
?.map( applyOptionDeprecations ) | ||
?.find( ( { name } ) => currentValue === name )?.hint; | ||
|
||
return ( | ||
<Styled.SelectedExperimentalHintWrapper> | ||
{ currentValue } | ||
<Styled.SelectedExperimentalHintItem | ||
// TODO: Legacy classname. Add V1 styles are removed from the codebase | ||
// className="components-custom-select-control__hint" | ||
> | ||
{ currentHint?.__experimentalHint } | ||
</Styled.SelectedExperimentalHintItem> | ||
{ selectedOptionHint && ( | ||
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. Previously, we were rendering this component even where there wasn't a hint to show 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. Good catch! |
||
<Styled.SelectedExperimentalHintItem | ||
// TODO: Legacy classname. Add V1 styles are removed from the codebase | ||
// className="components-custom-select-control__hint" | ||
> | ||
{ selectedOptionHint } | ||
</Styled.SelectedExperimentalHintItem> | ||
) } | ||
</Styled.SelectedExperimentalHintWrapper> | ||
); | ||
}; | ||
|
@@ -131,9 +156,7 @@ function CustomSelectControl( props: LegacyCustomSelectProps ) { | |
<_CustomSelect | ||
aria-describedby={ describedBy } | ||
renderSelectedValue={ | ||
__experimentalShowSelectedHint | ||
? renderSelectedValueHint | ||
: undefined | ||
showSelectedHint ? renderSelectedValueHint : undefined | ||
} | ||
size={ translatedSize } | ||
store={ store } | ||
|
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.
@mirka Relatively to #63248 (comment), I applied your suggestion of adapting the
options
array at runtime where necessary