feat(TextInput): add helper util for left trim - #4691
Conversation
|
PF4 preview: https://patternfly-react-pr-4691.surge.sh |
Codecov Report
@@ Coverage Diff @@
## master #4691 +/- ##
==========================================
- Coverage 52.52% 52.30% -0.23%
==========================================
Files 514 514
Lines 8974 9030 +56
Branches 3266 3281 +15
==========================================
+ Hits 4714 4723 +9
- Misses 3673 3718 +45
- Partials 587 589 +2
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
| @@ -26,7 +26,7 @@ class SimpleTextInput extends React.Component { | |||
| const { value } = this.state; | |||
There was a problem hiding this comment.
Undo changes to this file
|
Also, can you add a separate example to TextInput.md instead of modifying the Disabled example? |
| componentDidMount() { | ||
| if (this.props.isLeftTruncated) { | ||
| this.handleResize(); | ||
| window.addEventListener('resize', debounce(this.handleResize, 250)); |
There was a problem hiding this comment.
i just realized that the input box dimensions can change even if there was no window resize, for example if a sidebar is opened. Perhaps we should look at something like the ResizeObserver instead
https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver
Example implementation
https://github.com/ZeeCoder/use-resize-observer
There was a problem hiding this comment.
Opened followup issue for next milestone here #4710 @jenny-s51 @tlabaj
| isRequired, | ||
| isDisabled, | ||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| onFocus, |
There was a problem hiding this comment.
you'll want to extract isLeftTruncated here even though it is not used in the returned JSX so it doesn't end up in the DOM. Can combine the 3 unused props in this way
/* eslint-disable @typescript-eslint/no-unused-vars */
isLeftTruncated,
onFocus,
onBlur,
/* eslint-enable @typescript-eslint/no-unused-vars */
mcarrano
left a comment
There was a problem hiding this comment.
So how would this work for different components? I can see it implemented for the Text input, but what if I wanted on a different component like a Table or a Select? Would it need to be applied on a per component basis or is there a general way to do this? Also, we typically present a tooltip for truncated text strings, not sure if that would be part of the component demo or not.
|
@mcarrano with this PR there will be some utility functions to make it easier to implement for others, but we'll have to add and test it per component. |
|
Good points @jschuler . Are the utility functions documented somewhere? If I were a developer wanting to apply this on my own to a different component. Would it be obvious how to do that or would they need to wait for us to add it as a new library update? |
|
@mcarrano Currently they are only documented in code! But we do export all the functions out so consumers could use them if they wanted to. I think the utility function would be pretty simple to use! |
tlabaj
left a comment
There was a problem hiding this comment.
Can you just update demo app and integration test please.
|
Nevermind, turns out the value has to be changed via state |
| export const trimLeft = (node: HTMLElement, value: string) => { | ||
| const availableWidth = innerDimensions(node).width; | ||
| let newValue = value; | ||
| if (getTextWidth(value, node) > availableWidth) { |
There was a problem hiding this comment.
just one bug here, can you add an else condition to this if. Noticed that if you just a little overflow and you expand the textbox until the whole text should fit again, it won't restore the value
else {
if ((node as HTMLInputElement).value) {
(node as HTMLInputElement).value = value;
} else {
node.innerText = value;
}
}
|
Your changes have been released in:
Thanks for your contribution! 🎉 |
What: Closes #4637
This is WIP; working on adding this functionality to
TextInput.