diff --git a/src-docs/src/views/inline_edit/inline_edit_confirm.tsx b/src-docs/src/views/inline_edit/inline_edit_confirm.tsx deleted file mode 100644 index 432fcd73e42..00000000000 --- a/src-docs/src/views/inline_edit/inline_edit_confirm.tsx +++ /dev/null @@ -1,22 +0,0 @@ -import React from 'react'; - -import { EuiInlineEditText } from '../../../../src'; - -export default () => { - const confirmInlineEditChanges = () => { - // eslint-disable-next-line no-restricted-globals - const flag = confirm('Are you sure you want to save?') ? true : false; - return flag; - }; - - return ( - <> - - - ); -}; diff --git a/src-docs/src/views/inline_edit/inline_edit_example.js b/src-docs/src/views/inline_edit/inline_edit_example.js index 7d0e95b0a0d..8e4dde90c6b 100644 --- a/src-docs/src/views/inline_edit/inline_edit_example.js +++ b/src-docs/src/views/inline_edit/inline_edit_example.js @@ -23,11 +23,8 @@ const inlineEditModePropsSource = require('!!raw-loader!./inline_edit_mode_props import InlineEditSave from './inline_edit_save'; const inlineEditSaveSource = require('!!raw-loader!././inline_edit_save'); -import InlineEditConfirm from './inline_edit_confirm'; -const inlineEditConfirmSource = require('!!raw-loader!././inline_edit_confirm'); - -import InlineEditStates from './inline_edit_states'; -const inlineEditStatesSource = require('!!raw-loader!././inline_edit_states'); +import InlineEditValidation from './inline_edit_validation'; +const inlineEditValidationSource = require('!!raw-loader!././inline_edit_validation'); export const InlineEditExample = { title: 'Inline edit', @@ -87,14 +84,12 @@ export const InlineEditExample = { { title: 'Saving edited text', text: ( - <> -

- Use the onSave property to retrieve the value of - the edited text when the save button is pressed, and the{' '} - onConfirm callback (if passed) returns{' '} - true .{' '} -

- +

+ Use the onSave property to retrieve the value of + the edited text when the save button is pressed.{' '} + onSave does not fire if the user cancels their + edit. +

), source: [ { @@ -105,75 +100,84 @@ export const InlineEditExample = { demo: , }, { - title: 'Loading and invalid states', + title: 'Validating edited text', text: ( <>

- Setting the isLoading prop to true will add a - spinner to the input element in editMode and add - the loading state to the confirm and cancel input buttons. + Validation states (isLoading and{' '} + isInvalid) only display while the user is in edit + mode.

- Setting the isInvalid prop to true will display{' '} - EuiInlineEdit's error state. Optionally, use{' '} - editModeProps.formRowProps.error to pass an error - message that will be displayed on the form control. + To validate text when the user presses the save button but before + the user is returned to read mode, return a boolean (or an async + promise returning a boolean) from your onSave{' '} + callback.

- - ), - source: [ - { - type: GuideSectionTypes.TSX, - code: inlineEditStatesSource, - }, - ], - demo: , - }, - { - title: 'Confirm inline edit', - text: ( - <>

- Use the onConfirm property to pass a function - that will prompt users to confirm their changes. + Returning false from onSave{' '} + will keep the user in edit mode, where you can then display + validation state and messages. Returning true or{' '} + undefined will return the user to read mode.

), source: [ { type: GuideSectionTypes.TSX, - code: inlineEditConfirmSource, + code: inlineEditValidationSource, }, ], - demo: , + demo: , }, { title: 'Customizing read and edit modes', text: ( <>

- Customize the readMode state by passing{' '} - readModeProps. readMode{' '} - accepts{' '} + Customize the read mode by passing readModeProps, + which accepts any{' '} EuiButtonEmpty {' '} - properties with the exception of onClick. + properties.

- Customize the editMode state by passing{' '} - editModeProps. These properties are applied - directly to the{' '} - - EuiFormRow - {' '} - and{' '} - - EuiFieldText - {' '} - components. + Customize the edit mode by passing editModeProps. + This prop contains nested object properties that are applied to + various child components in edit mode:

+
    +
  • + editMode.formRowProps accepts any{' '} + + EuiFormRow + {' '} + properties +
  • +
  • + editMode.inputRowProps accepts any{' '} + + EuiFieldText + {' '} + properties +
  • +
  • + editMode.saveButtonProps accepts any{' '} + + EuiIconButton + {' '} + properties +
  • +
  • + editMode.cancelButtonProps accepts any{' '} + + EuiIconButton + {' '} + properties +
  • +
), source: [ diff --git a/src-docs/src/views/inline_edit/inline_edit_mode_props.tsx b/src-docs/src/views/inline_edit/inline_edit_mode_props.tsx index c855d91707d..e2958b9177c 100644 --- a/src-docs/src/views/inline_edit/inline_edit_mode_props.tsx +++ b/src-docs/src/views/inline_edit/inline_edit_mode_props.tsx @@ -8,10 +8,22 @@ export default () => { inputAriaLabel="Edit text inline for readMode and editMode props" defaultValue="This inline edit component has been customized!" size="m" - readModeProps={{ color: 'primary', iconSide: 'left' }} + readModeProps={{ + color: 'primary', + iconSide: 'left', + }} editModeProps={{ inputProps: { - prepend: 'Prepend Example', + prepend: 'Prepend example', + }, + formRowProps: { + helpText: 'Example help text', + }, + saveButtonProps: { + color: 'primary', + }, + cancelButtonProps: { + display: 'empty', }, }} /> diff --git a/src-docs/src/views/inline_edit/inline_edit_save.tsx b/src-docs/src/views/inline_edit/inline_edit_save.tsx index 1dc8017d567..9b01f695a56 100644 --- a/src-docs/src/views/inline_edit/inline_edit_save.tsx +++ b/src-docs/src/views/inline_edit/inline_edit_save.tsx @@ -1,34 +1,21 @@ import React from 'react'; -import { EuiButton, EuiInlineEditText, EuiSpacer } from '../../../../src'; +import { EuiInlineEditText } from '../../../../src'; export default () => { const saveToLocalStorage = (newInlineEditValue: string) => { localStorage.setItem('inlineEditValue', newInlineEditValue); }; - const removeFromLocalStorage = () => { - localStorage.removeItem('inlineEditValue'); - }; - const defaultInlineEditValue = localStorage.getItem('inlineEditValue') || 'This value will persist when you refresh the page!'; return ( - <> - saveToLocalStorage(onSaveVal)} - /> - - - - - Remove saved value from local storage - - + ); }; diff --git a/src-docs/src/views/inline_edit/inline_edit_states.tsx b/src-docs/src/views/inline_edit/inline_edit_states.tsx deleted file mode 100644 index 76eb115b944..00000000000 --- a/src-docs/src/views/inline_edit/inline_edit_states.tsx +++ /dev/null @@ -1,45 +0,0 @@ -import React, { useState } from 'react'; -import { - EuiFlexGroup, - EuiInlineEditText, - EuiSwitch, - EuiSpacer, -} from '../../../../src'; - -export default () => { - const [toggleLoading, setToggleLoading] = useState(true); - - const [toggleValid, setToggleValid] = useState(false); - - const errorMessage = ["Here's an example of an error"]; - - return ( - <> - - setToggleLoading(e.target.checked)} - /> - - setToggleValid(e.target.checked)} - /> - - - - - - - ); -}; diff --git a/src-docs/src/views/inline_edit/inline_edit_validation.tsx b/src-docs/src/views/inline_edit/inline_edit_validation.tsx new file mode 100644 index 00000000000..4c178eb9440 --- /dev/null +++ b/src-docs/src/views/inline_edit/inline_edit_validation.tsx @@ -0,0 +1,53 @@ +import React, { useState } from 'react'; + +import { EuiInlineEditText } from '../../../../src'; + +export default () => { + const [isLoading, setIsLoading] = useState(false); + const [errors, setErrors] = useState([]); + const isInvalid = errors.length > 0; + + const mockApiCall = (value: string) => + new Promise((resolve) => { + localStorage.setItem('inlineEditValueValidated', value); + setTimeout(resolve, 3000); + }); + + const defaultInlineEditValue = + localStorage.getItem('inlineEditValueValidated') || + 'This value will persist when you refresh the page!'; + + return ( + <> + setErrors([]) }, + }} + isInvalid={isInvalid} + isLoading={isLoading} + onSave={async (value) => { + // Validate edited text + if (!value) { + setErrors(['Please enter text.']); + return false; + } else if (value.length > 20) { + setErrors([ + 'Your text is too long - please enter less than 20 characters', + ]); + return false; + } + + // Clear errors, set loading state, and "call" an API + setErrors([]); + setIsLoading(true); + await mockApiCall(value); + setIsLoading(false); + return true; + }} + /> + + ); +}; diff --git a/src/components/inline_edit/__snapshots__/inline_edit_form.test.tsx.snap b/src/components/inline_edit/__snapshots__/inline_edit_form.test.tsx.snap index 9ae73106ac9..02c7cae7815 100644 --- a/src/components/inline_edit/__snapshots__/inline_edit_form.test.tsx.snap +++ b/src/components/inline_edit/__snapshots__/inline_edit_form.test.tsx.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`EuiInlineEditForm Edit Mode disables the save button when input is invalid 1`] = ` +exports[`EuiInlineEditForm Edit Mode editModeProps.cancelButtonProps 1`] = `
@@ -27,22 +27,13 @@ exports[`EuiInlineEditForm Edit Mode disables the save button when input is inva class="euiFormControlLayout__childrenWrapper" > -
- -
@@ -81,9 +72,8 @@ exports[`EuiInlineEditForm Edit Mode disables the save button when input is inva @@ -374,16 +386,39 @@ exports[`EuiInlineEditForm Edit Mode renders EuiSkeletonRectangles in place of e class="euiFormRow__fieldWrapper" >
+ class="emotion-euiScreenReaderOnly" + > +
+ Loaded +
+ +
@@ -393,7 +428,7 @@ exports[`EuiInlineEditForm Edit Mode renders EuiSkeletonRectangles in place of e `; -exports[`EuiInlineEditForm Edit Mode renders editModeProps.formRowProps 1`] = ` +exports[`EuiInlineEditForm Edit Mode editModeProps.saveButtonProps 1`] = `
@@ -408,7 +443,6 @@ exports[`EuiInlineEditForm Edit Mode renders editModeProps.formRowProps 1`] = ` >
`; -exports[`EuiInlineEditForm Edit Mode renders editModeProps.inputProps 1`] = ` +exports[`EuiInlineEditForm Edit Mode isInvalid 1`] = `
@@ -555,25 +589,28 @@ exports[`EuiInlineEditForm Edit Mode renders editModeProps.inputProps 1`] = ` class="euiFormRow__fieldWrapper" >
-
+
+ +
@@ -680,7 +717,110 @@ exports[`EuiInlineEditForm Edit Mode renders editModeProps.inputProps 1`] = `
`; -exports[`EuiInlineEditForm Edit Mode renders save button and cancel button aria-labels 1`] = ` +exports[`EuiInlineEditForm Edit Mode isLoading 1`] = ` +
+
+
+
+
+
+
+
+ +
+ +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+`; + +exports[`EuiInlineEditForm Edit Mode renders 1`] = `
@@ -751,7 +891,7 @@ exports[`EuiInlineEditForm Edit Mode renders save button and cancel button aria- />