Skip to content
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

feat(text-input): add inline option for TextInput (#5915) #6252

Merged
merged 3 commits into from
Jul 9, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions packages/components/src/components/text-input/_text-input.scss
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,55 @@
.#{$prefix}--form--fluid .#{$prefix}--text-input-wrapper--light {
background: $field-02;
}

//-----------------------------
// Inline Text Input
//-----------------------------

.#{$prefix}--text-input-wrapper--inline {
flex-direction: row;
flex-wrap: wrap;
}

.#{$prefix}--label--inline {
flex: 1;
margin: rem(13px) 0 0 0;
overflow-wrap: break-word;
word-break: break-word;
}

.#{$prefix}--label--inline--sm {
margin-top: rem(9px);
}

.#{$prefix}--label--inline--xl {
margin-top: rem(17px);
}

.#{$prefix}--text-input__label-helper-wrapper {
flex: 2;
flex-direction: column;
margin-right: rem(24px);
max-width: rem(128px);
overflow-wrap: break-word;
}

.#{$prefix}--form__helper-text--inline {
margin-top: rem(2px);
}

.#{$prefix}--text-input__field-outer-wrapper {
align-items: flex-start;
display: flex;
flex: 1 1 auto;
flex-direction: column;
width: 100%;
}

.#{$prefix}--text-input__field-outer-wrapper--inline {
flex: 8;
flex-direction: column;
}
}

@include exports('text-input') {
Expand Down
4 changes: 4 additions & 0 deletions packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5267,6 +5267,7 @@ Map {
"defaultProps": Object {
"disabled": false,
"helperText": "",
"inline": false,
"invalid": false,
"invalidText": "",
"light": false,
Expand Down Expand Up @@ -5304,6 +5305,9 @@ Map {
"isRequired": true,
"type": "string",
},
"inline": Object {
"type": "bool",
},
"invalid": Object {
"type": "bool",
},
Expand Down
5 changes: 3 additions & 2 deletions packages/react/src/components/TextInput/TextInput-story.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const props = {
'This is not a default value'
),
size: select('Field size (size)', sizes, undefined) || undefined,
labelText: text('Label text (labelText)', 'Text Input label'),
labelText: text('Label text (labelText)', 'Text input label'),
placeholder: text('Placeholder text (placeholder)', 'Placeholder text'),
light: boolean('Light variant (light)', false),
disabled: boolean('Disabled (disabled)', false),
Expand All @@ -66,7 +66,8 @@ const props = {
'Form validation UI content (invalidText)',
'A valid value is required'
),
helperText: text('Helper text (helperText)', 'Optional helper text.'),
helperText: text('Helper text (helperText)', 'Optional help text'),
inline: boolean('Inline variant (inline)', false),
onClick: action('onClick'),
onChange: action('onChange'),
}),
Expand Down
53 changes: 40 additions & 13 deletions packages/react/src/components/TextInput/TextInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const TextInput = React.forwardRef(function TextInput(
helperText,
light,
size,
inline,
...other
},
ref
Expand Down Expand Up @@ -65,15 +66,25 @@ const TextInput = React.forwardRef(function TextInput(
`${prefix}--text-input-wrapper`,
{
[`${prefix}--text-input-wrapper--light`]: light,
[`${prefix}--text-input-wrapper--inline`]: inline,
}
);
const labelClasses = classNames(`${prefix}--label`, {
[`${prefix}--visually-hidden`]: hideLabel,
[`${prefix}--label--disabled`]: other.disabled,
[`${prefix}--label--inline`]: inline,
[`${prefix}--label--inline--${size}`]: inline && !!size,
});
const helperTextClasses = classNames(`${prefix}--form__helper-text`, {
[`${prefix}--form__helper-text--disabled`]: other.disabled,
[`${prefix}--form__helper-text--inline`]: inline,
});
const fieldOuterWrapperClasses = classNames(
`${prefix}--text-input__field-outer-wrapper`,
{
[`${prefix}--text-input__field-outer-wrapper--inline`]: inline,
}
);
const label = labelText ? (
<label htmlFor={id} className={labelClasses}>
{labelText}
Expand All @@ -95,20 +106,31 @@ const TextInput = React.forwardRef(function TextInput(

return (
<div className={inputWrapperClasses}>
{label}
<div
className={`${prefix}--text-input__field-wrapper`}
data-invalid={invalid || null}>
{invalid && (
<WarningFilled16 className={`${prefix}--text-input__invalid-icon`} />
)}
{input}
{isFluid && <hr className={`${prefix}--text-input__divider`} />}
{/* <hr className={`${prefix}--text-input__divider`} /> */}
{isFluid ? error : null}
{!inline ? (
label
) : (
<div className={`${prefix}--text-input__label-helper-wrapper`}>
{label}
{!isFluid && helper}
</div>
)}
<div className={fieldOuterWrapperClasses}>
<div
className={`${prefix}--text-input__field-wrapper`}
data-invalid={invalid || null}>
{invalid && (
<WarningFilled16
className={`${prefix}--text-input__invalid-icon`}
/>
)}
{input}
{isFluid && <hr className={`${prefix}--text-input__divider`} />}
{/* <hr className={`${prefix}--text-input__divider`} /> */}
{isFluid && !inline && error}
</div>
{!isFluid && error}
{!invalid && !isFluid && !inline && helper}
</div>
{isFluid ? null : error}
{!invalid && !isFluid && helper}
</div>
);
});
Expand Down Expand Up @@ -198,6 +220,10 @@ TextInput.propTypes = {
* `true` to use the light version.
*/
light: PropTypes.bool,
/**
* `true` to use the inline version.
*/
inline: PropTypes.bool,
};

TextInput.defaultProps = {
Expand All @@ -209,6 +235,7 @@ TextInput.defaultProps = {
invalidText: '',
helperText: '',
light: false,
inline: false,
};

export default TextInput;