-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
SelectControl: Finish typescript migration #40737
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c4971dd
SelectControl: Move types
mirka 3fa1e48
Inherit types from underlying components
mirka 597b37d
Add named export
mirka 053a991
Convert stories to TS and Controls
mirka 2c549f9
Convert tests
mirka aabeb04
Add changelog
mirka c026862
Simplify size prop types
mirka e00184f
Use `text` control for prefix/suffix props
mirka f0336cc
Merge branch 'trunk' into typescript-select-control
mirka 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 |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
*/ | ||
import { isEmpty, noop } from 'lodash'; | ||
import classNames from 'classnames'; | ||
import type { ChangeEvent, FocusEvent, ReactNode, ForwardedRef } from 'react'; | ||
import type { ChangeEvent, FocusEvent, ForwardedRef } from 'react'; | ||
|
||
/** | ||
* WordPress dependencies | ||
|
@@ -17,10 +17,9 @@ import { Icon, chevronDown } from '@wordpress/icons'; | |
*/ | ||
import BaseControl from '../base-control'; | ||
import InputBase from '../input-control/input-base'; | ||
import type { InputBaseProps, LabelPosition } from '../input-control/types'; | ||
import { Select, DownArrowWrapper } from './styles/select-control-styles'; | ||
import type { Size } from './types'; | ||
import type { WordPressComponentProps } from '../ui/context'; | ||
import type { SelectControlProps } from './types'; | ||
|
||
function useUniqueId( idProp?: string ) { | ||
const instanceId = useInstanceId( SelectControl ); | ||
|
@@ -29,30 +28,7 @@ function useUniqueId( idProp?: string ) { | |
return idProp || id; | ||
} | ||
|
||
export interface SelectControlProps | ||
extends Omit< InputBaseProps, 'children' | 'isFocused' > { | ||
help?: string; | ||
hideLabelFromVision?: boolean; | ||
multiple?: boolean; | ||
onBlur?: ( event: FocusEvent< HTMLSelectElement > ) => void; | ||
onFocus?: ( event: FocusEvent< HTMLSelectElement > ) => void; | ||
onChange?: ( | ||
value: string | string[], | ||
extra?: { event?: ChangeEvent< HTMLSelectElement > } | ||
) => void; | ||
options?: { | ||
label: string; | ||
value: string; | ||
id?: string; | ||
disabled?: boolean; | ||
}[]; | ||
size?: Size; | ||
value?: string | string[]; | ||
labelPosition?: LabelPosition; | ||
children?: ReactNode; | ||
} | ||
|
||
function SelectControl( | ||
function UnforwardedSelectControl( | ||
{ | ||
className, | ||
disabled = false, | ||
|
@@ -165,6 +141,31 @@ function SelectControl( | |
/* eslint-enable jsx-a11y/no-onchange */ | ||
} | ||
|
||
const ForwardedComponent = forwardRef( SelectControl ); | ||
/** | ||
* `SelectControl` allows users to select from a single or multiple option menu. | ||
* It functions as a wrapper around the browser's native `<select>` element. | ||
* | ||
* @example | ||
* import { SelectControl } from '@wordpress/components'; | ||
* import { useState } from '@wordpress/element'; | ||
* | ||
* const MySelectControl = () => { | ||
* const [ size, setSize ] = useState( '50%' ); | ||
* | ||
* return ( | ||
* <SelectControl | ||
* label="Size" | ||
* value={ size } | ||
* options={ [ | ||
* { label: 'Big', value: '100%' }, | ||
* { label: 'Medium', value: '50%' }, | ||
* { label: 'Small', value: '25%' }, | ||
* ] } | ||
* onChange={ setSize } | ||
* /> | ||
* ); | ||
* }; | ||
*/ | ||
export const SelectControl = forwardRef( UnforwardedSelectControl ); | ||
Comment on lines
+144
to
+169
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. Thank you for adding the JSDoc example 🎉 |
||
|
||
export default ForwardedComponent; | ||
export default SelectControl; |
104 changes: 0 additions & 104 deletions
104
packages/components/src/select-control/stories/index.js
This file was deleted.
Oops, something went wrong.
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,90 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import type { ComponentMeta, ComponentStory } from '@storybook/react'; | ||
import type { ComponentProps } from 'react'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useState } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import SelectControl from '..'; | ||
|
||
const meta: ComponentMeta< typeof SelectControl > = { | ||
title: 'Components/SelectControl', | ||
component: SelectControl, | ||
argTypes: { | ||
help: { control: { type: 'text' } }, | ||
label: { control: { type: 'text' } }, | ||
prefix: { control: { type: 'text' } }, | ||
suffix: { control: { type: 'text' } }, | ||
value: { control: { type: null } }, | ||
mirka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
parameters: { | ||
controls: { expanded: true }, | ||
docs: { source: { state: 'open' } }, | ||
}, | ||
}; | ||
export default meta; | ||
|
||
const SelectControlWithState: ComponentStory< typeof SelectControl > = ( | ||
args | ||
) => { | ||
const [ selection, setSelection ] = useState< | ||
ComponentProps< typeof SelectControl >[ 'value' ] | ||
>(); | ||
|
||
return ( | ||
<SelectControl | ||
{ ...args } | ||
value={ selection } | ||
onChange={ setSelection } | ||
/> | ||
); | ||
}; | ||
|
||
export const Default = SelectControlWithState.bind( {} ); | ||
Default.args = { | ||
options: [ | ||
{ value: '', label: 'Select an Option', disabled: true }, | ||
{ value: 'a', label: 'Option A' }, | ||
{ value: 'b', label: 'Option B' }, | ||
{ value: 'c', label: 'Option C' }, | ||
], | ||
}; | ||
|
||
export const WithLabelAndHelpText = SelectControlWithState.bind( {} ); | ||
WithLabelAndHelpText.args = { | ||
...Default.args, | ||
help: 'Help text to explain the select control.', | ||
label: 'Value', | ||
}; | ||
|
||
/** | ||
* As an alternative to the `options` prop, `optgroup`s and `options` can be | ||
* passed in as `children` for more customizability. | ||
*/ | ||
export const WithCustomChildren: ComponentStory< typeof SelectControl > = ( | ||
args | ||
) => { | ||
return ( | ||
<SelectControlWithState { ...args }> | ||
<option value="option-1">Option 1</option> | ||
<option value="option-2" disabled> | ||
Option 2 - Disabled | ||
</option> | ||
<optgroup label="Option Group 1"> | ||
<option value="option-group-1-option-1"> | ||
Option Group 1 - Option 1 | ||
</option> | ||
<option value="option-group-1-option-2" disabled> | ||
Option Group 1 - Option 2 - Disabled | ||
</option> | ||
</optgroup> | ||
</SelectControlWithState> | ||
); | ||
}; |
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
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.
null
is not a valid value. Caught by TS in StorybookThere 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.
So good! That's exactly what TypeScript is great at.