diff --git a/docs/en_US/column_dialog.rst b/docs/en_US/column_dialog.rst index 1c0e269fbf4..c680b09bd01 100644 --- a/docs/en_US/column_dialog.rst +++ b/docs/en_US/column_dialog.rst @@ -18,7 +18,9 @@ SQL code generated by dialog selections. Use the fields in the *General* tab to identify the column: * Use the *Name* field to add a descriptive name for the column. The name will - be displayed in the *pgAdmin* tree control. This field is required. + be displayed in the *pgAdmin* tree control. This field is required. Leading + and trailing whitespace is removed from a name you type when you leave the + field. * Store notes about the column in the *Comment* field. Click the *Definition* tab to continue. diff --git a/docs/en_US/table_dialog.rst b/docs/en_US/table_dialog.rst index ee770beae5c..12a24d1ec76 100644 --- a/docs/en_US/table_dialog.rst +++ b/docs/en_US/table_dialog.rst @@ -47,7 +47,8 @@ they must be modified at the parent level. Click the *Add* icon (+) to specify the names of columns and their datatypes in the *Columns* table: -* Use the *Name* field to add a descriptive name for the column. +* Use the *Name* field to add a descriptive name for the column. Leading and + trailing whitespace is removed from a name you type when you leave the field. * Use the drop-down listbox in the *Data type* field to select a data type for the column. This can include array specifiers. For more information on the data types supported by PostgreSQL, refer to Chapter 8 of the core diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/columns/static/js/column.ui.js b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/columns/static/js/column.ui.js index dc6958a128d..98f4692042c 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/columns/static/js/column.ui.js +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/columns/static/js/column.ui.js @@ -204,7 +204,7 @@ export default class ColumnSchema extends BaseUISchema { id: 'name', label: gettext('Name'), cell: 'text', type: 'text', readonly: obj.inSchemaWithColumnCheck, editable: this.editableCheckForTable, noEmpty: true, - width: 115, + width: 115, controlProps: { trimOnBlur: true }, },{ // Need to show this field only when creating new table // [in SubNode control] diff --git a/web/pgadmin/static/js/components/FormComponents.jsx b/web/pgadmin/static/js/components/FormComponents.jsx index 29b129e1bf3..631f6731f69 100644 --- a/web/pgadmin/static/js/components/FormComponents.jsx +++ b/web/pgadmin/static/js/components/FormComponents.jsx @@ -438,6 +438,9 @@ export function InputText({ref, cid, helpid, readonly, disabled, value, onChange if (_.isNull(finalValue) || _.isUndefined(finalValue)) finalValue = ''; const [val, setVal] = useState(finalValue); + // Set once the user edits the value, so that merely focusing and leaving + // the field does not trim a value that came from the server. + const editedRef = useRef(false); useEffect(() => setVal(finalValue), [finalValue]); const onChangeFinal = (e) => { @@ -452,10 +455,27 @@ export function InputText({ref, cid, helpid, readonly, disabled, value, onChange if (controlProps?.formatter) { changeVal = controlProps.formatter.toRaw(changeVal); } + editedRef.current = true; setVal(changeVal); onChange?.(changeVal); }; + // Trim leading and trailing whitespace when the user leaves the field, + // rather than on every keystroke, so that spaces inside the value can + // still be typed. + const {trimOnBlur, ...inputControlProps} = controlProps ?? {}; + const onBlurFinal = (e) => { + if (trimOnBlur && editedRef.current && typeof(val) === 'string') { + const trimmed = val.trim(); + if (trimmed !== val) { + setVal(trimmed); + onChange?.(trimmed); + } + } + editedRef.current = false; + controlProps?.onBlur?.(e); + }; + const filteredProps = _.pickBy(props, (_v, key)=>( /* When used in ButtonGroup, following props should be skipped */ (!['color', 'disableElevation', 'disableFocusRipple', 'disableRipple'].includes(key)) @@ -490,10 +510,8 @@ export function InputText({ref, cid, helpid, readonly, disabled, value, onChange { ...(controlProps?.onFocus && { onFocus: controlProps.onFocus }) } - { - ...(controlProps?.onBlur && { onBlur: controlProps.onBlur }) - } - {...controlProps} + {...inputControlProps} + onBlur={onBlurFinal} {...filteredProps} {...(['numeric', 'int'].indexOf(type) > -1 ? { type: 'tel' } : { type: type })} /> diff --git a/web/regression/javascript/components/FormComponents.spec.js b/web/regression/javascript/components/FormComponents.spec.js index 9d10bb6b382..56c83071ebe 100644 --- a/web/regression/javascript/components/FormComponents.spec.js +++ b/web/regression/javascript/components/FormComponents.spec.js @@ -95,6 +95,39 @@ describe('FormComponents', ()=>{ it('accessibility', ()=>{ onAccessibility(); }); + + it.each([ + [true, ' my col ', 'my col', [' my col ', 'my col']], + [true, 'my col', 'my col', ['my col']], + [false, ' my col ', ' my col ', [' my col ']], + ])('trimOnBlur=%s, typed %p, blurred to %p', + (trimOnBlur, typed, expected, expectedCalls)=>{ + const onChange = jest.fn(); + ctrlRerender({ + value: '', + onChange: onChange, + controlProps: {trimOnBlur: trimOnBlur}, + }); + const input = screen.getByTestId('input-text'); + fireEvent.change(input, {target: {value: typed}}); + fireEvent.blur(input); + expect(input.value).toBe(expected); + expect(onChange.mock.calls.map((c)=>c[0])).toEqual(expectedCalls); + }); + + it('trimOnBlur does not trim a value the user has not edited', ()=>{ + const onChange = jest.fn(); + ctrlRerender({ + value: ' existing ', + onChange: onChange, + controlProps: {trimOnBlur: true}, + }); + const input = screen.getByTestId('input-text'); + fireEvent.focus(input); + fireEvent.blur(input); + expect(input.value).toBe(' existing '); + expect(onChange).not.toHaveBeenCalled(); + }); }); describe('FormInputFileSelect', ()=>{ diff --git a/web/regression/javascript/schema_ui_files/column.ui.spec.js b/web/regression/javascript/schema_ui_files/column.ui.spec.js index ce31215b909..b8f9b8255ef 100644 --- a/web/regression/javascript/schema_ui_files/column.ui.spec.js +++ b/web/regression/javascript/schema_ui_files/column.ui.spec.js @@ -224,6 +224,11 @@ describe('ColumnSchema', ()=>{ ]); }); + it('name - trims leading and trailing whitespace on blur', ()=>{ + let nameField = _.find(schemaObj.fields, (f)=>f.id === 'name'); + expect(nameField.controlProps?.trimOnBlur).toBe(true); + }); + it('editableCheckForTable', ()=>{ let state = {}; schemaObj.nodeInfo = {};