-
Notifications
You must be signed in to change notification settings - Fork 734
Feat/number input 2 #2333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Feat/number input 2 #2333
Changes from 6 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
4495a71
NumberInput - initial commit
M-i-k-e-l 4503a99
NumberInput - complete
M-i-k-e-l f9c080c
Fix docs
M-i-k-e-l 9a5a7c3
Improve example screen
M-i-k-e-l 300d9de
Fix non en languages
M-i-k-e-l 21e5a2b
Add tests
M-i-k-e-l 0432a36
Merge branch 'master' into feat/number-input-2
M-i-k-e-l 74ea7ad
Merge branch 'master' into feat/number-input-2
M-i-k-e-l f02a45e
Use TextField's formatter and the new centered prop
M-i-k-e-l d755c81
Review changes
M-i-k-e-l f9fc114
Remove tests from npm package
M-i-k-e-l 5e69f61
Use only one screen
M-i-k-e-l 4325ab7
Prettify
M-i-k-e-l f2e2a78
Improve screen (requires next commit to work)
M-i-k-e-l cbdedb0
Add some function component support
M-i-k-e-l ab14928
Support centered for validationMessage, memoize and fix typescript
M-i-k-e-l a4560b1
Merge branch 'master' into feat/number-input-2
M-i-k-e-l 098bd3b
result to data
M-i-k-e-l 516bb05
Remove margins and fix TextField's centered
M-i-k-e-l 3e06a97
Merge branch 'master' into feat/number-input-2
M-i-k-e-l e1282aa
Remove more code and fix placeholder RN bug
M-i-k-e-l 63807e5
Fix ExampleScreenPresenter
M-i-k-e-l 91e6b39
Fix tests
M-i-k-e-l 985cf4c
Last two comments
M-i-k-e-l File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
demo/src/screens/componentScreens/NumberInputScreen/NumberInputErrorOnChangeScreen.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import React, {useState, useCallback, useRef, useMemo} from 'react'; | ||
import {StyleSheet, TouchableWithoutFeedback, Keyboard as RNKeyboard} from 'react-native'; | ||
import { | ||
Text, | ||
Spacings, | ||
NumberInput, | ||
NumberInputResult, | ||
View, | ||
Typography, | ||
Constants, | ||
Incubator | ||
} from 'react-native-ui-lib'; | ||
|
||
const NumberInputErrorOnChangeScreen = () => { | ||
const currentData = useRef<NumberInputResult>(); | ||
const minimum = useRef<number>(5000); | ||
// const [initialNumber, setInitialNumber] = useState<number>(100); | ||
const [text, setText] = useState<string>(''); | ||
|
||
const onChangeNumber = useCallback((result: NumberInputResult) => { | ||
currentData.current = result; | ||
}, []); | ||
|
||
const processInput = useCallback(() => { | ||
let newText = ''; | ||
if (currentData.current) { | ||
switch (currentData.current.type) { | ||
case 'valid': | ||
newText = currentData.current.formattedNumber; | ||
break; | ||
case 'empty': | ||
newText = 'Empty'; | ||
break; | ||
case 'error': | ||
newText = `Error: value '${currentData.current.userInput}' is invalid`; | ||
break; | ||
} | ||
} | ||
|
||
setText(newText); | ||
}, []); | ||
|
||
const isValid = useCallback(() => { | ||
return currentData.current?.type === 'valid'; | ||
}, []); | ||
|
||
const minimumPrice = useCallback(() => { | ||
if (currentData.current?.type === 'valid') { | ||
return currentData.current.number >= minimum.current; | ||
} | ||
}, []); | ||
|
||
const validate = useMemo((): Incubator.TextFieldProps['validate'] => [isValid, minimumPrice], | ||
[isValid, minimumPrice]); | ||
|
||
const validationMessage = useMemo((): Incubator.TextFieldProps['validationMessage'] => [ | ||
'Please enter a valid number', | ||
`Make sure your number is above ${minimum.current}` | ||
], | ||
[]); | ||
|
||
return ( | ||
<TouchableWithoutFeedback onPress={RNKeyboard.dismiss}> | ||
<View flex centerH> | ||
<Text text40 margin-s10> | ||
Number Input | ||
</Text> | ||
|
||
<View flex center> | ||
<NumberInput | ||
// initialNumber={initialNumber} | ||
onChangeNumber={onChangeNumber} | ||
placeholder={'Price'} | ||
leadingText={'$'} | ||
leadingTextStyle={styles.leadingText} | ||
style={styles.mainText} | ||
containerStyle={styles.containerStyle} | ||
label={'Enter Price'} | ||
labelStyle={styles.label} | ||
validate={validate} | ||
validationMessage={validationMessage} | ||
validationMessageStyle={styles.validationMessage} | ||
marginLeft={Spacings.s4} | ||
marginRight={Spacings.s4} | ||
onBlur={processInput} | ||
validateOnChange | ||
/> | ||
<Text marginT-s5>{text}</Text> | ||
</View> | ||
</View> | ||
</TouchableWithoutFeedback> | ||
); | ||
}; | ||
|
||
export default NumberInputErrorOnChangeScreen; | ||
|
||
const styles = StyleSheet.create({ | ||
containerStyle: { | ||
alignSelf: 'center', | ||
marginBottom: 30, | ||
marginLeft: Spacings.s5, | ||
marginRight: Spacings.s5 | ||
}, | ||
mainText: { | ||
height: 36, | ||
marginVertical: Spacings.s1, | ||
textAlign: 'center', | ||
...Typography.text30M | ||
}, | ||
leadingText: { | ||
marginTop: Constants.isIOS ? Spacings.s2 : 0, | ||
...Typography.text50M | ||
}, | ||
label: { | ||
textAlign: 'center', | ||
marginBottom: Spacings.s1, | ||
...Typography.bodySmallMedium | ||
}, | ||
validationMessage: { | ||
flexGrow: 1, | ||
textAlign: 'center', | ||
...Typography.bodySmallMedium | ||
} | ||
}); |
95 changes: 95 additions & 0 deletions
95
demo/src/screens/componentScreens/NumberInputScreen/NumberInputProcessButtonScreen.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import React, {useState, useCallback, useRef} from 'react'; | ||
import {StyleSheet, TouchableWithoutFeedback, Keyboard as RNKeyboard} from 'react-native'; | ||
import {Text, Spacings, NumberInput, NumberInputResult, View, Typography, Constants, Button} from 'react-native-ui-lib'; | ||
|
||
const NumberInputProcessButtonScreen = () => { | ||
const currentData = useRef<NumberInputResult>(); | ||
// const [initialNumber, setInitialNumber] = useState<number>(100); | ||
lidord-wix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const [text, setText] = useState<string>(''); | ||
|
||
const onChangeNumber = useCallback((result: NumberInputResult) => { | ||
currentData.current = result; | ||
}, []); | ||
|
||
const processInput = useCallback(() => { | ||
let newText = ''; | ||
if (currentData.current) { | ||
switch (currentData.current.type) { | ||
case 'valid': | ||
newText = currentData.current.formattedNumber; | ||
break; | ||
case 'empty': | ||
newText = 'Empty'; | ||
break; | ||
case 'error': | ||
newText = `Error: value '${currentData.current.userInput}' is invalid`; | ||
break; | ||
} | ||
} | ||
|
||
setText(newText); | ||
}, []); | ||
|
||
return ( | ||
<TouchableWithoutFeedback onPress={RNKeyboard.dismiss}> | ||
<View flex centerH> | ||
<Text text40 margin-s10> | ||
Number Input | ||
</Text> | ||
|
||
<View flex center> | ||
<NumberInput | ||
// initialNumber={initialNumber} | ||
onChangeNumber={onChangeNumber} | ||
placeholder={'Price'} | ||
leadingText={'$'} | ||
leadingTextStyle={styles.leadingText} | ||
style={styles.mainText} | ||
containerStyle={styles.containerStyle} | ||
label={'Enter Price'} | ||
labelStyle={styles.label} | ||
validate={'required'} | ||
lidord-wix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
validationMessage={'Please enter a price'} | ||
validationMessageStyle={styles.validationMessage} | ||
marginLeft={Spacings.s4} | ||
marginRight={Spacings.s4} | ||
onBlur={processInput} | ||
/> | ||
<Button label={'Process'} onPress={processInput}/> | ||
lidord-wix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<Text marginT-s5>{text}</Text> | ||
</View> | ||
</View> | ||
</TouchableWithoutFeedback> | ||
); | ||
}; | ||
|
||
export default NumberInputProcessButtonScreen; | ||
|
||
const styles = StyleSheet.create({ | ||
containerStyle: { | ||
alignSelf: 'center', | ||
marginBottom: 30, | ||
marginLeft: Spacings.s5, | ||
marginRight: Spacings.s5 | ||
}, | ||
mainText: { | ||
height: 36, | ||
marginVertical: Spacings.s1, | ||
textAlign: 'center', | ||
...Typography.text30M | ||
}, | ||
leadingText: { | ||
marginTop: Constants.isIOS ? Spacings.s2 : 0, | ||
...Typography.text50M | ||
}, | ||
label: { | ||
textAlign: 'center', | ||
marginBottom: Spacings.s1, | ||
...Typography.bodySmallMedium | ||
}, | ||
validationMessage: { | ||
flexGrow: 1, | ||
textAlign: 'center', | ||
...Typography.bodySmallMedium | ||
} | ||
}); |
42 changes: 42 additions & 0 deletions
42
demo/src/screens/componentScreens/NumberInputScreen/index.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import React, {Component} from 'react'; | ||
import {Colors, View, TabController} from 'react-native-ui-lib'; | ||
import _ from 'lodash'; | ||
import {gestureHandlerRootHOC} from 'react-native-gesture-handler'; | ||
|
||
import NumberInputProcessButtonScreen from './NumberInputProcessButtonScreen'; | ||
import NumberInputErrorOnChangeScreen from './NumberInputErrorOnChangeScreen'; | ||
|
||
const SCREENS = [ | ||
{title: 'Process button', screen: NumberInputProcessButtonScreen}, | ||
{title: 'Error on change', screen: NumberInputErrorOnChangeScreen} | ||
lidord-wix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
]; | ||
|
||
class NumberInputScreen extends Component { | ||
state = {}; | ||
|
||
renderPages() { | ||
return ( | ||
<View flex> | ||
{_.map(SCREENS, (item, index) => { | ||
const Screen = item.screen; | ||
return ( | ||
<TabController.TabPage key={`${item.title}_page`} index={index}> | ||
<Screen/> | ||
</TabController.TabPage> | ||
); | ||
lidord-wix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
})} | ||
</View> | ||
); | ||
} | ||
|
||
render() { | ||
return ( | ||
<TabController items={SCREENS.map(item => ({label: item.title}))}> | ||
<TabController.TabBar activeBackgroundColor={Colors.blue70}/> | ||
lidord-wix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{this.renderPages()} | ||
</TabController> | ||
); | ||
} | ||
} | ||
|
||
export default gestureHandlerRootHOC(NumberInputScreen); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import {isEmpty} from 'lodash'; | ||
|
||
export type NumberInputResult = | ||
lidord-wix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| {type: 'valid'; userInput: string; number: number; formattedNumber: string} | ||
| {type: 'empty'} | ||
| {type: 'error'; userInput: string}; | ||
|
||
export const EMPTY: NumberInputResult = {type: 'empty'}; | ||
|
||
export interface LocaleOptions { | ||
locale: string; | ||
decimalSeparator: string; | ||
thousandSeparator: string; | ||
} | ||
|
||
export interface Options { | ||
localeOptions: LocaleOptions; | ||
fractionDigits: number; | ||
} | ||
|
||
function formatNumber(value: number, options: Options) { | ||
return value.toLocaleString(options.localeOptions.locale, {maximumFractionDigits: options.fractionDigits}); | ||
} | ||
|
||
function generateLocaleOptions(locale: string) { | ||
const options: Options = { | ||
localeOptions: { | ||
locale, | ||
decimalSeparator: '', // fake decimalSeparator, we're creating it now | ||
thousandSeparator: '' // fake thousandSeparator, we're creating it now | ||
}, | ||
fractionDigits: 1 | ||
}; | ||
const decimalSeparator = formatNumber(1.1, options).replace(/1/g, ''); | ||
const thousandSeparator = formatNumber(1111, options).replace(/1/g, ''); | ||
|
||
return { | ||
locale, | ||
decimalSeparator, | ||
thousandSeparator | ||
}; | ||
} | ||
|
||
export function generateOptions(locale: string, fractionDigits: number): Options { | ||
return {localeOptions: generateLocaleOptions(locale), fractionDigits}; | ||
} | ||
|
||
export function parseInput(text: string, options: Options): NumberInputResult { | ||
if (isEmpty(text)) { | ||
return EMPTY; | ||
} | ||
|
||
let cleanInput: string = text.replaceAll(options.localeOptions.thousandSeparator, ''); | ||
cleanInput = cleanInput.replaceAll(options.localeOptions.decimalSeparator, '.'); | ||
let number = Number(cleanInput); | ||
if (isNaN(number)) { | ||
return {type: 'error', userInput: text}; | ||
} | ||
|
||
number = Number(number.toFixed(options.fractionDigits)); | ||
const formattedNumber = formatNumber(number, options); | ||
|
||
return {type: 'valid', userInput: text, number, formattedNumber}; | ||
} | ||
|
||
export function getInitialResult(options: Options, initialValue?: number): NumberInputResult { | ||
if (initialValue === undefined) { | ||
return EMPTY; | ||
} | ||
|
||
return parseInput(formatNumber(initialValue, options), options); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.