Skip to content

Commit

Permalink
Optionally Controlled textinput (#1357)
Browse files Browse the repository at this point in the history
* value for text input

* number input
  • Loading branch information
tomdye authored Apr 8, 2020
1 parent 6bb8fe4 commit adabdf9
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 14 deletions.
6 changes: 6 additions & 0 deletions src/examples/src/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ import HiddenLabelTextArea from './widgets/text-area/HiddenLabel';
import ValidatedCustomTextArea from './widgets/text-area/ValidatedCustom';
import ValidatedRequiredTextArea from './widgets/text-area/ValidatedRequired';
import BasicTextInput from './widgets/text-input/Basic';
import ControlledTextInput from './widgets/text-input/Controlled';
import DisabledTextInput from './widgets/text-input/Disabled';
import HelperTextInput from './widgets/text-input/HelperText';
import HiddenLabelTextInput from './widgets/text-input/HiddenLabel';
Expand Down Expand Up @@ -1375,6 +1376,11 @@ export const config = {
module: PlaceholderTextInput,
title: 'TextInput with placeholder and no label'
},
{
filename: 'Controlled',
module: ControlledTextInput,
title: 'Controlled TextInput'
},
{
filename: 'Disabled',
module: DisabledTextInput,
Expand Down
31 changes: 31 additions & 0 deletions src/examples/src/widgets/text-input/Controlled.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { create, tsx } from '@dojo/framework/core/vdom';
import icache from '@dojo/framework/core/middleware/icache';
import TextInput from '@dojo/widgets/text-input';
import Button from '@dojo/widgets/button';

const factory = create({ icache });

const Example = factory(function Controlled({ middleware: { icache } }) {
return (
<virtual>
<TextInput
value={icache.getOrSet('value', '')}
onValue={(value) => {
icache.set('value', value);
}}
>
{{ label: 'Controlled input with reset' }}
</TextInput>
<Button
onClick={() => {
icache.set('value', '');
}}
>
Reset input
</Button>
<div>The value text input is: "{icache.getOrSet('value', '')}"</div>
</virtual>
);
});

export default Example;
10 changes: 3 additions & 7 deletions src/number-input/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,7 @@ const factory = create({ theme })
.children<TextInputChildren | undefined>();

export default factory(function NumberInput({ properties, children, middleware: { theme } }) {
const { initialValue, onValue } = properties();

const valueAsString =
initialValue !== undefined && initialValue !== null
? initialValue.toString()
: initialValue;
const { initialValue, value, onValue } = properties();

function onValueAdapter(valueAsString: string | undefined) {
if (!onValue) {
Expand All @@ -42,7 +37,8 @@ export default factory(function NumberInput({ properties, children, middleware:
return (
<TextInput
{...properties()}
initialValue={valueAsString}
value={value === undefined ? value : `${value}`}
initialValue={initialValue === undefined ? initialValue : `${initialValue}`}
onValue={onValueAdapter}
type="number"
theme={theme.compose(
Expand Down
8 changes: 7 additions & 1 deletion src/number-input/tests/unit/NumberInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const baseTemplate = assertionTemplate(() => (
theme={{ '@dojo/widgets/text-input': textInputCss }}
onValue={noop}
initialValue={undefined}
value={undefined}
/>
));

Expand All @@ -29,6 +30,10 @@ registerSuite('NumberInput', {
const h = harness(() => <NumberInput />, [compareTheme]);
h.expect(baseTemplate);
},
'can take controlled property'() {
const h = harness(() => <NumberInput value={42} />, [compareTheme]);
h.expect(baseTemplate.setProperty(':root', 'value', '42'));
},
'passes expected properties to underlying TextInput'() {
const baseProperties: BaseInputProperties<{ value: number }> = {
aria: { foo: 'bar' },
Expand All @@ -48,7 +53,8 @@ registerSuite('NumberInput', {
onValue: noop,
readOnly: true,
initialValue: 42,
widgetId: 'widgetId'
widgetId: 'widgetId',
value: undefined
};

const h = harness(
Expand Down
17 changes: 11 additions & 6 deletions src/text-input/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export interface BaseInputProperties<T extends { value: any } = { value: string
required?: boolean;
/** The initial value */
initialValue?: T['value'];
/** The controlled value */
value?: T['value'];
/** The id to be applied to the input */
widgetId?: string;
}
Expand Down Expand Up @@ -167,15 +169,18 @@ export const TextInput = factory(function TextInput({
widgetId = `text-input-${id}`
} = properties();

let { value } = properties();
const [{ label, leading, trailing } = {} as TextInputChildren] = children();

let value = icache.get('value');
const existingInitialValue = icache.get('initialValue');
if (value === undefined) {
value = icache.get('value');
const existingInitialValue = icache.get('initialValue');

if (initialValue !== existingInitialValue) {
icache.set('value', initialValue);
icache.set('initialValue', initialValue);
value = initialValue;
if (initialValue !== existingInitialValue) {
icache.set('value', initialValue);
icache.set('initialValue', initialValue);
value = initialValue;
}
}

const pattern = patternValue instanceof RegExp ? patternValue.source : patternValue;
Expand Down
19 changes: 19 additions & 0 deletions src/text-input/tests/unit/TextInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,25 @@ registerSuite('TextInput', {
h.expect(expected);
},

'controlled value'() {
const h = harness(() => <TextInput value="foo" />);
const valueTemplate = baseAssertion
.setProperty('@input', 'value', 'foo')
.setProperty('@wrapper', 'classes', [
css.wrapper,
null,
null,
null,
null,
null,
null,
null,
null,
css.noLabel
]);
h.expect(valueTemplate);
},

'custom properties'() {
const h = harness(() => (
<TextInput
Expand Down

0 comments on commit adabdf9

Please sign in to comment.