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

Fixes for EditInPlace control #179

Merged
merged 4 commits into from
Jun 14, 2024
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 @@ -15,7 +15,7 @@ limitations under the License.
*/

.container {
min-block-size: 110px;
min-block-size: 124px;
}

.label {
Expand Down Expand Up @@ -76,6 +76,7 @@ limitations under the License.
inline-size: 100%;
}

.container-show-buttons .button-group,
.container:focus-within .button-group {
display: inline-grid;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export default {
"savedLabel",
"saveButtonLabel",
"cancelButtonLabel",
"disableSaveButton",
],
},
design: {
Expand All @@ -52,7 +53,7 @@ export default {
type: "string",
},
disableSaveButton: {
type: "string",
type: "boolean",
},
onChange: {
action: "changed",
Expand Down
16 changes: 12 additions & 4 deletions src/components/Form/Controls/EditInPlace/EditInPlace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ type Props = {
saveButtonLabel: string;

/**
* True to disable the save button, false to enasble.
* True to disable the save button, false to enable.
* Default: false (enabled)
*/
disableSaveButton?: boolean;
Expand Down Expand Up @@ -103,12 +103,15 @@ export const EditInPlace = forwardRef<HTMLInputElement, Props>(
const id = useId();
const labelId = useId();
const errorTextId = useId();

const [showSaved, setShowSaved] = React.useState(false);
const [saving, setSaving] = React.useState(false);

const classes = classnames(styles.container, className, {
[styles["container-error"]]: Boolean(error),
[styles["container-show-buttons"]]: saving,
});

const [showSaved, setShowSaved] = React.useState(false);

const hideTimer = useRef<NodeJS.Timeout | null>(null);

const saveButtonRef = useRef<HTMLButtonElement | null>(null);
Expand All @@ -124,6 +127,7 @@ export const EditInPlace = forwardRef<HTMLInputElement, Props>(
async (e: FormEvent) => {
e.preventDefault();
try {
setSaving(true);
await onSave();
saveButtonRef.current?.blur();
setShowSaved(true);
Expand All @@ -134,6 +138,8 @@ export const EditInPlace = forwardRef<HTMLInputElement, Props>(
// We don't really need to do anything here, we just don't want to display the
// 'saved' label, obviously. The user of the component can update the error to
// show what failed.
} finally {
setSaving(false);
}
},
[setShowSaved, onSave, hideTimer],
Expand All @@ -157,6 +163,7 @@ export const EditInPlace = forwardRef<HTMLInputElement, Props>(
aria-labelledby={labelId}
aria-invalid={Boolean(error)}
aria-errormessage={error ? errorTextId : undefined}
disabled={saving}
/>
<div className={styles["button-group"]}>
<button
Expand All @@ -167,7 +174,7 @@ export const EditInPlace = forwardRef<HTMLInputElement, Props>(
ref={saveButtonRef}
aria-controls={id}
aria-label={saveButtonLabel}
disabled={disableSaveButton}
disabled={disableSaveButton || saving}
>
<CheckIcon />
</button>
Expand All @@ -179,6 +186,7 @@ export const EditInPlace = forwardRef<HTMLInputElement, Props>(
onClick={onCancelButtonClicked}
aria-controls={id}
aria-label={cancelButtonLabel}
disabled={saving}
>
<CancelIcon />
</button>
Expand Down
Loading