-
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
BoxControl: Convert to TypeScript #47622
Merged
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
51c052f
Rename index.tsx
mirka 42e639c
BoxControl: Add types
mirka 527cab8
Rename all-input-control.tsx
mirka 3a4e091
Rename unit-control.tsx
mirka 69df1ab
Add types
mirka 89768fd
Rename axial-input-controls.tsx
mirka 2da9696
Add types to axial input controls
mirka 100ea1d
Rename input-controls.tsx
mirka 245d110
Add types for input-controls
mirka b38a4b4
Rename icon.tsx
mirka e1960bd
Add types to icon
mirka 325629e
Add types to LinkedButton
mirka 11a6208
Add types for styles
mirka c049ba4
Improve types in utils
mirka c1fe951
Remove from tsconfig
mirka 57390da
Convert tests
mirka c6391b0
Add main JSDoc
mirka 1aeed0c
Add changelog
mirka f5c8246
Add description for `id` prop
mirka 8703310
Fix lint errors
mirka 56f225e
Remove copypasta in types
mirka 20f8fa6
Update formatting in readme
mirka c64c2a6
Add todo comment
mirka 1b1d1e5
Fixup optional
mirka fbcf0e8
Add default value
mirka 07f6cdf
Make onChange required
mirka 23e6620
Add default reset value to readme
mirka d4b7461
Add allowed `side` values to readme
mirka 229db2c
Fix default value for `label` in readme
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { UnitControlProps } from '../unit-control/types'; | ||
import type { BoxControlInputControlProps } from './types'; | ||
import UnitControl from './unit-control'; | ||
import { | ||
LABELS, | ||
|
@@ -22,18 +24,20 @@ export default function AllInputControl( { | |
selectedUnits, | ||
setSelectedUnits, | ||
...props | ||
} ) { | ||
}: BoxControlInputControlProps ) { | ||
const allValue = getAllValue( values, selectedUnits, sides ); | ||
const hasValues = isValuesDefined( values ); | ||
const isMixed = hasValues && isValuesMixed( values, selectedUnits, sides ); | ||
const allPlaceholder = isMixed ? LABELS.mixed : null; | ||
const allPlaceholder = isMixed ? LABELS.mixed : undefined; | ||
|
||
const handleOnFocus = ( event ) => { | ||
const handleOnFocus: React.FocusEventHandler< HTMLInputElement > = ( | ||
event | ||
) => { | ||
onFocus( event, { side: 'all' } ); | ||
}; | ||
|
||
const handleOnChange = ( next ) => { | ||
const isNumeric = ! isNaN( parseFloat( next ) ); | ||
const handleOnChange: UnitControlProps[ 'onChange' ] = ( next ) => { | ||
const isNumeric = next !== undefined && ! isNaN( parseFloat( next ) ); | ||
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. Just making the undefined case explicit. Should not change runtime behavior. |
||
const nextValue = isNumeric ? next : undefined; | ||
const nextValues = applyValueToSides( values, nextValue, sides ); | ||
|
||
|
@@ -42,7 +46,7 @@ export default function AllInputControl( { | |
|
||
// Set selected unit so it can be used as fallback by unlinked controls | ||
// when individual sides do not have a value containing a unit. | ||
const handleOnUnitChange = ( unit ) => { | ||
const handleOnUnitChange: UnitControlProps[ 'onUnitChange' ] = ( unit ) => { | ||
const newUnits = applyValueToSides( selectedUnits, unit, sides ); | ||
setSelectedUnits( newUnits ); | ||
}; | ||
|
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 |
---|---|---|
|
@@ -29,19 +29,50 @@ import { | |
isValuesDefined, | ||
} from './utils'; | ||
import { useControlledState } from '../utils/hooks'; | ||
import type { | ||
BoxControlIconProps, | ||
BoxControlProps, | ||
BoxControlValue, | ||
} from './types'; | ||
|
||
const defaultInputProps = { | ||
min: 0, | ||
}; | ||
|
||
const noop = () => {}; | ||
|
||
function useUniqueId( idProp ) { | ||
function useUniqueId( idProp?: string ) { | ||
const instanceId = useInstanceId( BoxControl, 'inspector-box-control' ); | ||
|
||
return idProp || instanceId; | ||
} | ||
export default function BoxControl( { | ||
|
||
/** | ||
* BoxControl components let users set values for Top, Right, Bottom, and Left. | ||
* This can be used as an input control for values like `padding` or `margin`. | ||
* | ||
* ```jsx | ||
* import { __experimentalBoxControl as BoxControl } from '@wordpress/components'; | ||
* import { useState } from '@wordpress/element'; | ||
* | ||
* const Example = () => { | ||
* const [ values, setValues ] = useState( { | ||
* top: '50px', | ||
* left: '10%', | ||
* right: '10%', | ||
* bottom: '50px', | ||
* } ); | ||
* | ||
* return ( | ||
* <BoxControl | ||
* values={ values } | ||
* onChange={ ( nextValues ) => setValues( nextValues ) } | ||
* /> | ||
* ); | ||
* }; | ||
* ``` | ||
*/ | ||
function BoxControl( { | ||
id: idProp, | ||
inputProps = defaultInputProps, | ||
onChange = noop, | ||
|
@@ -54,7 +85,7 @@ export default function BoxControl( { | |
resetValues = DEFAULT_VALUES, | ||
onMouseOver, | ||
onMouseOut, | ||
} ) { | ||
}: BoxControlProps ) { | ||
const [ values, setValues ] = useControlledState( valuesProp, { | ||
fallback: DEFAULT_VALUES, | ||
} ); | ||
|
@@ -67,14 +98,14 @@ export default function BoxControl( { | |
! hasInitialValue || ! isValuesMixed( inputValues ) || hasOneSide | ||
); | ||
|
||
const [ side, setSide ] = useState( | ||
const [ side, setSide ] = useState< BoxControlIconProps[ 'side' ] >( | ||
getInitialSide( isLinked, splitOnAxis ) | ||
); | ||
|
||
// Tracking selected units via internal state allows filtering of CSS unit | ||
// only values from being saved while maintaining preexisting unit selection | ||
// behaviour. Filtering CSS only values prevents invalid style values. | ||
const [ selectedUnits, setSelectedUnits ] = useState( { | ||
const [ selectedUnits, setSelectedUnits ] = useState< BoxControlValue >( { | ||
top: parseQuantityAndUnitFromRawValue( valuesProp?.top )[ 1 ], | ||
right: parseQuantityAndUnitFromRawValue( valuesProp?.right )[ 1 ], | ||
bottom: parseQuantityAndUnitFromRawValue( valuesProp?.bottom )[ 1 ], | ||
|
@@ -89,11 +120,14 @@ export default function BoxControl( { | |
setSide( getInitialSide( ! isLinked, splitOnAxis ) ); | ||
}; | ||
|
||
const handleOnFocus = ( event, { side: nextSide } ) => { | ||
const handleOnFocus = ( | ||
_event: React.FocusEvent< HTMLInputElement >, | ||
{ side: nextSide }: { side: typeof side } | ||
) => { | ||
setSide( nextSide ); | ||
}; | ||
|
||
const handleOnChange = ( nextValues ) => { | ||
const handleOnChange = ( nextValues: BoxControlValue ) => { | ||
onChange( nextValues ); | ||
setValues( nextValues ); | ||
setIsDirty( true ); | ||
|
@@ -132,7 +166,7 @@ export default function BoxControl( { | |
<FlexItem> | ||
<Button | ||
className="component-box-control__reset-button" | ||
isSecondary | ||
variant="secondary" | ||
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. Updated a deprecated prop. |
||
isSmall | ||
onClick={ handleOnReset } | ||
disabled={ ! isDirty } | ||
|
@@ -176,3 +210,4 @@ export default function BoxControl( { | |
} | ||
|
||
export { applyValueToSides } from './utils'; | ||
export default BoxControl; |
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.
The
placeholder
prop on inputs do not acceptnull
.