Skip to content

Commit

Permalink
[WIP] Add support for second fractions in SecondInput
Browse files Browse the repository at this point in the history
Closes #56
  • Loading branch information
wojtekmaj committed Sep 27, 2021
1 parent 5371ef0 commit 231090e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/TimeInput.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function focus(element) {
function renderCustomInputs(placeholder, elementFunctions, allowMultipleInstances) {
const usedFunctions = [];
const pattern = new RegExp(
Object.keys(elementFunctions).map((el) => `${el}+`).join('|'), 'g',
Object.keys(elementFunctions).join('|'), 'g',
);
const matches = placeholder.match(pattern);

Expand Down Expand Up @@ -488,7 +488,19 @@ export default class TimeInput extends PureComponent {
throw new Error(`Unsupported token: ${currentMatch}`);
}

const showLeadingZeros = currentMatch ? currentMatch.length === 2 : true;
const showFractions = /S+/.test(currentMatch);
const showLeadingZeros = !showFractions && currentMatch ? currentMatch.length === 2 : true;

/**
* Returns "0.1" given "S"
* Returns "0.01" given "SS"
* Returns "0.001" given "SSS"
* and so on
*/
function getStepFromFormat(format) {
const step = Math.pow(10, -format.length + 1);
return step;
}

return (
<SecondInput
Expand All @@ -500,6 +512,7 @@ export default class TimeInput extends PureComponent {
inputRef={this.secondInput}
minute={minute}
placeholder={secondPlaceholder}
step={showFractions ? getStepFromFormat(currentMatch.match(/S+/)) : undefined}
showLeadingZeros={showLeadingZeros}
value={second}
/>
Expand Down Expand Up @@ -529,11 +542,11 @@ export default class TimeInput extends PureComponent {
const { format } = this.props;

const elementFunctions = {
h: this.renderHour,
H: this.renderHour,
m: this.renderMinute,
s: this.renderSecond,
a: this.renderAmPm,
'h+': this.renderHour,
'H+': this.renderHour,
'm+': this.renderMinute,
's+(\\.S+)?': this.renderSecond,
'a+': this.renderAmPm,
};

const allowMultipleInstances = typeof format !== 'undefined';
Expand Down
37 changes: 37 additions & 0 deletions src/TimeInput.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,8 @@ describe('TimeInput', () => {

expect(componentInput).toHaveLength(1);
expect(customInputs).toHaveLength(1);
expect(componentInput.prop('showLeadingZeros')).toBeFalsy();
expect(componentInput.prop('step')).toBeUndefined();
});

it('renders "ss" properly', () => {
Expand All @@ -420,6 +422,41 @@ describe('TimeInput', () => {
expect(componentInput).toHaveLength(1);
expect(customInputs).toHaveLength(1);
expect(componentInput.prop('showLeadingZeros')).toBeTruthy();
expect(componentInput.prop('step')).toBeUndefined();
});

it('renders "ss.S" properly', () => {
const component = mount(
<TimeInput
{...defaultProps}
format="ss"
/>,
);

const componentInput = component.find('SecondInput');
const customInputs = component.find('input[data-input]');

expect(componentInput).toHaveLength(1);
expect(customInputs).toHaveLength(1);
expect(componentInput.prop('showLeadingZeros')).toBeTruthy();
expect(componentInput.prop('step')).toBe('0.1');
});

it('renders "ss.SS" properly', () => {
const component = mount(
<TimeInput
{...defaultProps}
format="ss"
/>,
);

const componentInput = component.find('SecondInput');
const customInputs = component.find('input[data-input]');

expect(componentInput).toHaveLength(1);
expect(customInputs).toHaveLength(1);
expect(componentInput.prop('showLeadingZeros')).toBeTruthy();
expect(componentInput.prop('step')).toBe('0.01');
});

it('throws error for "sss"', () => {
Expand Down

0 comments on commit 231090e

Please sign in to comment.