Skip to content

Commit

Permalink
feat(NumberControl.js): add prefix/suffix
Browse files Browse the repository at this point in the history
- wrap the input in 2 Div elements with custom CSS
- add example with both prefix & suffix
  • Loading branch information
ngdangtu-vn committed Aug 8, 2023
1 parent 584e348 commit 4aed4e8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
2 changes: 1 addition & 1 deletion dev-test/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ collections: # A list of collections the CMS should be able to edit
- { label: 'Boolean', name: 'boolean', widget: 'boolean', prefix: 'Toggle this to switch on/off:', hint: 'Toggle this to switch on/off', default: true }
- { label: 'Map', name: 'map', widget: 'map' }
- { label: 'Text', name: 'text', widget: 'text', hint: 'Plain text, not markdown' }
- { label: 'Number', name: 'number', widget: 'number', hint: 'To infinity and beyond!' }
- { label: 'Number', name: 'number', widget: 'number', suffix: 'px', hint: 'To infinity and beyond!' }
- { label: 'Markdown', name: 'markdown', widget: 'markdown' }
- { label: 'Datetime', name: 'datetime', widget: 'datetime' }
- { label: 'Date', name: 'date', widget: 'date' }
Expand Down
41 changes: 29 additions & 12 deletions packages/netlify-cms-widget-number/src/NumberControl.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import React from 'react';
import PropTypes from 'prop-types';
import ImmutablePropTypes from 'react-immutable-proptypes';
import { css } from '@emotion/core';

const ValidationErrorTypes = {
PRESENCE: 'PRESENCE',
PATTERN: 'PATTERN',
RANGE: 'RANGE',
CUSTOM: 'CUSTOM',
};

const innerWrapper = css`
display: flex;
align-items: baseline;
`

export function validateMinMax(value, min, max, field, t) {
let error;

Expand Down Expand Up @@ -100,19 +107,29 @@ export default class NumberControl extends React.Component {
const min = field.get('min', '');
const max = field.get('max', '');
const step = field.get('step', field.get('value_type') === 'int' ? 1 : '');

const prefix = field.get('prefix', false);
const suffix = field.get('suffix', false);

return (
<input
type="number"
id={forID}
className={classNameWrapper}
onFocus={setActiveStyle}
onBlur={setInactiveStyle}
value={value || (value === 0 ? value : '')}
step={step}
min={min}
max={max}
onChange={this.handleChange}
/>
<div className={classNameWrapper}>
<div css={innerWrapper}>
{prefix && (<span>{prefix}&nbsp;</span>)}
<input
type="number"
id={forID}
onFocus={setActiveStyle}
onBlur={setInactiveStyle}
value={value || (value === 0 ? value : '')}
step={step}
min={min}
max={max}
onChange={this.handleChange}
css={css`flex-grow: 1`}
/>
{suffix && (<span>&nbsp;{suffix}</span>)}
</div>
</div>
);
}
}

0 comments on commit 4aed4e8

Please sign in to comment.