Skip to content
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
4 changes: 3 additions & 1 deletion docs/en_US/column_dialog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion docs/en_US/table_dialog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
26 changes: 22 additions & 4 deletions web/pgadmin/static/js/components/FormComponents.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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))
Expand Down Expand Up @@ -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 })}
/>
Expand Down
33 changes: 33 additions & 0 deletions web/regression/javascript/components/FormComponents.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', ()=>{
Expand Down
5 changes: 5 additions & 0 deletions web/regression/javascript/schema_ui_files/column.ui.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down
Loading