Skip to content

Commit

Permalink
input: add disabled state
Browse files Browse the repository at this point in the history
  • Loading branch information
JBWatenbergScality committed Feb 28, 2022
1 parent 4079515 commit 9a4d787
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 15 deletions.
32 changes: 19 additions & 13 deletions src/lib/components/input/Input.component.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
//@flow
import React, { useRef } from "react";
import { DebounceInput } from "react-debounce-input";
import Checkbox from "../checkbox/Checkbox.component";
import Select from "../select/Select.component";
import TextArea from "../textarea/TextArea.component";
import React, { useRef } from 'react';
import { DebounceInput } from 'react-debounce-input';
import Checkbox from '../checkbox/Checkbox.component';
import Select from '../select/Select.component';
import TextArea from '../textarea/TextArea.component';
import {
InputContainer,
LabelStyle,
InputErrorMessage,
InputWrapper,
} from "./Input.component.style";
} from './Input.component.style';

export type Item = {
label: string,
Expand All @@ -18,7 +18,7 @@ export type Item = {

type Items = Array<Item>;

type Props = {
export type InputProps = {
value: any,
type?: string,
label?: string,
Expand All @@ -28,6 +28,7 @@ type Props = {
checked?: boolean,
onChange: (e: SyntheticEvent<HTMLInputElement>) => void,
options?: Items,
disabled?: boolean,
min?: string,
max?: string,
};
Expand All @@ -52,12 +53,12 @@ const InputRenderer = ({
*/
const valuePropDescriptor = Object.getOwnPropertyDescriptor(
window.HTMLInputElement.prototype,
"value",
'value',
);
if (valuePropDescriptor && inputEl.current) {
const nativeInputValueSetter = valuePropDescriptor.set;
if (nativeInputValueSetter) {
if (e.target.dataset.core === "up")
if (e.target.dataset.core === 'up')
nativeInputValueSetter.call(
inputEl.current,
max
Expand All @@ -76,7 +77,7 @@ const InputRenderer = ({
: parseInt(value) - 1,
);
}
const event = new Event("input", { bubbles: true });
const event = new Event('input', { bubbles: true });
inputEl.current.dispatchEvent(event);
}
};
Expand Down Expand Up @@ -146,16 +147,21 @@ const InputRenderer = ({
else return input.text;
};

const Input = ({ label, id, error, type, ...rest }: Props) => {
const Input = ({ label, id, error, type, disabled, ...rest }: InputProps) => {
return (
<InputContainer className="sc-input" error={error} type={type}>
<InputContainer
className="sc-input"
disabled={disabled}
error={error}
type={type}
>
{label && (
<LabelStyle htmlFor={id} className="sc-input-label">
{label}
</LabelStyle>
)}
<InputWrapper className="sc-input-wrapper">
<InputRenderer id={id} type={type} {...rest} />
<InputRenderer id={id} type={type} disabled={disabled} {...rest} />
{error && (
<InputErrorMessage className="sc-input-error">
{error}
Expand Down
12 changes: 11 additions & 1 deletion src/lib/components/input/Input.component.style.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@ import { getTheme, getThemePropSelector } from "../../utils";

export const InputContainer = styled.div`
display: inline-flex;
${(props) => {
if (props.disabled) {
return css`
opacity: 0.3;
input, textarea, select, i {
cursor: not-allowed;
}
`;
}
}}
.sc-checkbox {
margin: ${defaultTheme.padding.smaller} 0;
}
.sc-select {
&.sc-select {
width: 200px;
}
Expand Down
21 changes: 20 additions & 1 deletion src/lib/components/searchinput/SearchInput.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,23 @@ export type Props = {
onChange: (e: SyntheticEvent<HTMLInputElement>) => void,
onReset?: () => void,
disableToggle: boolean,
disabled?: boolean,
};

const SearchInputContainer = styled.div`
position: relative;
${(props) => {
if (props.disabled) {
return css`
i {
opacity: 0.3;
cursor: not-allowed;
}
`;
}
}}
.sc-input {
display: block;
input[type='text'] {
Expand Down Expand Up @@ -67,6 +79,7 @@ function SearchInput({
value,
onChange,
onReset,
disabled,
...rest
}: Props) {
const [docked, setDocked] = useState(!disableToggle);
Expand All @@ -90,7 +103,12 @@ function SearchInput({
};

return (
<SearchInputContainer className="sc-searchinput" docked={docked} {...rest}>
<SearchInputContainer
className="sc-searchinput"
disabled={disabled}
docked={docked}
{...rest}
>
<Input
minLength={1}
debounceTimeout={300}
Expand All @@ -99,6 +117,7 @@ function SearchInput({
placeholder={placeholder}
value={value}
onChange={onChange}
disabled={disabled}
inputRef={myInputRef}
/>
<SearchIcon onClick={toggle} disabled={!docked}>
Expand Down
8 changes: 8 additions & 0 deletions stories/input.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ const ExampleInput = () => {
onChange={action('onChange')}
data-cy="default_input"
/>
<Title>Input disabled</Title>
<Input
id="id1"
value="value"
disabled={true}
onChange={action('onChange')}
data-cy="default_input"
/>
<Title>Input with label</Title>
<Input
id="id2"
Expand Down
11 changes: 11 additions & 0 deletions stories/searchinput.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ export const Default = () => {
disableToggle={false}
/>
</div>
<Title>Disabled</Title>
<div style={{ width: '250px' }}>
<SearchInput
value=""
disabled={true}
placeholder="Search server..."
onChange={action('on input change')}
onReset={action('on input reset')}
disableToggle={true}
/>
</div>
<Title>Search Input filled</Title>
<div style={{ width: '250px' }}>
<SearchInput
Expand Down

0 comments on commit 9a4d787

Please sign in to comment.