Skip to content
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

feat: preserve white spaces on Web #485

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,15 @@ such that the total number of lines does not exceed this number. Default is '1'
| ------- | -------- | -------- |
| ColorValue | no | iOS |

### `preserveSpacesInLabel`

If set to true, the items label will be displayed with spaces preserved.

@default false
| Type | Required | Platform |
| ------- | -------- | -------- |
| boolean | no | Web |

## Methods

### `blur` (Android only, lib version 1.16.0+)
Expand Down
23 changes: 23 additions & 0 deletions example/src/PickerExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,25 @@ function DropdownMultilinePickerExample() {
);
}

function DropdownPreserveWhiteSpacesPickerExample() {
const [value, setValue] = React.useState('key1');

return (
<Picker
numberOfLines={5}
selectedValue={value}
onValueChange={(v) => setValue(v)}
mode="dropdown"
preserveSpacesInLabel>
<Item label="Lorem ipsum dolor sit amet" value="key0" />
<Item
label="quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
value="key1"
/>
</Picker>
);
}

function PromptPickerExample() {
const [value, setValue] = React.useState('key1');
return (
Expand Down Expand Up @@ -294,6 +313,10 @@ export const examples = [
title: 'Multiline Dropdown Picker',
render: DropdownMultilinePickerExample,
},
{
title: 'Preserve White Space Dropdown Picker',
render: DropdownPreserveWhiteSpacesPickerExample,
},
{
title: 'Picker with prompt message',
render: PromptPickerExample,
Expand Down
14 changes: 12 additions & 2 deletions js/Picker.web.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type PickerProps = {
itemStyle?: GenericStyleProp<TextStyle>,
mode?: string,
prompt?: string,
preserveSpacesInLabel?: boolean,
};

const Select = forwardRef((props: any, forwardedRef) =>
Expand Down Expand Up @@ -69,8 +70,17 @@ const Picker: React$AbstractComponent<PickerProps, empty> = forwardRef<
onChange={handleChange}
ref={forwardedRef}
value={selectedValue}
{...other}
/>
{...other}>
{React.Children.map(other.children, (child) => {
if (React.isValidElement(child)) {
let cloneElement = React.cloneElement(child, {
preserveSpacesInLabel: other.preserveSpacesInLabel || false,
});

return cloneElement;
}
})}
</Select>
);
});

Expand Down
12 changes: 10 additions & 2 deletions js/PickerItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@ import type {ColorValue} from 'react-native/Libraries/StyleSheet/StyleSheet';
import * as React from 'react';
import * as ReactNativeWeb from 'react-native-web';

const preserveSpaces = (label: string) => {
return label.replace(/ /g, '\u00a0');
};

type Props = {
color?: ColorValue,
label: string,
testID?: string,
enabled?: boolean,
value?: number | string,
preserveSpacesInLabel?: boolean,
};

const Option = (props: any) =>
Expand All @@ -31,15 +36,18 @@ export default function PickerItem({
testID,
value,
enabled = true,
preserveSpacesInLabel = false,
}: Props): React.Node {
const pickerLabel = preserveSpacesInLabel ? preserveSpaces(label) : label;

return (
<Option
disabled={enabled === false ? true : undefined}
style={{color}}
testID={testID}
value={value}
label={label}>
{label}
label={pickerLabel}>
{pickerLabel}
</Option>
);
}
6 changes: 6 additions & 0 deletions js/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,10 @@ export type PickerItem = $ReadOnly<{|
* @platform android
*/
enabled?: ?boolean,
/**
* If set to true, the specific item will be displayed with spaces preserved.
* default false
* @platform web
*/
preserveSpacesInLabel?: ?boolean,
|}>;
13 changes: 13 additions & 0 deletions typings/Picker.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ export interface PickerItemProps<T = ItemValue> {
* @platform android | web
*/
enabled?: boolean;
/**
* If set to true, the specific item will be displayed with spaces preserved.
* default false
* @platform web
*/
preserveSpacesInLabel?: ?boolean,
}

export interface PickerProps<T = ItemValue> extends ViewProps {
Expand Down Expand Up @@ -118,8 +124,15 @@ export interface PickerProps<T = ItemValue> extends ViewProps {
* @platform android
*/
onBlur?: (e: NativeSyntheticEvent<TargetedEvent>) => void;
/**
* If set to true, the items label will be displayed with spaces preserved.
* default false
* @platform web
*/
preserveSpacesInLabel?: ?boolean,
}


declare class Picker<T> extends React.Component<PickerProps<T>, {}> {
/**
* On Android, display the options in a dialog (this is the default).
Expand Down