Skip to content

Commit

Permalink
fix: appearance of disabled inputs (#165)
Browse files Browse the repository at this point in the history
- disabled appearance of TextInput, PasswordInput and InputMenu
- update website examples to demonstrate various disabled components
  • Loading branch information
uipoet authored Jan 26, 2022
1 parent 59c62da commit 6458cff
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/docs/pages/examples/input-menu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import { InputMenuState, options, useInputMenuState } from "./state";

export function InputMenuExample() {
const state = useInputMenuState();
const [{ appearance, error, flex, helper, leadingIcon }] = state;
const [{ appearance, disabled, error, flex, helper, leadingIcon }] = state;

return (
<Card appearance="elevated" id="input-menu" title="InputMenu">
<section>
<section className={styles.center}>
<InputMenu
appearance={appearance}
disabled={disabled}
error={error && "Error message"}
flex={flex}
helper={helper ? "Helper message" : undefined}
Expand Down
8 changes: 8 additions & 0 deletions src/docs/pages/examples/input-menu/state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type Appearance = "filled" | "outlined";

type InputMenuProps = {
appearance: Appearance;
disabled: boolean;
error: boolean;
flex: boolean;
helper: boolean;
Expand All @@ -14,6 +15,7 @@ type InputMenuProps = {
export function useInputMenuState() {
return useState<InputMenuProps>({
appearance: "filled",
disabled: false,
error: false,
flex: true,
helper: false,
Expand Down Expand Up @@ -74,6 +76,12 @@ export function InputMenuState({
setState((state) => ({ ...state, error: !state.error }))
}
/>
<Checkbox
label="Disabled"
onClick={() =>
setState((state) => ({ ...state, disabled: !state.disabled }))
}
/>
</form>
</aside>
);
Expand Down
3 changes: 2 additions & 1 deletion src/docs/pages/examples/password-input/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { PasswordInputState, usePasswordInputState } from "./state";
export function PasswordInputExample() {
const ref = useRef<HTMLInputElement>(null);
const state = usePasswordInputState();
const [{ appearance, error, flex, helper, leadingIcon }] = state;
const [{ appearance, disabled, error, flex, helper, leadingIcon }] = state;

return (
<Card appearance="elevated" id="password-input" title="PasswordInput">
Expand All @@ -17,6 +17,7 @@ export function PasswordInputExample() {
<form autoComplete="off">
<PasswordInput
appearance={appearance}
disabled={disabled}
error={error && "Invalid password"}
flex={flex}
helper={helper ? "8 characters minimum" : undefined}
Expand Down
8 changes: 8 additions & 0 deletions src/docs/pages/examples/password-input/state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Dispatch, SetStateAction, useState } from "react";

type PasswordInputProps = {
appearance: "filled" | "outlined";
disabled: boolean;
error: boolean;
flex: boolean;
helper: boolean;
Expand All @@ -12,6 +13,7 @@ type PasswordInputProps = {
export function usePasswordInputState() {
return useState<PasswordInputProps>({
appearance: "filled",
disabled: false,
error: false,
flex: true,
helper: false,
Expand Down Expand Up @@ -72,6 +74,12 @@ export function PasswordInputState({
setState((state) => ({ ...state, error: !state.error }))
}
/>
<Checkbox
label="Disabled"
onClick={() =>
setState((state) => ({ ...state, disabled: !state.disabled }))
}
/>
</form>
</aside>
);
Expand Down
8 changes: 6 additions & 2 deletions src/docs/pages/examples/radioset/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { RadiosetState, useRadiosetState } from "./state";

export function RadiosetExample() {
const state = useRadiosetState();
const [{ disabled }] = state;
const [{ disabled, disabledOption }] = state;

return (
<Card appearance="elevated" id="radioset" title="RadioSet">
Expand All @@ -25,7 +25,11 @@ export function RadiosetExample() {
label: <span>Item Two</span>,
value: "item-two",
},
{ disabled: true, label: "Item Three", value: "item-three" },
{
disabled: disabledOption,
label: "Item Three",
value: "item-three",
},
]}
/>
</section>
Expand Down
11 changes: 11 additions & 0 deletions src/docs/pages/examples/radioset/state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { Dispatch, SetStateAction, useState } from "react";

type RadiosetProps = {
disabled: boolean;
disabledOption: boolean;
};

export function useRadiosetState() {
return useState<RadiosetProps>({
disabled: false,
disabledOption: false,
});
}

Expand All @@ -19,6 +21,15 @@ export function RadiosetState({
return (
<aside>
<form>
<Checkbox
label="Disabled option"
onClick={() =>
setState((state) => ({
...state,
disabledOption: !state.disabledOption,
}))
}
/>
<Checkbox
label="Disabled"
onClick={() =>
Expand Down
6 changes: 4 additions & 2 deletions src/docs/pages/examples/text-input/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import { TextInputState, useTextInputState } from "./state";
export function TextInputExample() {
const ref = useRef<HTMLInputElement>(null);
const state = useTextInputState();
const [{ appearance, error, flex, helper, leadingIcon, trailingIcon }] =
state;
const [
{ appearance, disabled, error, flex, helper, leadingIcon, trailingIcon },
] = state;

return (
<Card appearance="elevated" id="text-input" title="TextInput">
Expand All @@ -18,6 +19,7 @@ export function TextInputExample() {
<form autoComplete="off">
<TextInput
appearance={appearance}
disabled={disabled}
error={error && "Error message"}
flex={flex}
helper={helper ? "Helper message" : undefined}
Expand Down
8 changes: 8 additions & 0 deletions src/docs/pages/examples/text-input/state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Dispatch, SetStateAction, useState } from "react";

type TextInputProps = {
appearance: "filled" | "outlined";
disabled: boolean;
error: boolean;
flex: boolean;
helper: boolean;
Expand All @@ -13,6 +14,7 @@ type TextInputProps = {
export function useTextInputState() {
return useState<TextInputProps>({
appearance: "filled",
disabled: false,
error: false,
flex: true,
helper: false,
Expand Down Expand Up @@ -83,6 +85,12 @@ export function TextInputState({
setState((state) => ({ ...state, error: !state.error }))
}
/>
<Checkbox
label="Disabled"
onClick={() =>
setState((state) => ({ ...state, disabled: !state.disabled }))
}
/>
</form>
</aside>
);
Expand Down
3 changes: 3 additions & 0 deletions src/lib/components/input/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ export type InputProps = {

export function useClassName({
appearance = "filled",
disabled,
flex,
error,
leadingIcon,
override,
trailingIcon,
}: {
appearance?: Appearance;
disabled?: boolean;
error?: ReactNode;
flex?: boolean;
leadingIcon?: ReactNode;
Expand All @@ -42,6 +44,7 @@ export function useClassName({
styles[appearance],
trailingIcon ? styles.trailingIcon : undefined,
typography.labelLarge,
disabled ? styles.disabled : undefined,
]
.join(" ")
.trim();
Expand Down
12 changes: 12 additions & 0 deletions src/lib/components/input/input.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,18 @@
color: var(--md-color-error);
}

.field.disabled {
opacity: 0.38;
}

.field.filled.disabled > .row > .input {
box-shadow: 0 0.1rem 0 0 var(--md-color-outline);
}

.field.outlined.disabled > .row > .input {
box-shadow: 0 0 0 0.1rem var(--md-color-outline);
}

@media screen and (min-width: 600px) {
.input {
font-size: 1.4rem; /* follow Material 3 spec at desktop width */
Expand Down
3 changes: 3 additions & 0 deletions src/lib/components/input/text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const TextInput = forwardRef<
"aria-expanded": ariaExpanded,
"aria-invalid": ariaInvalid,
className: override,
disabled,
error,
flex,
helper,
Expand All @@ -28,6 +29,7 @@ export const TextInput = forwardRef<
) {
const className = useClassName({
appearance,
disabled,
error,
flex,
leadingIcon,
Expand All @@ -43,6 +45,7 @@ export const TextInput = forwardRef<
{...props}
aria-invalid={ariaInvalid || !!error}
className={styles.input}
disabled={disabled}
id={id}
placeholder={
placeholder || typeof label === "string"
Expand Down

0 comments on commit 6458cff

Please sign in to comment.