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(number-input): match readonly variant #8992

Merged
merged 3 commits into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@
padding-right: rem(112px);
}

.#{$prefix}--number input[type='number']:disabled,
.#{$prefix}--number--readonly input[type='number'] {
.#{$prefix}--number input[type='number']:disabled {
border-bottom-color: transparent;
background-color: $field-disabled;
color: $text-disabled;
Expand Down Expand Up @@ -296,10 +295,6 @@
background-color: transparent;
}

.#{$prefix}--number--readonly .#{$prefix}--number__control-btn {
display: none;
}

.#{$prefix}--number__invalid {
position: absolute;
right: rem(96px);
Expand Down Expand Up @@ -363,10 +358,7 @@
}

// V11: Possibly deprecate
.#{$prefix}--number--light input[type='number']:disabled,
.#{$prefix}--number--light
.#{$prefix}--number--readonly
input[type='number'] {
.#{$prefix}--number--light input[type='number']:disabled {
background-color: $field-02;
}

Expand Down Expand Up @@ -445,6 +437,20 @@
right: rem(16px);
}

// Readonly
.#{$prefix}--number--readonly input[type='number'] {
background: transparent;
}

.#{$prefix}--number--readonly .#{$prefix}--number__controls {
display: none;
}

.#{$prefix}--number__readonly-icon {
position: absolute;
right: rem(16px);
}

// Skeleton State
.#{$prefix}--number.#{$prefix}--skeleton {
@include skeleton;
Expand Down
119 changes: 61 additions & 58 deletions packages/react/src/components/NumberInput/NumberInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ import PropTypes from 'prop-types';
import React, { Component } from 'react';
import classNames from 'classnames';
import { settings } from 'carbon-components';
import {
Add16,
Subtract16,
WarningFilled16,
WarningAltFilled16,
} from '@carbon/icons-react';
import { Add16, Subtract16 } from '@carbon/icons-react';
import mergeRefs from '../../tools/mergeRefs';
import requiredIfValueExists from '../../prop-types/requiredIfValueExists';
// replace "use" prefix to avoid react thinking this is a hook that
// can only be placed in a function component
import { useNormalizedInputProps as getNormalizedInputProps } from '../../internal/useNormalizedInputProps';
import { useControlledStateWithValue } from '../../internal/FeatureFlags';
import deprecate from '../../prop-types/deprecate';

Expand Down Expand Up @@ -359,29 +357,6 @@ class NumberInput extends Component {
}
);

const props = {
disabled,
id,
max,
min,
step,
onChange: this.handleChange,
value:
useControlledStateWithValue && this.isControlled
? value
: this.state.value,
readOnly,
'aria-label': label ? null : ariaLabel,
};

const buttonProps = {
disabled,
};

const inputWrapperProps = {};
let errorId = null;
let error = null;

let isInputInvalid;

// If the user supplied `invalid` through props, we'll defer to the passed in value
Expand All @@ -402,33 +377,51 @@ class NumberInput extends Component {
}
}

if (isInputInvalid) {
const normalizedProps = getNormalizedInputProps({
id,
readOnly,
disabled,
invalid: isInputInvalid,
invalidText,
warn,
warnText,
});

const props = {
disabled: normalizedProps.disabled,
id,
max,
min,
step,
onChange: this.handleChange,
value:
useControlledStateWithValue && this.isControlled
? value
: this.state.value,
readOnly,
'aria-label': label ? null : ariaLabel,
};

const buttonProps = {
disabled,
};

const inputWrapperProps = {};

if (normalizedProps.invalid) {
inputWrapperProps['data-invalid'] = true;
errorId = `${id}-error-id`;
error = (
<div className={`${prefix}--form-requirement`} id={errorId}>
{invalidText}
</div>
);
} else if (warn) {
errorId = `${id}-error-id`;
error = (
<div className={`${prefix}--form-requirement`} id={errorId}>
{warnText}
</div>
);
}

const helperTextClasses = classNames(`${prefix}--form__helper-text`, {
[`${prefix}--form__helper-text--disabled`]: disabled,
[`${prefix}--form__helper-text--disabled`]: normalizedProps.disabled,
});

const helper = helperText ? (
<div className={helperTextClasses}>{helperText}</div>
) : null;

const labelClasses = classNames(`${prefix}--label`, {
[`${prefix}--label--disabled`]: disabled,
[`${prefix}--label--disabled`]: normalizedProps.disabled,
[`${prefix}--visually-hidden`]: hideLabel,
});

Expand All @@ -444,9 +437,24 @@ class NumberInput extends Component {
];

const wrapperClasses = classNames(`${prefix}--number__input-wrapper`, {
[`${prefix}--number__input-wrapper--warning`]: !isInputInvalid && warn,
[`${prefix}--number__input-wrapper--warning`]: normalizedProps.warn,
});

const iconClasses = classNames({
[`${prefix}--number__invalid`]:
normalizedProps.invalid || normalizedProps.warn,
[`${prefix}--number__invalid--warning`]: normalizedProps.warn,
[`${prefix}--number__readonly-icon`]: readOnly,
});

let ariaDescribedBy = null;
if (normalizedProps.invalid) {
ariaDescribedBy = normalizedProps.invalidId;
}
if (normalizedProps.warn) {
ariaDescribedBy = normalizedProps.warnId;
}

return (
<div className={`${prefix}--form-item`}>
<div className={numberInputClasses} {...inputWrapperProps}>
Expand All @@ -456,22 +464,17 @@ class NumberInput extends Component {
{labelText}
<div className={wrapperClasses}>
<input
data-invalid={isInputInvalid}
aria-invalid={isInputInvalid}
aria-describedby={errorId}
data-invalid={normalizedProps.invalid}
aria-invalid={normalizedProps.invalid}
aria-describedby={ariaDescribedBy}
type="number"
pattern="[0-9]*"
{...other}
{...props}
ref={mergeRefs(ref, this._handleInputRef)}
/>
{isInputInvalid && (
<WarningFilled16 className={`${prefix}--number__invalid`} />
)}
{!isInputInvalid && warn && (
<WarningAltFilled16
className={`${prefix}--number__invalid ${prefix}--number__invalid--warning`}
/>
{normalizedProps.icon && (
<normalizedProps.icon className={iconClasses} />
)}
{!hideSteppers && (
<div className={`${prefix}--number__controls`}>
Expand Down Expand Up @@ -500,11 +503,11 @@ class NumberInput extends Component {
</div>
)}
</div>
{error ? null : helper}
{normalizedProps.validation ? null : helper}
</>
);
})()}
{error}
{normalizedProps.validation}
</div>
</div>
);
Expand Down